component.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: component_8php-source.html 675 2008-12-26 00:27: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.
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: 675 $
00022  * @modifiedby      $LastChangedBy: gwoo $
00023  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00024  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00025  */
00026 /**
00027  * Component
00028  *
00029  * Used to create instances of applications components
00030  *
00031  * @package     cake
00032  * @subpackage  cake.cake.libs.controller
00033  */
00034 class Component extends Object {
00035 /**
00036  * Instance Controller
00037  *
00038  * @var object
00039  * @access private
00040  */
00041     var $__controller = null;
00042 /**
00043  * Constructor
00044  */
00045     function __construct() {
00046     }
00047 /**
00048  * Used to initialize the components for current controller
00049  *
00050  * @param object $controller
00051  * @access public
00052  */
00053     function init(&$controller) {
00054         $this->__controller =& $controller;
00055 
00056         if ($this->__controller->components !== false) {
00057             $loaded = array();
00058             $this->__controller->components = array_merge(array('Session'), $this->__controller->components);
00059             $loaded = $this->__loadComponents($loaded, $this->__controller->components);
00060 
00061             foreach (array_keys($loaded)as $component) {
00062                 $tempComponent =& $loaded[$component];
00063 
00064                 if (isset($tempComponent->components) && is_array($tempComponent->components)) {
00065                     foreach ($tempComponent->components as $subComponent) {
00066                         $this->__controller->{$component}->{$subComponent} =& $loaded[$subComponent];
00067                     }
00068                 }
00069                 if (is_callable(array($tempComponent, 'initialize'))) {
00070                     $tempComponent->initialize($controller);
00071                 }
00072             }
00073         }
00074     }
00075 
00076 /**
00077  * Enter description here...
00078  *
00079  * @param array $loaded
00080  * @param array $components
00081  * @return loaded components
00082  * @access private
00083  */
00084     function &__loadComponents(&$loaded, $components) {
00085         foreach ($components as $component) {
00086             $parts = preg_split('/\/|\./', $component);
00087 
00088             if (count($parts) === 1) {
00089                 $plugin = $this->__controller->plugin;
00090             } else {
00091                 $plugin = Inflector::underscore($parts['0']);
00092                 $component = $parts[count($parts) - 1];
00093             }
00094 
00095             $componentCn = $component . 'Component';
00096 
00097             if (in_array($component, array_keys($loaded)) !== true) {
00098 
00099                 if (!class_exists($componentCn)) {
00100 
00101                     if (is_null($plugin) || !loadPluginComponent($plugin, $component)) {
00102 
00103                         if (!loadComponent($component)) {
00104                             $this->cakeError('missingComponentFile', array(array(
00105                                                     'className' => $this->__controller->name,
00106                                                     'component' => $component,
00107                                                     'file' => Inflector::underscore($component) . '.php',
00108                                                     'base' => $this->__controller->base)));
00109                             exit();
00110                         }
00111                     }
00112 
00113                     if (!class_exists($componentCn)) {
00114                         $this->cakeError('missingComponentClass', array(array(
00115                                                 'className' => $this->__controller->name,
00116                                                 'component' => $component,
00117                                                 'file' => Inflector::underscore($component) . '.php',
00118                                                 'base' => $this->__controller->base)));
00119                         exit();
00120                     }
00121                 }
00122 
00123                 if ($componentCn == 'SessionComponent') {
00124                     $param = strip_plugin($this->__controller->base, $this->__controller->plugin) . '/';
00125                 } else {
00126                     $param = null;
00127                 }
00128                 $this->__controller->{$component} =& new $componentCn($param);
00129                 $loaded[$component] =& $this->__controller->{$component};
00130 
00131                 if (isset($this->__controller->{$component}->components) && is_array($this->__controller->{$component}->components)) {
00132                     $loaded =& $this->__loadComponents($loaded, $this->__controller->{$component}->components);
00133                 }
00134             }
00135         }
00136         return $loaded;
00137     }
00138 }
00139 ?>