2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-11-06 06:00:11 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller
|
|
|
|
* @since CakePHP(tm) v TBD
|
2009-11-06 06:51:51 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-05-31 03:54:22 +00:00
|
|
|
* Handler for Controller::$components
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller
|
2008-10-30 19:39:18 +00:00
|
|
|
* @link http://book.cakephp.org/view/62/Components
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class Component extends Object {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:39:18 +00:00
|
|
|
* Contains various controller variable information (plugin, name, base).
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-05-31 03:54:22 +00:00
|
|
|
* @var object
|
|
|
|
* @access private
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-05-31 03:54:22 +00:00
|
|
|
var $__controllerVars = array('plugin' => null, 'name' => null, 'base' => null);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:39:18 +00:00
|
|
|
* List of loaded components.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var object
|
2008-10-15 13:07:46 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
var $_loaded = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-15 13:07:46 +00:00
|
|
|
/**
|
|
|
|
* List of components attached directly to the controller, which callbacks
|
|
|
|
* should be executed on.
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
* @access protected
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-10-15 13:07:46 +00:00
|
|
|
var $_primary = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-04 02:39:05 +00:00
|
|
|
/**
|
|
|
|
* Settings for loaded components.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
**/
|
|
|
|
var $__settings = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:39:18 +00:00
|
|
|
* Used to initialize the components for current controller.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-05-31 03:54:22 +00:00
|
|
|
* @param object $controller Controller with components to load
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function init(&$controller) {
|
2008-10-15 13:07:46 +00:00
|
|
|
if (!is_array($controller->components)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->__controllerVars = array(
|
|
|
|
'plugin' => $controller->plugin, 'name' => $controller->name,
|
|
|
|
'base' => $controller->base
|
|
|
|
);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-15 13:07:46 +00:00
|
|
|
$this->_loadComponents($controller);
|
2008-05-31 03:54:22 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
/**
|
2008-10-30 19:39:18 +00:00
|
|
|
* Called before the Controller::beforeFilter().
|
2008-05-31 03:54:22 +00:00
|
|
|
*
|
|
|
|
* @param object $controller Controller with components to initialize
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-31 03:54:22 +00:00
|
|
|
* @access public
|
2008-10-30 19:39:18 +00:00
|
|
|
* @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
|
2008-05-31 03:54:22 +00:00
|
|
|
*/
|
|
|
|
function initialize(&$controller) {
|
2008-10-15 13:07:46 +00:00
|
|
|
foreach (array_keys($this->_loaded) as $name) {
|
|
|
|
$component =& $this->_loaded[$name];
|
|
|
|
|
2008-05-31 09:24:42 +00:00
|
|
|
if (method_exists($component,'initialize') && $component->enabled === true) {
|
2008-06-04 02:39:05 +00:00
|
|
|
$settings = array();
|
|
|
|
if (isset($this->__settings[$name])) {
|
|
|
|
$settings = $this->__settings[$name];
|
|
|
|
}
|
|
|
|
$component->initialize($controller, $settings);
|
2008-05-31 03:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
/**
|
|
|
|
* Called after the Controller::beforeFilter() and before the controller action
|
|
|
|
*
|
|
|
|
* @param object $controller Controller with components to startup
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-31 03:54:22 +00:00
|
|
|
* @access public
|
2008-10-30 19:39:18 +00:00
|
|
|
* @link http://book.cakephp.org/view/65/MVC-Class-Access-Within-Components
|
2008-05-31 03:54:22 +00:00
|
|
|
*/
|
|
|
|
function startup(&$controller) {
|
2008-10-15 13:07:46 +00:00
|
|
|
foreach ($this->_primary as $name) {
|
|
|
|
$component =& $this->_loaded[$name];
|
|
|
|
if ($component->enabled === true && method_exists($component, 'startup')) {
|
2008-05-31 03:54:22 +00:00
|
|
|
$component->startup($controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
/**
|
2008-10-15 13:07:46 +00:00
|
|
|
* Called after the Controller::beforeRender(), after the view class is loaded, and before the
|
|
|
|
* Controller::render()
|
2008-05-31 03:54:22 +00:00
|
|
|
*
|
|
|
|
* @param object $controller Controller with components to beforeRender
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-31 03:54:22 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function beforeRender(&$controller) {
|
2008-10-15 13:07:46 +00:00
|
|
|
foreach ($this->_primary as $name) {
|
|
|
|
$component =& $this->_loaded[$name];
|
|
|
|
if ($component->enabled === true && method_exists($component,'beforeRender')) {
|
2008-05-31 03:54:22 +00:00
|
|
|
$component->beforeRender($controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
/**
|
2008-10-30 19:39:18 +00:00
|
|
|
* Called before Controller::redirect().
|
2008-05-31 03:54:22 +00:00
|
|
|
*
|
|
|
|
* @param object $controller Controller with components to beforeRedirect
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-31 03:54:22 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
|
|
|
|
$response = array();
|
2008-10-15 13:07:46 +00:00
|
|
|
|
|
|
|
foreach ($this->_primary as $name) {
|
|
|
|
$component =& $this->_loaded[$name];
|
|
|
|
|
|
|
|
if ($component->enabled === true && method_exists($component, 'beforeRedirect')) {
|
2008-05-31 03:54:22 +00:00
|
|
|
$resp = $component->beforeRedirect($controller, $url, $status, $exit);
|
|
|
|
if ($resp === false) {
|
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-05-31 03:54:22 +00:00
|
|
|
$response[] = $resp;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2008-05-31 03:54:22 +00:00
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
/**
|
2008-10-30 19:39:18 +00:00
|
|
|
* Called after Controller::render() and before the output is printed to the browser.
|
2008-05-31 03:54:22 +00:00
|
|
|
*
|
|
|
|
* @param object $controller Controller with components to shutdown
|
2008-09-25 16:49:56 +00:00
|
|
|
* @return void
|
2008-05-31 03:54:22 +00:00
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function shutdown(&$controller) {
|
2008-10-15 13:07:46 +00:00
|
|
|
foreach ($this->_primary as $name) {
|
|
|
|
$component =& $this->_loaded[$name];
|
2008-05-31 09:24:42 +00:00
|
|
|
if (method_exists($component,'shutdown') && $component->enabled === true) {
|
2008-05-31 03:54:22 +00:00
|
|
|
$component->shutdown($controller);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2008-10-30 19:39:18 +00:00
|
|
|
* Loads components used by this component.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-05-31 03:54:22 +00:00
|
|
|
* @param object $object Object with a Components array
|
|
|
|
* @param object $parent the parent of the current object
|
|
|
|
* @return void
|
2008-05-30 11:40:08 +00:00
|
|
|
* @access protected
|
|
|
|
*/
|
2008-05-31 03:54:22 +00:00
|
|
|
function _loadComponents(&$object, $parent = null) {
|
|
|
|
$base = $this->__controllerVars['base'];
|
2009-02-15 20:13:14 +00:00
|
|
|
$normal = Set::normalize($object->components);
|
|
|
|
if ($parent == null) {
|
|
|
|
$normal = Set::merge(array('Session' => null), $normal);
|
|
|
|
}
|
|
|
|
foreach ((array)$normal as $component => $config) {
|
|
|
|
$plugin = null;
|
2008-06-06 16:18:14 +00:00
|
|
|
|
2009-02-15 20:13:14 +00:00
|
|
|
if (isset($this->__controllerVars['plugin'])) {
|
|
|
|
$plugin = $this->__controllerVars['plugin'] . '.';
|
2009-02-13 01:11:00 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-02-15 20:13:14 +00:00
|
|
|
if (strpos($component, '.') !== false) {
|
|
|
|
list($plugin, $component) = explode('.', $component);
|
|
|
|
$plugin = $plugin . '.';
|
|
|
|
}
|
|
|
|
$componentCn = $component . 'Component';
|
2008-05-31 03:54:22 +00:00
|
|
|
|
2009-02-15 20:13:14 +00:00
|
|
|
if (!class_exists($componentCn)) {
|
|
|
|
if (is_null($plugin) || !App::import('Component', $plugin . $component)) {
|
|
|
|
if (!App::import('Component', $component)) {
|
|
|
|
$this->cakeError('missingComponentFile', array(array(
|
2008-05-31 03:54:22 +00:00
|
|
|
'className' => $this->__controllerVars['name'],
|
2008-05-30 11:40:08 +00:00
|
|
|
'component' => $component,
|
|
|
|
'file' => Inflector::underscore($component) . '.php',
|
2008-05-31 03:54:22 +00:00
|
|
|
'base' => $base,
|
2008-05-30 11:40:08 +00:00
|
|
|
'code' => 500
|
|
|
|
)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-06-06 16:18:14 +00:00
|
|
|
|
2009-02-15 20:13:14 +00:00
|
|
|
if (!class_exists($componentCn)) {
|
|
|
|
$this->cakeError('missingComponentClass', array(array(
|
|
|
|
'className' => $this->__controllerVars['name'],
|
|
|
|
'component' => $component,
|
|
|
|
'file' => Inflector::underscore($component) . '.php',
|
|
|
|
'base' => $base,
|
|
|
|
'code' => 500
|
|
|
|
)));
|
|
|
|
return false;
|
2008-10-15 17:37:19 +00:00
|
|
|
}
|
2009-02-15 20:13:14 +00:00
|
|
|
}
|
2008-10-15 17:37:19 +00:00
|
|
|
|
2009-02-15 20:13:14 +00:00
|
|
|
if ($parent === null) {
|
|
|
|
$this->_primary[] = $component;
|
|
|
|
}
|
2008-06-06 16:18:14 +00:00
|
|
|
|
2009-02-15 20:13:14 +00:00
|
|
|
if (isset($this->_loaded[$component])) {
|
|
|
|
$object->{$component} =& $this->_loaded[$component];
|
|
|
|
|
|
|
|
if (!empty($config) && isset($this->__settings[$component])) {
|
|
|
|
$this->__settings[$component] = array_merge($this->__settings[$component], $config);
|
|
|
|
} elseif (!empty($config)) {
|
|
|
|
$this->__settings[$component] = $config;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ($componentCn === 'SessionComponent') {
|
|
|
|
$object->{$component} =& new $componentCn($base);
|
2008-05-31 03:54:22 +00:00
|
|
|
} else {
|
2009-02-15 20:13:14 +00:00
|
|
|
$object->{$component} =& new $componentCn();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-02-15 20:13:14 +00:00
|
|
|
$object->{$component}->enabled = true;
|
|
|
|
$this->_loaded[$component] =& $object->{$component};
|
|
|
|
if (!empty($config)) {
|
|
|
|
$this->__settings[$component] = $config;
|
2008-05-31 03:54:22 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-02-15 20:13:14 +00:00
|
|
|
|
|
|
|
if (isset($object->{$component}->components) && is_array($object->{$component}->components) && (!isset($object->{$component}->{$parent}))) {
|
|
|
|
$this->_loadComponents($object->{$component}, $component);
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-20 20:17:23 +00:00
|
|
|
|
2008-05-31 03:54:22 +00:00
|
|
|
?>
|