2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
2010-07-04 18:58:57 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-11-06 06:46:59 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, 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.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, 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
|
2010-07-04 17:14:04 +00:00
|
|
|
* @since CakePHP(tm) v 1.2
|
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
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
App::import('Controller', 'ComponentCollection', false);
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-04 18:58:57 +00:00
|
|
|
* Base class for an individual Component. Components provide resuable bits of
|
|
|
|
* controller logic that can be composed into a controller. Components also
|
|
|
|
* provide request life-cycle callbacks for injecting logic at specific points.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2008-10-30 17:30:26 +00:00
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.libs.controller
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/993/Components
|
2010-07-04 18:58:57 +00:00
|
|
|
* @see Controller::$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
|
|
|
/**
|
2010-07-04 18:58:57 +00:00
|
|
|
* Component collection class used to lazy load components.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-07-04 18:58:57 +00:00
|
|
|
* @var ComponentCollection
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
protected $_Collection;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-04 18:58:57 +00:00
|
|
|
* Settings for this Component
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-07-04 18:58:57 +00:00
|
|
|
* @var array
|
2008-10-15 13:07:46 +00:00
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
public $settings = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-10-15 13:07:46 +00:00
|
|
|
/**
|
2010-07-04 18:58:57 +00:00
|
|
|
* Other Components this component uses.
|
2008-10-15 13:07:46 +00:00
|
|
|
*
|
2010-07-04 18:58:57 +00:00
|
|
|
* @var array
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
public $components = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-04 02:39:05 +00:00
|
|
|
/**
|
2010-07-04 18:58:57 +00:00
|
|
|
* A component lookup table used to lazy load component objects.
|
2008-06-04 02:39:05 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
protected $_componentMap = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-07-04 18:58:57 +00:00
|
|
|
* Constructor
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-07-04 18:58:57 +00:00
|
|
|
* @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
|
|
|
|
* @param array $settings Array of configuration settings.
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
public function __construct(ComponentCollection $collection, $settings = array()) {
|
|
|
|
$this->_Collection = $collection;
|
|
|
|
$this->settings = $settings;
|
2010-07-04 21:09:44 +00:00
|
|
|
$this->_set($settings);
|
2010-07-04 18:58:57 +00:00
|
|
|
if (!empty($this->components)) {
|
|
|
|
$this->_componentMap = ComponentCollection::normalizeObjectArray($this->components);
|
2008-10-15 13:07:46 +00:00
|
|
|
}
|
2010-07-04 18:58:57 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-04 18:58:57 +00:00
|
|
|
/**
|
|
|
|
* Magic method for lazy loading $components.
|
|
|
|
*
|
|
|
|
* @param sting $name Name of component to get.
|
|
|
|
* @return mixed A Component object or null.
|
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
if (isset($this->_componentMap[$name]) && !isset($this->{$name})) {
|
|
|
|
$this->{$name} = $this->_Collection->load(
|
|
|
|
$this->_componentMap[$name]['class'], $this->_componentMap[$name]['settings'], false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (isset($this->{$name})) {
|
|
|
|
return $this->{$name};
|
|
|
|
}
|
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
|
2010-03-28 15:34:58 +00:00
|
|
|
* @link http://book.cakephp.org/view/998/MVC-Class-Access-Within-Components
|
2008-05-31 03:54:22 +00:00
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
public function initialize(&$controller) { }
|
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
|
2010-03-28 15:34:58 +00:00
|
|
|
* @link http://book.cakephp.org/view/998/MVC-Class-Access-Within-Components
|
2008-05-31 03:54:22 +00:00
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
public function 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
|
|
|
|
*/
|
2010-07-04 18:58:57 +00:00
|
|
|
public function beforeRender(&$controller) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called after Controller::render() and before the output is printed to the browser.
|
|
|
|
*
|
|
|
|
* @param object $controller Controller with components to shutdown
|
|
|
|
* @return void
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function shutdown(&$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
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function beforeRedirect(&$controller, $url, $status = null, $exit = true) {
|
2008-05-31 03:54:22 +00:00
|
|
|
$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
|
|
|
|
2010-02-28 02:47:32 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|