6
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
7
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
9
* Licensed under The MIT License
10
* Redistributions of files must retain the above copyright notice.
12
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
13
* @link http://cakephp.org CakePHP(tm) Project
15
* @subpackage cake.cake.libs.controller
16
* @since CakePHP(tm) v TBD
17
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
21
* Handler for Controller::$components
24
* @subpackage cake.cake.libs.controller
25
* @link http://book.cakephp.org/view/993/Components
27
class Component extends Object {
30
* Contains various controller variable information (plugin, name, base).
35
var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
38
* List of loaded components.
43
var $_loaded = array();
46
* List of components attached directly to the controller, which callbacks
47
* should be executed on.
52
var $_primary = array();
55
* Settings for loaded components.
60
var $__settings = array();
63
* Used to initialize the components for current controller.
65
* @param object $controller Controller with components to load
69
function init(&$controller) {
70
if (!is_array($controller->components)) {
73
$this->__controllerVars = array(
74
'plugin' => $controller->plugin, 'name' => $controller->name,
75
'base' => $controller->base
78
$this->_loadComponents($controller);
82
* Called before the Controller::beforeFilter().
84
* @param object $controller Controller with components to initialize
87
* @link http://book.cakephp.org/view/998/MVC-Class-Access-Within-Components
89
function initialize(&$controller) {
90
foreach (array_keys($this->_loaded) as $name) {
91
$component =& $this->_loaded[$name];
93
if (method_exists($component,'initialize') && $component->enabled === true) {
95
if (isset($this->__settings[$name])) {
96
$settings = $this->__settings[$name];
98
$component->initialize($controller, $settings);
104
* Called after the Controller::beforeFilter() and before the controller action
106
* @param object $controller Controller with components to startup
109
* @link http://book.cakephp.org/view/998/MVC-Class-Access-Within-Components
110
* @deprecated See Component::triggerCallback()
112
function startup(&$controller) {
113
$this->triggerCallback('startup', $controller);
117
* Called after the Controller::beforeRender(), after the view class is loaded, and before the
118
* Controller::render()
120
* @param object $controller Controller with components to beforeRender
123
* @deprecated See Component::triggerCallback()
125
function beforeRender(&$controller) {
126
$this->triggerCallback('beforeRender', $controller);
130
* Called before Controller::redirect().
132
* @param object $controller Controller with components to beforeRedirect
136
function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
139
foreach ($this->_primary as $name) {
140
$component =& $this->_loaded[$name];
142
if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
143
$resp = $component->beforeRedirect($controller, $url, $status, $exit);
144
if ($resp === false) {
154
* Called after Controller::render() and before the output is printed to the browser.
156
* @param object $controller Controller with components to shutdown
159
* @deprecated See Component::triggerCallback()
161
function shutdown(&$controller) {
162
$this->triggerCallback('shutdown', $controller);
166
* Trigger a callback on all primary components. Will fire $callback on all components
167
* that have such a method. You can implement and fire custom callbacks in addition to the
170
* example use, from inside a controller:
172
* `$this->Component->triggerCallback('beforeFilter', $this);`
174
* will trigger the beforeFilter callback on all components that have implemented one. You
175
* can trigger any method in this fashion.
177
* @param Controller $controller Controller instance
178
* @param string $callback Callback to trigger.
182
function triggerCallback($callback, &$controller) {
183
foreach ($this->_primary as $name) {
184
$component =& $this->_loaded[$name];
185
if (method_exists($component, $callback) && $component->enabled === true) {
186
$component->{$callback}($controller);
192
* Loads components used by this component.
194
* @param object $object Object with a Components array
195
* @param object $parent the parent of the current object
199
function _loadComponents(&$object, $parent = null) {
200
$base = $this->__controllerVars['base'];
201
$normal = Set::normalize($object->components);
202
foreach ((array)$normal as $component => $config) {
203
$plugin = isset($this->__controllerVars['plugin']) ? $this->__controllerVars['plugin'] . '.' : null;
204
list($plugin, $component) = pluginSplit($component, true, $plugin);
205
$componentCn = $component . 'Component';
207
if (!class_exists($componentCn)) {
208
if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
209
if (!App::import('Component', $component)) {
210
$this->cakeError('missingComponentFile', array(array(
211
'className' => $this->__controllerVars['name'],
212
'component' => $component,
213
'file' => Inflector::underscore($component) . '.php',
221
if (!class_exists($componentCn)) {
222
$this->cakeError('missingComponentClass', array(array(
223
'className' => $this->__controllerVars['name'],
224
'component' => $component,
225
'file' => Inflector::underscore($component) . '.php',
233
if ($parent === null) {
234
$this->_primary[] = $component;
237
if (isset($this->_loaded[$component])) {
238
$object->{$component} =& $this->_loaded[$component];
240
if (!empty($config) && isset($this->__settings[$component])) {
241
$this->__settings[$component] = array_merge($this->__settings[$component], $config);
242
} elseif (!empty($config)) {
243
$this->__settings[$component] = $config;
246
if ($componentCn === 'SessionComponent') {
247
$object->{$component} =& new $componentCn($base);
249
$object->{$component} =& new $componentCn();
251
$object->{$component}->enabled = true;
252
$this->_loaded[$component] =& $object->{$component};
253
if (!empty($config)) {
254
$this->__settings[$component] = $config;
258
if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
259
$this->_loadComponents($object->{$component}, $component);