PageRenderTime 39ms CodeModel.GetById 31ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/controller/component.php

https://github.com/yisraeldov/iPhPhoto
PHP | 255 lines | 135 code | 13 blank | 107 comment | 33 complexity | 5c44285ee88b4d559d977fb02eda02a3 MD5 | raw file
  1<?php
  2/* SVN FILE: $Id: component.php 8120 2009-03-19 20:25:10Z gwoo $ */
  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: 8120 $
 20 * @modifiedby    $LastChangedBy: gwoo $
 21 * @lastmodified  $Date: 2009-03-19 22:25:10 +0200 (ה', 19 מרץ 2009) $
 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		$this->_loadComponents($controller);
 78	}
 79/**
 80 * Called before the Controller::beforeFilter().
 81 *
 82 * @param object $controller Controller with components to initialize
 83 * @return void
 84 * @access public
 85 * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
 86 */
 87	function initialize(&$controller) {
 88		foreach (array_keys($this->_loaded) as $name) {
 89			$component =& $this->_loaded[$name];
 90
 91			if (method_exists($component,'initialize') && $component->enabled === true) {
 92				$settings = array();
 93				if (isset($this->__settings[$name])) {
 94					$settings = $this->__settings[$name];
 95				}
 96				$component->initialize($controller, $settings);
 97			}
 98		}
 99	}
100/**
101 * Called after the Controller::beforeFilter() and before the controller action
102 *
103 * @param object $controller Controller with components to startup
104 * @return void
105 * @access public
106 * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
107 */
108	function startup(&$controller) {
109		foreach ($this->_primary as $name) {
110			$component =& $this->_loaded[$name];
111			if ($component->enabled === true && method_exists($component, 'startup')) {
112				$component->startup($controller);
113			}
114		}
115	}
116/**
117 * Called after the Controller::beforeRender(), after the view class is loaded, and before the
118 * Controller::render()
119 *
120 * @param object $controller Controller with components to beforeRender
121 * @return void
122 * @access public
123 */
124	function beforeRender(&$controller) {
125		foreach ($this->_primary as $name) {
126			$component =& $this->_loaded[$name];
127			if ($component->enabled === true && method_exists($component,'beforeRender')) {
128				$component->beforeRender($controller);
129			}
130		}
131	}
132/**
133 * Called before Controller::redirect().
134 *
135 * @param object $controller Controller with components to beforeRedirect
136 * @return void
137 * @access public
138 */
139	function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
140		$response = array();
141
142		foreach ($this->_primary as $name) {
143			$component =& $this->_loaded[$name];
144
145			if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
146				$resp = $component->beforeRedirect($controller, $url, $status, $exit);
147				if ($resp === false) {
148					return false;
149				}
150				$response[] = $resp;
151			}
152		}
153		return $response;
154	}
155/**
156 * Called after Controller::render() and before the output is printed to the browser.
157 *
158 * @param object $controller Controller with components to shutdown
159 * @return void
160 * @access public
161 */
162	function shutdown(&$controller) {
163		foreach ($this->_primary as $name) {
164			$component =& $this->_loaded[$name];
165			if (method_exists($component,'shutdown') && $component->enabled === true) {
166				$component->shutdown($controller);
167			}
168		}
169	}
170/**
171 * Loads components used by this component.
172 *
173 * @param object $object Object with a Components array
174 * @param object $parent the parent of the current object
175 * @return void
176 * @access protected
177 */
178	function _loadComponents(&$object, $parent = null) {
179		$base = $this->__controllerVars['base'];
180		$normal = Set::normalize($object->components);
181		if ($parent == null) {
182			$normal = Set::merge(array('Session' => null), $normal);
183		}
184		foreach ((array)$normal as $component => $config) {
185			$plugin = null;
186
187			if (isset($this->__controllerVars['plugin'])) {
188				$plugin = $this->__controllerVars['plugin'] . '.';
189			}
190
191			if (strpos($component, '.') !== false) {
192				list($plugin, $component) = explode('.', $component);
193				$plugin = $plugin . '.';
194			}
195			$componentCn = $component . 'Component';
196
197			if (!class_exists($componentCn)) {
198				if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
199					if (!App::import('Component', $component)) {
200						$this->cakeError('missingComponentFile', array(array(
201							'className' => $this->__controllerVars['name'],
202							'component' => $component,
203							'file' => Inflector::underscore($component) . '.php',
204							'base' => $base,
205							'code' => 500
206						)));
207						return false;
208					}
209				}
210
211				if (!class_exists($componentCn)) {
212					$this->cakeError('missingComponentClass', array(array(
213						'className' => $this->__controllerVars['name'],
214						'component' => $component,
215						'file' => Inflector::underscore($component) . '.php',
216						'base' => $base,
217						'code' => 500
218					)));
219					return false;
220				}
221			}
222
223			if ($parent === null) {
224				$this->_primary[] = $component;
225			}
226
227			if (isset($this->_loaded[$component])) {
228				$object->{$component} =& $this->_loaded[$component];
229
230				if (!empty($config) && isset($this->__settings[$component])) {
231					$this->__settings[$component] = array_merge($this->__settings[$component], $config);
232				} elseif (!empty($config)) {
233					$this->__settings[$component] = $config;
234				}
235			} else {
236				if ($componentCn === 'SessionComponent') {
237					$object->{$component} =& new $componentCn($base);
238				} else {
239					$object->{$component} =& new $componentCn();
240				}
241				$object->{$component}->enabled = true;
242				$this->_loaded[$component] =& $object->{$component};
243				if (!empty($config)) {
244					$this->__settings[$component] = $config;
245				}
246			}
247
248			if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
249				$this->_loadComponents($object->{$component}, $component);
250			}
251		}
252	}
253}
254
255?>