PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/libs/controller/component.php

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