Component.php 5.3 KB

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