Base for a static organization website

Component.php 5.3KB

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