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