Component.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ComponentCollection', 'Controller');
  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. * Component collection class used to lazy load components.
  44. *
  45. * @var ComponentCollection
  46. */
  47. protected $_Collection;
  48. /**
  49. * Settings for this Component
  50. *
  51. * @var array
  52. */
  53. public $settings = array();
  54. /**
  55. * Other Components this component uses.
  56. *
  57. * @var array
  58. */
  59. public $components = array();
  60. /**
  61. * A component lookup table used to lazy load component objects.
  62. *
  63. * @var array
  64. */
  65. protected $_componentMap = array();
  66. /**
  67. * Constructor
  68. *
  69. * @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
  70. * @param array $settings Array of configuration settings.
  71. */
  72. public function __construct(ComponentCollection $collection, $settings = array()) {
  73. $this->_Collection = $collection;
  74. $this->settings = $settings;
  75. $this->_set($settings);
  76. if (!empty($this->components)) {
  77. $this->_componentMap = ComponentCollection::normalizeObjectArray($this->components);
  78. }
  79. }
  80. /**
  81. * Magic method for lazy loading $components.
  82. *
  83. * @param string $name Name of component to get.
  84. * @return mixed A Component object or null.
  85. */
  86. public function __get($name) {
  87. if (isset($this->_componentMap[$name]) && !isset($this->{$name})) {
  88. $settings = array_merge((array)$this->_componentMap[$name]['settings'], array('enabled' => false));
  89. $this->{$name} = $this->_Collection->load($this->_componentMap[$name]['class'], $settings);
  90. }
  91. if (isset($this->{$name})) {
  92. return $this->{$name};
  93. }
  94. }
  95. /**
  96. * Called before the Controller::beforeFilter().
  97. *
  98. * @param Controller $controller Controller with components to initialize
  99. * @return void
  100. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::initialize
  101. */
  102. public function initialize(Controller $controller) {
  103. }
  104. /**
  105. * Called after the Controller::beforeFilter() and before the controller action
  106. *
  107. * @param Controller $controller Controller with components to startup
  108. * @return void
  109. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::startup
  110. */
  111. public function startup(Controller $controller) {
  112. }
  113. /**
  114. * Called before the Controller::beforeRender(), and before
  115. * the view class is loaded, and before Controller::render()
  116. *
  117. * @param Controller $controller Controller with components to beforeRender
  118. * @return void
  119. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRender
  120. */
  121. public function beforeRender(Controller $controller) {
  122. }
  123. /**
  124. * Called after Controller::render() and before the output is printed to the browser.
  125. *
  126. * @param Controller $controller Controller with components to shutdown
  127. * @return void
  128. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::shutdown
  129. */
  130. public function shutdown(Controller $controller) {
  131. }
  132. /**
  133. * Called before Controller::redirect(). Allows you to replace the url that will
  134. * be redirected to with a new url. The return of this method can either be an array or a string.
  135. *
  136. * If the return is an array and contains a 'url' key. You may also supply the following:
  137. *
  138. * - `status` The status code for the redirect
  139. * - `exit` Whether or not the redirect should exit.
  140. *
  141. * If your response is a string or an array that does not contain a 'url' key it will
  142. * be used as the new url to redirect to.
  143. *
  144. * @param Controller $controller Controller with components to beforeRedirect
  145. * @param string|array $url Either the string or url array that is being redirected to.
  146. * @param integer $status The status code of the redirect
  147. * @param boolean $exit Will the script exit.
  148. * @return array|void Either an array or null.
  149. * @link http://book.cakephp.org/2.0/en/controllers/components.html#Component::beforeRedirect
  150. */
  151. public function beforeRedirect(Controller $controller, $url, $status = null, $exit = true) {
  152. }
  153. }