ComponentRegistry.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 CakePHP(tm) v 2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Controller;
  16. use Cake\Core\App;
  17. use Cake\Error;
  18. use Cake\Event\EventListener;
  19. use Cake\Event\EventManager;
  20. use Cake\Utility\ObjectRegistry;
  21. /**
  22. * ComponentRegistry is a registry for loaded components
  23. *
  24. * Handles loading, constructing and binding events for component class objects.
  25. */
  26. class ComponentRegistry extends ObjectRegistry {
  27. /**
  28. * The controller that this collection was initialized with.
  29. *
  30. * @var Controller
  31. */
  32. protected $_Controller = null;
  33. /**
  34. * The event manager to bind components to.
  35. *
  36. * @var Cake\Event\EventManager
  37. */
  38. protected $_eventManager = null;
  39. /**
  40. * Constructor.
  41. *
  42. * @param Cake\Controller\Controller $Controller
  43. */
  44. public function __construct(Controller $Controller = null) {
  45. if ($Controller) {
  46. $this->_Controller = $Controller;
  47. $this->_eventManager = $Controller->getEventManager();
  48. } else {
  49. $this->_eventManager = new EventManager();
  50. }
  51. }
  52. /**
  53. * Get the controller associated with the collection.
  54. *
  55. * @return Controller Controller instance
  56. */
  57. public function getController() {
  58. return $this->_Controller;
  59. }
  60. /**
  61. * Resolve a component classname.
  62. *
  63. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  64. *
  65. * @param string $class Partial classname to resolve.
  66. * @return string|false Either the correct classname or false.
  67. */
  68. protected function _resolveClassName($class) {
  69. return App::classname($class, 'Controller/Component', 'Component');
  70. }
  71. /**
  72. * Throws an exception when a component is missing.
  73. *
  74. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  75. *
  76. * @param string $class The classname that is missing.
  77. * @param string $plugin The plugin the component is missing in.
  78. * @throws Cake\Error\MissingComponentException
  79. */
  80. protected function _throwMissingClassError($class, $plugin) {
  81. throw new Error\MissingComponentException([
  82. 'class' => $class,
  83. 'plugin' => $plugin
  84. ]);
  85. }
  86. /**
  87. * Create the component instance.
  88. *
  89. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  90. * Enabled components will be registered with the event manager.
  91. *
  92. * @param string $class The classname that is missing.
  93. * @param array $settings An array of settings to use for the component.
  94. * @return Component The constructed component class.
  95. */
  96. protected function _create($class, $settings) {
  97. $instance = new $class($this, $settings);
  98. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  99. if ($enable) {
  100. $this->_eventManager->attach($instance);
  101. }
  102. return $instance;
  103. }
  104. /**
  105. * Destroys all objects in the registry.
  106. *
  107. * Removes all attached listeners and destroys all stored instances.
  108. *
  109. * @return void
  110. */
  111. public function reset() {
  112. foreach ($this->_loaded as $component) {
  113. $this->_eventManager->detach($component);
  114. }
  115. parent::reset();
  116. }
  117. }