PageRenderTime 35ms CodeModel.GetById 21ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/cake/libs/controller/component.php

http://skygames.googlecode.com/
PHP | 259 lines | 138 code | 14 blank | 107 comment | 33 complexity | 619cd8df61c3cc921c1789230926aba7 MD5 | raw file
  1<?php
  2/* SVN FILE: $Id: component.php 7809 2008-10-30 19:39:18Z john $ */
  3/**
  4 *
  5 * PHP versions 4 and 5
  6 *
  7 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
  8 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  9 *
 10 * Licensed under The MIT License
 11 * Redistributions of files must retain the above copyright notice.
 12 *
 13 * @filesource
 14 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 15 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 16 * @package       cake
 17 * @subpackage    cake.cake.libs.controller
 18 * @since         CakePHP(tm) v TBD
 19 * @version       $Revision: 7809 $
 20 * @modifiedby    $LastChangedBy: john $
 21 * @lastmodified  $Date: 2008-10-30 14:39:18 -0500 (Thu, 30 Oct 2008) $
 22 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 23 */
 24/**
 25 * Handler for Controller::$components
 26 *
 27 * @package       cake
 28 * @subpackage    cake.cake.libs.controller
 29 * @link          http://book.cakephp.org/view/62/Components
 30 */
 31class Component extends Object {
 32/**
 33 * Contains various controller variable information (plugin, name, base).
 34 *
 35 * @var object
 36 * @access private
 37 */
 38	var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
 39/**
 40 * List of loaded components.
 41 *
 42 * @var object
 43 * @access protected
 44 */
 45	var $_loaded = array();
 46/**
 47 * List of components attached directly to the controller, which callbacks
 48 * should be executed on.
 49 *
 50 * @var object
 51 * @access protected
 52 */
 53	var $_primary = array();
 54/**
 55 * Settings for loaded components.
 56 *
 57 * @var array
 58 * @access private
 59 **/
 60	var $__settings = array();
 61/**
 62 * Used to initialize the components for current controller.
 63 *
 64 * @param object $controller Controller with components to load
 65 * @return void
 66 * @access public
 67 */
 68	function init(&$controller) {
 69		if (!is_array($controller->components)) {
 70			return;
 71		}
 72		$this->__controllerVars = array(
 73			'plugin' => $controller->plugin, 'name' => $controller->name,
 74			'base' => $controller->base
 75		);
 76
 77		if (!in_array('Session', $controller->components)) {
 78			array_unshift($controller->components, 'Session');
 79		}
 80		$this->_loadComponents($controller);
 81	}
 82/**
 83 * Called before the Controller::beforeFilter().
 84 *
 85 * @param object $controller Controller with components to initialize
 86 * @return void
 87 * @access public
 88 * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
 89 */
 90	function initialize(&$controller) {
 91		foreach (array_keys($this->_loaded) as $name) {
 92			$component =& $this->_loaded[$name];
 93
 94			if (method_exists($component,'initialize') && $component->enabled === true) {
 95				$settings = array();
 96				if (isset($this->__settings[$name])) {
 97					$settings = $this->__settings[$name];
 98				}
 99				$component->initialize($controller, $settings);
100			}
101		}
102	}
103/**
104 * Called after the Controller::beforeFilter() and before the controller action
105 *
106 * @param object $controller Controller with components to startup
107 * @return void
108 * @access public
109 * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
110 */
111	function startup(&$controller) {
112		foreach ($this->_primary as $name) {
113			$component =& $this->_loaded[$name];
114			if ($component->enabled === true && method_exists($component, 'startup')) {
115				$component->startup($controller);
116			}
117		}
118	}
119/**
120 * Called after the Controller::beforeRender(), after the view class is loaded, and before the
121 * Controller::render()
122 *
123 * @param object $controller Controller with components to beforeRender
124 * @return void
125 * @access public
126 */
127	function beforeRender(&$controller) {
128		foreach ($this->_primary as $name) {
129			$component =& $this->_loaded[$name];
130			if ($component->enabled === true && method_exists($component,'beforeRender')) {
131				$component->beforeRender($controller);
132			}
133		}
134	}
135/**
136 * Called before Controller::redirect().
137 *
138 * @param object $controller Controller with components to beforeRedirect
139 * @return void
140 * @access public
141 */
142	function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
143		$response = array();
144
145		foreach ($this->_primary as $name) {
146			$component =& $this->_loaded[$name];
147
148			if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
149				$resp = $component->beforeRedirect($controller, $url, $status, $exit);
150				if ($resp === false) {
151					return false;
152				}
153				$response[] = $resp;
154			}
155		}
156		return $response;
157	}
158/**
159 * Called after Controller::render() and before the output is printed to the browser.
160 *
161 * @param object $controller Controller with components to shutdown
162 * @return void
163 * @access public
164 */
165	function shutdown(&$controller) {
166		foreach ($this->_primary as $name) {
167			$component =& $this->_loaded[$name];
168			if (method_exists($component,'shutdown') && $component->enabled === true) {
169				$component->shutdown($controller);
170			}
171		}
172	}
173/**
174 * Loads components used by this component.
175 *
176 * @param object $object Object with a Components array
177 * @param object $parent the parent of the current object
178 * @return void
179 * @access protected
180 */
181	function _loadComponents(&$object, $parent = null) {
182		$components = $object->components;
183		$base = $this->__controllerVars['base'];
184
185		if (is_array($object->components)) {
186			$normal = Set::normalize($object->components);
187			foreach ($normal as $component => $config) {
188				$plugin = null;
189
190				if (isset($this->__controllerVars['plugin'])) {
191					$plugin = $this->__controllerVars['plugin'] . '.';
192				}
193
194				if (strpos($component, '.') !== false) {
195					list($plugin, $component) = explode('.', $component);
196					$plugin = $plugin . '.';
197				}
198				$componentCn = $component . 'Component';
199
200				if (!class_exists($componentCn)) {
201					if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
202						if (!App::import('Component', $component)) {
203							$this->cakeError('missingComponentFile', array(array(
204								'className' => $this->__controllerVars['name'],
205								'component' => $component,
206								'file' => Inflector::underscore($component) . '.php',
207								'base' => $base,
208								'code' => 500
209							)));
210							return false;
211						}
212					}
213
214					if (!class_exists($componentCn)) {
215						$this->cakeError('missingComponentClass', array(array(
216							'className' => $this->__controllerVars['name'],
217							'component' => $component,
218							'file' => Inflector::underscore($component) . '.php',
219							'base' => $base,
220							'code' => 500
221						)));
222						return false;
223					}
224				}
225
226				if ($parent === null) {
227					$this->_primary[] = $component;
228				}
229
230				if (isset($this->_loaded[$component])) {
231					$object->{$component} =& $this->_loaded[$component];
232
233					if (!empty($config) && isset($this->__settings[$component])) {
234						$this->__settings[$component] = array_merge($this->__settings[$component], $config);
235					} elseif (!empty($config)) {
236						$this->__settings[$component] = $config;
237					}
238				} else {
239					if ($componentCn === 'SessionComponent') {
240						$object->{$component} =& new $componentCn($base);
241					} else {
242						$object->{$component} =& new $componentCn();
243					}
244					$object->{$component}->enabled = true;
245					$this->_loaded[$component] =& $object->{$component};
246					if (!empty($config)) {
247						$this->__settings[$component] = $config;
248					}
249				}
250
251				if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
252					$this->_loadComponents($object->{$component}, $component);
253				}
254			}
255		}
256	}
257}
258
259?>