PageRenderTime 44ms CodeModel.GetById 13ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/controller/component.php

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