Base for a static organization website

ComponentCollection.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Components collection is used as a registry for loaded components and handles loading
  4. * and constructing component class objects.
  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 2.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. App::uses('ObjectCollection', 'Utility');
  20. App::uses('Component', 'Controller');
  21. App::uses('CakeEventListener', 'Event');
  22. /**
  23. * Components collection is used as a registry for loaded components and handles loading
  24. * and constructing component class objects.
  25. *
  26. * @package Cake.Controller
  27. */
  28. class ComponentCollection extends ObjectCollection implements CakeEventListener {
  29. /**
  30. * The controller that this collection was initialized with.
  31. *
  32. * @var Controller
  33. */
  34. protected $_Controller = null;
  35. /**
  36. * Initializes all the Components for a controller.
  37. * Attaches a reference of each component to the Controller.
  38. *
  39. * @param Controller $Controller Controller to initialize components for.
  40. * @return void
  41. */
  42. public function init(Controller $Controller) {
  43. if (empty($Controller->components)) {
  44. return;
  45. }
  46. $this->_Controller = $Controller;
  47. $components = ComponentCollection::normalizeObjectArray($Controller->components);
  48. foreach ($components as $name => $properties) {
  49. $Controller->{$name} = $this->load($properties['class'], $properties['settings']);
  50. }
  51. }
  52. /**
  53. * Set the controller associated with the collection.
  54. *
  55. * @param Controller $Controller Controller to set
  56. * @return void
  57. */
  58. public function setController(Controller $Controller) {
  59. $this->_Controller = $Controller;
  60. }
  61. /**
  62. * Get the controller associated with the collection.
  63. *
  64. * @return Controller Controller instance
  65. */
  66. public function getController() {
  67. return $this->_Controller;
  68. }
  69. /**
  70. * Loads/constructs a component. Will return the instance in the registry if it already exists.
  71. * You can use `$settings['enabled'] = false` to disable callbacks on a component when loading it.
  72. * Callbacks default to on. Disabled component methods work as normal, only callbacks are disabled.
  73. *
  74. * You can alias your component as an existing component by setting the 'className' key, i.e.,
  75. * ```
  76. * public $components = array(
  77. * 'Email' => array(
  78. * 'className' => 'AliasedEmail'
  79. * );
  80. * );
  81. * ```
  82. * All calls to the `Email` component would use `AliasedEmail` instead.
  83. *
  84. * @param string $component Component name to load
  85. * @param array $settings Settings for the component.
  86. * @return Component A component object, Either the existing loaded component or a new one.
  87. * @throws MissingComponentException when the component could not be found
  88. */
  89. public function load($component, $settings = array()) {
  90. if (isset($settings['className'])) {
  91. $alias = $component;
  92. $component = $settings['className'];
  93. }
  94. list($plugin, $name) = pluginSplit($component, true);
  95. if (!isset($alias)) {
  96. $alias = $name;
  97. }
  98. if (isset($this->_loaded[$alias])) {
  99. return $this->_loaded[$alias];
  100. }
  101. $componentClass = $name . 'Component';
  102. App::uses($componentClass, $plugin . 'Controller/Component');
  103. if (!class_exists($componentClass)) {
  104. throw new MissingComponentException(array(
  105. 'class' => $componentClass,
  106. 'plugin' => substr($plugin, 0, -1)
  107. ));
  108. }
  109. $this->_loaded[$alias] = new $componentClass($this, $settings);
  110. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  111. if ($enable) {
  112. $this->enable($alias);
  113. }
  114. return $this->_loaded[$alias];
  115. }
  116. /**
  117. * Returns the implemented events that will get routed to the trigger function
  118. * in order to dispatch them separately on each component
  119. *
  120. * @return array
  121. */
  122. public function implementedEvents() {
  123. return array(
  124. 'Controller.initialize' => array('callable' => 'trigger'),
  125. 'Controller.startup' => array('callable' => 'trigger'),
  126. 'Controller.beforeRender' => array('callable' => 'trigger'),
  127. 'Controller.beforeRedirect' => array('callable' => 'trigger'),
  128. 'Controller.shutdown' => array('callable' => 'trigger'),
  129. );
  130. }
  131. }