Component.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Controller;
  16. use Cake\Core\InstanceConfigTrait;
  17. use Cake\Event\EventListener;
  18. use Cake\Log\LogTrait;
  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(Event $event)`
  30. * Called before the controller's beforeFilter method.
  31. * - `startup(Event $event)`
  32. * Called after the controller's beforeFilter method, and before the
  33. * controller action is called.
  34. * - `beforeRender(Event $event)`
  35. * Called before the Controller beforeRender, and before the view class is loaded.
  36. * - `shutdown(Event $event)`
  37. * Called after the action is complete and the view has been rendered but
  38. * before Controller::afterFilter().
  39. * - `beforeRedirect(Event $event $url, Response $response)`
  40. * Called before a redirect is done. Allows you to change the URL that will
  41. * be redirected to by returning a Response instance with new URL set using
  42. * Response::location(). Redirection can be prevented by stopping the event
  43. * propagation.
  44. *
  45. * While the controller is not an explicit argument for the callback methods it
  46. * is the subject of each event and can be fetched using Event::subject().
  47. *
  48. * @link http://book.cakephp.org/2.0/en/controllers/components.html
  49. * @see Controller::$components
  50. */
  51. class Component implements EventListener {
  52. use InstanceConfigTrait;
  53. use LogTrait;
  54. /**
  55. * Component registry class used to lazy load components.
  56. *
  57. * @var ComponentRegistry
  58. */
  59. protected $_registry;
  60. /**
  61. * Other Components this component uses.
  62. *
  63. * @var array
  64. */
  65. public $components = array();
  66. /**
  67. * Default config
  68. *
  69. * These are merged with user-provided config when the component is used.
  70. *
  71. * @var array
  72. */
  73. protected $_defaultConfig = [];
  74. /**
  75. * A component lookup table used to lazy load component objects.
  76. *
  77. * @var array
  78. */
  79. protected $_componentMap = array();
  80. /**
  81. * Constructor
  82. *
  83. * @param ComponentRegistry $registry A ComponentRegistry this component can use to lazy load its components
  84. * @param array $config Array of configuration settings.
  85. */
  86. public function __construct(ComponentRegistry $registry, array $config = []) {
  87. $this->_registry = $registry;
  88. $this->config($config);
  89. if (!empty($this->components)) {
  90. $this->_componentMap = $registry->normalizeArray($this->components);
  91. }
  92. }
  93. /**
  94. * Magic method for lazy loading $components.
  95. *
  96. * @param string $name Name of component to get.
  97. * @return mixed A Component object or null.
  98. */
  99. public function __get($name) {
  100. if (isset($this->_componentMap[$name]) && !isset($this->{$name})) {
  101. $config = array('enabled' => false) + (array)$this->_componentMap[$name]['config'];
  102. $this->{$name} = $this->_registry->load($this->_componentMap[$name]['class'], $config);
  103. }
  104. if (isset($this->{$name})) {
  105. return $this->{$name};
  106. }
  107. }
  108. /**
  109. * Get the Controller callbacks this Component is interested in.
  110. *
  111. * Uses Conventions to map controller events to standard component
  112. * callback method names. By defining one of the callback methods a
  113. * component is assumed to be interested in the related event.
  114. *
  115. * Override this method if you need to add non-conventional event listeners.
  116. * Or if you want components to listen to non-standard events.
  117. *
  118. * @return array
  119. */
  120. public function implementedEvents() {
  121. $eventMap = [
  122. 'Controller.initialize' => 'initialize',
  123. 'Controller.startup' => 'startup',
  124. 'Controller.beforeRender' => 'beforeRender',
  125. 'Controller.beforeRedirect' => 'beforeRedirect',
  126. 'Controller.shutdown' => 'shutdown',
  127. ];
  128. $events = [];
  129. foreach ($eventMap as $event => $method) {
  130. if (method_exists($this, $method)) {
  131. $events[$event] = $method;
  132. }
  133. }
  134. return $events;
  135. }
  136. }