component.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: component_8php-source.html 580 2008-07-01 14:45:49Z 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.
00009  *                              1785 E. Sahara Avenue, Suite 490-204
00010  *                              Las Vegas, Nevada 89104
00011  *
00012  * Licensed under The MIT License
00013  * Redistributions of files must retain the above copyright notice.
00014  *
00015  * @filesource
00016  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00017  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00018  * @package         cake
00019  * @subpackage      cake.cake.libs.controller
00020  * @since           CakePHP(tm) v TBD
00021  * @version         $Revision: 580 $
00022  * @modifiedby      $LastChangedBy: gwoo $
00023  * @lastmodified    $Date: 2008-07-01 09:45:49 -0500 (Tue, 01 Jul 2008) $
00024  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00025  */
00026 /**
00027  * Handler for Controller::$components
00028  *
00029  * @package     cake
00030  * @subpackage  cake.cake.libs.controller
00031  */
00032 class Component extends Object {
00033 /**
00034  * Some vars from controller (plugin, name, base)
00035  *
00036  * @var object
00037  * @access private
00038  */
00039     var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
00040 /**
00041  * All loaded components
00042  *
00043  * @var object
00044  * @access private
00045  */
00046     var $__loaded = array();
00047 /**
00048  * Settings for loaded components.
00049  *
00050  * @var array
00051  * @access private
00052  **/
00053     var $__settings = array();
00054 /**
00055  * Used to initialize the components for current controller
00056  *
00057  * @param object $controller Controller with components to load
00058  * @access public
00059  */
00060     function init(&$controller) {
00061         if ($controller->components !== false && is_array($controller->components)) {
00062             $this->__controllerVars = array(
00063                 'plugin' => $controller->plugin, 'name' => $controller->name, 'base' => $controller->base
00064             );
00065 
00066             if (!in_array('Session', $controller->components)) {
00067                 array_unshift($controller->components, 'Session');
00068             }
00069             $this->_loadComponents($controller);
00070         }
00071     }
00072 /**
00073  * Called before the Controller::beforeFilter()
00074  *
00075  * @param object $controller Controller with components to initialize
00076  * @access public
00077  */
00078     function initialize(&$controller) {
00079         foreach (array_keys($this->__loaded) as $name) {
00080             $component =& $this->__loaded[$name];
00081             if (method_exists($component,'initialize') && $component->enabled === true) {
00082                 $settings = array();
00083                 if (isset($this->__settings[$name])) {
00084                     $settings = $this->__settings[$name];
00085                 }
00086                 $component->initialize($controller, $settings);
00087             }
00088         }
00089     }
00090 /**
00091  * Called after the Controller::beforeFilter() and before the controller action
00092  *
00093  * @param object $controller Controller with components to startup
00094  * @access public
00095  */
00096     function startup(&$controller) {
00097         foreach (array_keys($this->__loaded) as $name) {
00098             $component =& $this->__loaded[$name];
00099             if (method_exists($component,'startup') && $component->enabled === true) {
00100                 $component->startup($controller);
00101             }
00102         }
00103     }
00104 /**
00105  * Called after the Controller::beforeRender(), after the view class is loaded, and before the Controller::render()
00106  *
00107  * @param object $controller Controller with components to beforeRender
00108  * @access public
00109  */
00110     function beforeRender(&$controller) {
00111         foreach (array_keys($this->__loaded) as $name) {
00112             $component =& $this->__loaded[$name];
00113             if (method_exists($component,'beforeRender') && $component->enabled === true) {
00114                 $component->beforeRender($controller);
00115             }
00116         }
00117     }
00118 /**
00119  * Called before Controller::redirect();
00120  *
00121  * @param object $controller Controller with components to beforeRedirect
00122  * @access public
00123  */
00124     function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
00125         $response = array();
00126         foreach (array_keys($this->__loaded) as $name) {
00127             $component =& $this->__loaded[$name];
00128             if (method_exists($component,'beforeRedirect') && $component->enabled === true) {
00129                 $resp = $component->beforeRedirect($controller, $url, $status, $exit);
00130                 if ($resp === false) {
00131                     return false;
00132                 }
00133                 $response[] = $resp;
00134             }
00135         }
00136         return $response;
00137     }
00138 /**
00139  * Called after Controller::render() and before the output is printed to the browser
00140  *
00141  * @param object $controller Controller with components to shutdown
00142  * @access public
00143  */
00144     function shutdown(&$controller) {
00145         foreach (array_keys($this->__loaded) as $name) {
00146             $component =& $this->__loaded[$name];
00147             if (method_exists($component,'shutdown') && $component->enabled === true) {
00148                 $component->shutdown($controller);
00149             }
00150         }
00151     }
00152 /**
00153  * Load components used by this component.
00154  *
00155  * @param object $object Object with a Components array
00156  * @param object $parent the parent of the current object
00157  * @return void
00158  * @access protected
00159  */
00160     function _loadComponents(&$object, $parent = null) {
00161         $components = $object->components;
00162         $base = $this->__controllerVars['base'];
00163 
00164         if (is_array($object->components)) {
00165             $normal = Set::normalize($object->components);
00166             foreach ($normal as $component => $config) {
00167                 $parts = preg_split('/\/|\./', $component);
00168 
00169                 if (count($parts) === 1) {
00170                     $plugin = $this->__controllerVars['plugin'] . '.';
00171                 } else {
00172                     $plugin = Inflector::underscore($parts['0']) . '.';
00173                     $component = array_pop($parts);
00174                 }
00175                 $componentCn = $component . 'Component';
00176 
00177                 if (!class_exists($componentCn)) {
00178                     if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
00179                         if (!App::import('Component', $component)) {
00180                             $this->cakeError('missingComponentFile', array(array(
00181                                 'className' => $this->__controllerVars['name'],
00182                                 'component' => $component,
00183                                 'file' => Inflector::underscore($component) . '.php',
00184                                 'base' => $base,
00185                                 'code' => 500
00186                             )));
00187                             return false;
00188                         }
00189                     }
00190 
00191                     if (!class_exists($componentCn)) {
00192                         $this->cakeError('missingComponentClass', array(array(
00193                             'className' => $this->__controllerVars['name'],
00194                             'component' => $component,
00195                             'file' => Inflector::underscore($component) . '.php',
00196                             'base' => $base,
00197                             'code' => 500
00198                         )));
00199                         return false;
00200                     }
00201                 }
00202 
00203                 if (isset($this->__loaded[$component])) {
00204                     $object->{$component} =& $this->__loaded[$component];
00205 
00206                     if (!empty($config) && isset($this->__settings[$component])) {
00207                         $this->__settings[$component] = array_merge($this->__settings[$component], $config);
00208                     } elseif (!empty($config)) {
00209                         $this->__settings[$component] = $config;
00210                     }
00211                 } else {
00212                     if ($componentCn == 'SessionComponent') {
00213                         $object->{$component} =& new $componentCn($base);
00214                     } else {
00215                         $object->{$component} =& new $componentCn();
00216                     }
00217                     $object->{$component}->enabled = true;
00218                     $this->__loaded[$component] =& $object->{$component};
00219                     if (!empty($config)) {
00220                         $this->__settings[$component] = $config;
00221                     }
00222                 }
00223 
00224                 if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
00225                     $this->_loadComponents($object->{$component}, $component);
00226                 }
00227             }
00228         }
00229     }
00230 }
00231 
00232 ?>