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