1: <?php
2: /**
3: *
4: *
5: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
6: * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7: *
8: * Licensed under The MIT License
9: * For full copyright and license information, please see the LICENSE.txt
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @package Cake.Controller
15: * @since CakePHP(tm) v 1.2
16: * @license http://www.opensource.org/licenses/mit-license.php MIT License
17: */
18:
19: App::uses('ComponentCollection', 'Controller');
20:
21: /**
22: * Base class for an individual Component. Components provide reusable bits of
23: * controller logic that can be composed into a controller. Components also
24: * provide request life-cycle callbacks for injecting logic at specific points.
25: *
26: * ## Life cycle callbacks
27: *
28: * Components can provide several callbacks that are fired at various stages of the request
29: * cycle. The available callbacks are:
30: *
31: * - `initialize()` - Fired before the controller's beforeFilter method.
32: * - `startup()` - Fired after the controller's beforeFilter method.
33: * - `beforeRender()` - Fired before the view + layout are rendered.
34: * - `shutdown()` - Fired after the action is complete and the view has been rendered
35: * but before Controller::afterFilter().
36: * - `beforeRedirect()` - Fired before a redirect() is done.
37: *
38: * @package Cake.Controller
39: * @link http://book.cakephp.org/2.0/en/controllers/components.html
40: * @see Controller::$components
41: */
42: class Component extends Object {
43:
44: /**
45: * Component collection class used to lazy load components.
46: *
47: * @var ComponentCollection
48: */
49: protected $_Collection;
50:
51: /**
52: * Settings for this Component
53: *
54: * @var array
55: */
56: public $settings = array();
57:
58: /**
59: * Other Components this component uses.
60: *
61: * @var array
62: */
63: public $components = array();
64:
65: /**
66: * A component lookup table used to lazy load component objects.
67: *
68: * @var array
69: */
70: protected $_componentMap = array();
71:
72: /**
73: * Constructor
74: *
75: * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
76: * @param array $settings Array of configuration settings.
77: */
78: public function __construct(ComponentCollection $collection, $settings = array()) {
79: $this->_Collection = $collection;
80: $this->settings = $settings;
81: $this->_set($settings);
82: if (!empty($this->components)) {
83: $this->_componentMap = ComponentCollection::normalizeObjectArray($this->components);
84: }
85: }
86:
87: /**
88: * Magic method for lazy loading $components.
89: *
90: * @param string $name Name of component to get.
91: * @return mixed A Component object or null.
92: */
93: public function __get($name) {
94: if (isset($this->_componentMap[$name]) && !isset($this->{$name})) {
95: $settings = array_merge((array)$this->_componentMap[$name]['settings'], array('enabled' => false));
96: $this->{$name} = $this->_Collection->load($this->_componentMap[$name]['class'], $settings);
97: }
98: if (isset($this->{$name})) {
99: return $this->{$name};
100: }
101: }
102:
103: /**
104: * Called before the Controller::beforeFilter().
105: *
106: * @param Controller $controller Controller with components to initialize
107: * @return void
108: * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::initialize
109: */
110: public function initialize(Controller $controller) {
111: }
112:
113: /**
114: * Called after the Controller::beforeFilter() and before the controller action
115: *
116: * @param Controller $controller Controller with components to startup
117: * @return void
118: * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::startup
119: */
120: public function startup(Controller $controller) {
121: }
122:
123: /**
124: * Called before the Controller::beforeRender(), and before
125: * the view class is loaded, and before Controller::render()
126: *
127: * @param Controller $controller Controller with components to beforeRender
128: * @return void
129: * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRender
130: */
131: public function beforeRender(Controller $controller) {
132: }
133:
134: /**
135: * Called after Controller::render() and before the output is printed to the browser.
136: *
137: * @param Controller $controller Controller with components to shutdown
138: * @return void
139: * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::shutdown
140: */
141: public function shutdown(Controller $controller) {
142: }
143:
144: /**
145: * Called before Controller::redirect(). Allows you to replace the URL that will
146: * be redirected to with a new URL. The return of this method can either be an array or a string.
147: *
148: * If the return is an array and contains a 'url' key. You may also supply the following:
149: *
150: * - `status` The status code for the redirect
151: * - `exit` Whether or not the redirect should exit.
152: *
153: * If your response is a string or an array that does not contain a 'url' key it will
154: * be used as the new URL to redirect to.
155: *
156: * @param Controller $controller Controller with components to beforeRedirect
157: * @param string|array $url Either the string or URL array that is being redirected to.
158: * @param integer $status The status code of the redirect
159: * @param boolean $exit Will the script exit.
160: * @return array|void Either an array or null.
161: * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRedirect
162: */
163: public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
164: }
165:
166: }
167: