component.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: component.php 8031 2009-02-15 20:13:14Z gwoo $ */
00003 /**
00004  *
00005  * PHP versions 4 and 5
00006  *
00007  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00008  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00009  *
00010  * Licensed under The MIT License
00011  * Redistributions of files must retain the above copyright notice.
00012  *
00013  * @filesource
00014  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00015  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00016  * @package       cake
00017  * @subpackage    cake.cake.libs.controller
00018  * @since         CakePHP(tm) v TBD
00019  * @version       $Revision: 8031 $
00020  * @modifiedby    $LastChangedBy: gwoo $
00021  * @lastmodified  $Date: 2009-02-15 15:13:14 -0500 (Sun, 15 Feb 2009) $
00022  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00023  */
00024 /**
00025  * Handler for Controller::$components
00026  *
00027  * @package       cake
00028  * @subpackage    cake.cake.libs.controller
00029  * @link          http://book.cakephp.org/view/62/Components
00030  */
00031 class Component extends Object {
00032 /**
00033  * Contains various controller variable information (plugin, name, base).
00034  *
00035  * @var object
00036  * @access private
00037  */
00038     var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
00039 /**
00040  * List of loaded components.
00041  *
00042  * @var object
00043  * @access protected
00044  */
00045     var $_loaded = array();
00046 /**
00047  * List of components attached directly to the controller, which callbacks
00048  * should be executed on.
00049  *
00050  * @var object
00051  * @access protected
00052  */
00053     var $_primary = array();
00054 /**
00055  * Settings for loaded components.
00056  *
00057  * @var array
00058  * @access private
00059  **/
00060     var $__settings = array();
00061 /**
00062  * Used to initialize the components for current controller.
00063  *
00064  * @param object $controller Controller with components to load
00065  * @return void
00066  * @access public
00067  */
00068     function init(&$controller) {
00069         if (!is_array($controller->components)) {
00070             return;
00071         }
00072         $this->__controllerVars = array(
00073             'plugin' => $controller->plugin, 'name' => $controller->name,
00074             'base' => $controller->base
00075         );
00076 
00077         $this->_loadComponents($controller);
00078     }
00079 /**
00080  * Called before the Controller::beforeFilter().
00081  *
00082  * @param object $controller Controller with components to initialize
00083  * @return void
00084  * @access public
00085  * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
00086  */
00087     function initialize(&$controller) {
00088         foreach (array_keys($this->_loaded) as $name) {
00089             $component =& $this->_loaded[$name];
00090 
00091             if (method_exists($component,'initialize') && $component->enabled === true) {
00092                 $settings = array();
00093                 if (isset($this->__settings[$name])) {
00094                     $settings = $this->__settings[$name];
00095                 }
00096                 $component->initialize($controller, $settings);
00097             }
00098         }
00099     }
00100 /**
00101  * Called after the Controller::beforeFilter() and before the controller action
00102  *
00103  * @param object $controller Controller with components to startup
00104  * @return void
00105  * @access public
00106  * @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
00107  */
00108     function startup(&$controller) {
00109         foreach ($this->_primary as $name) {
00110             $component =& $this->_loaded[$name];
00111             if ($component->enabled === true && method_exists($component, 'startup')) {
00112                 $component->startup($controller);
00113             }
00114         }
00115     }
00116 /**
00117  * Called after the Controller::beforeRender(), after the view class is loaded, and before the
00118  * Controller::render()
00119  *
00120  * @param object $controller Controller with components to beforeRender
00121  * @return void
00122  * @access public
00123  */
00124     function beforeRender(&$controller) {
00125         foreach ($this->_primary as $name) {
00126             $component =& $this->_loaded[$name];
00127             if ($component->enabled === true && method_exists($component,'beforeRender')) {
00128                 $component->beforeRender($controller);
00129             }
00130         }
00131     }
00132 /**
00133  * Called before Controller::redirect().
00134  *
00135  * @param object $controller Controller with components to beforeRedirect
00136  * @return void
00137  * @access public
00138  */
00139     function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
00140         $response = array();
00141 
00142         foreach ($this->_primary as $name) {
00143             $component =& $this->_loaded[$name];
00144 
00145             if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
00146                 $resp = $component->beforeRedirect($controller, $url, $status, $exit);
00147                 if ($resp === false) {
00148                     return false;
00149                 }
00150                 $response[] = $resp;
00151             }
00152         }
00153         return $response;
00154     }
00155 /**
00156  * Called after Controller::render() and before the output is printed to the browser.
00157  *
00158  * @param object $controller Controller with components to shutdown
00159  * @return void
00160  * @access public
00161  */
00162     function shutdown(&$controller) {
00163         foreach ($this->_primary as $name) {
00164             $component =& $this->_loaded[$name];
00165             if (method_exists($component,'shutdown') && $component->enabled === true) {
00166                 $component->shutdown($controller);
00167             }
00168         }
00169     }
00170 /**
00171  * Loads components used by this component.
00172  *
00173  * @param object $object Object with a Components array
00174  * @param object $parent the parent of the current object
00175  * @return void
00176  * @access protected
00177  */
00178     function _loadComponents(&$object, $parent = null) {
00179         $base = $this->__controllerVars['base'];
00180         $normal = Set::normalize($object->components);
00181         if ($parent == null) {
00182             $normal = Set::merge(array('Session' => null), $normal);
00183         }
00184         foreach ((array)$normal as $component => $config) {
00185             $plugin = null;
00186 
00187             if (isset($this->__controllerVars['plugin'])) {
00188                 $plugin = $this->__controllerVars['plugin'] . '.';
00189             }
00190 
00191             if (strpos($component, '.') !== false) {
00192                 list($plugin, $component) = explode('.', $component);
00193                 $plugin = $plugin . '.';
00194             }
00195             $componentCn = $component . 'Component';
00196 
00197             if (!class_exists($componentCn)) {
00198                 if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
00199                     if (!App::import('Component', $component)) {
00200                         $this->cakeError('missingComponentFile', array(array(
00201                             'className' => $this->__controllerVars['name'],
00202                             'component' => $component,
00203                             'file' => Inflector::underscore($component) . '.php',
00204                             'base' => $base,
00205                             'code' => 500
00206                         )));
00207                         return false;
00208                     }
00209                 }
00210 
00211                 if (!class_exists($componentCn)) {
00212                     $this->cakeError('missingComponentClass', array(array(
00213                         'className' => $this->__controllerVars['name'],
00214                         'component' => $component,
00215                         'file' => Inflector::underscore($component) . '.php',
00216                         'base' => $base,
00217                         'code' => 500
00218                     )));
00219                     return false;
00220                 }
00221             }
00222 
00223             if ($parent === null) {
00224                 $this->_primary[] = $component;
00225             }
00226 
00227             if (isset($this->_loaded[$component])) {
00228                 $object->{$component} =& $this->_loaded[$component];
00229 
00230                 if (!empty($config) && isset($this->__settings[$component])) {
00231                     $this->__settings[$component] = array_merge($this->__settings[$component], $config);
00232                 } elseif (!empty($config)) {
00233                     $this->__settings[$component] = $config;
00234                 }
00235             } else {
00236                 if ($componentCn === 'SessionComponent') {
00237                     $object->{$component} =& new $componentCn($base);
00238                 } else {
00239                     $object->{$component} =& new $componentCn();
00240                 }
00241                 $object->{$component}->enabled = true;
00242                 $this->_loaded[$component] =& $object->{$component};
00243                 if (!empty($config)) {
00244                     $this->__settings[$component] = $config;
00245                 }
00246             }
00247 
00248             if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
00249                 $this->_loadComponents($object->{$component}, $component);
00250             }
00251         }
00252     }
00253 }
00254 
00255 ?>

Generated on Sun Nov 22 00:30:52 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7