1: <?php
2: /**
3: *
4: * PHP 5
5: *
6: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
7: * Copyright 2005-2012, 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-2012, 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/2.0/en/controllers/components.html
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 $controller) {
110: }
111:
112: /**
113: * Called after the Controller::beforeFilter() and before the controller action
114: *
115: * @param Controller $controller Controller with components to startup
116: * @return void
117: * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::startup
118: */
119: public function startup(Controller $controller) {
120: }
121:
122: /**
123: * Called before the Controller::beforeRender(), and before
124: * the view class is loaded, and before Controller::render()
125: *
126: * @param Controller $controller Controller with components to beforeRender
127: * @return void
128: * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRender
129: */
130: public function beforeRender(Controller $controller) {
131: }
132:
133: /**
134: * Called after Controller::render() and before the output is printed to the browser.
135: *
136: * @param Controller $controller Controller with components to shutdown
137: * @return void
138: * @link @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::shutdown
139: */
140: public function shutdown(Controller $controller) {
141: }
142:
143: /**
144: * Called before Controller::redirect(). Allows you to replace the url that will
145: * be redirected to with a new url. The return of this method can either be an array or a string.
146: *
147: * If the return is an array and contains a 'url' key. You may also supply the following:
148: *
149: * - `status` The status code for the redirect
150: * - `exit` Whether or not the redirect should exit.
151: *
152: * If your response is a string or an array that does not contain a 'url' key it will
153: * be used as the new url to redirect to.
154: *
155: * @param Controller $controller Controller with components to beforeRedirect
156: * @param string|array $url Either the string or url array that is being redirected to.
157: * @param integer $status The status code of the redirect
158: * @param boolean $exit Will the script exit.
159: * @return array|null Either an array or null.
160: * @link @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRedirect
161: */
162: public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
163: }
164:
165: }
166: