HelperRegistry.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\View;
  16. use Cake\Core\App;
  17. use Cake\Error;
  18. use Cake\Event\EventManager;
  19. use Cake\Utility\ObjectRegistry;
  20. use Cake\View\View;
  21. /**
  22. * HelperRegistry is used as a registry for loaded helpers and handles loading
  23. * and constructing helper class objects.
  24. */
  25. class HelperRegistry extends ObjectRegistry {
  26. /**
  27. * View object to use when making helpers.
  28. *
  29. * @var View
  30. */
  31. protected $_View;
  32. /**
  33. * EventManager instance.
  34. *
  35. * Helpers constructed by this object will be subscribed to this manager.
  36. *
  37. * @var Cake\Event\EventManager
  38. */
  39. protected $_eventManager;
  40. /**
  41. * Constructor
  42. *
  43. * @param View $view
  44. */
  45. public function __construct(View $view) {
  46. $this->_View = $view;
  47. $this->_eventManager = $view->getEventManager();
  48. }
  49. /**
  50. * Tries to lazy load a helper based on its name, if it cannot be found
  51. * in the application folder, then it tries looking under the current plugin
  52. * if any
  53. *
  54. * @param string $helper The helper name to be loaded
  55. * @return boolean whether the helper could be loaded or not
  56. * @throws MissingHelperException When a helper could not be found.
  57. * App helpers are searched, and then plugin helpers.
  58. */
  59. public function __isset($helper) {
  60. if (isset($this->_loaded[$helper])) {
  61. return true;
  62. }
  63. try {
  64. $this->load($helper);
  65. } catch (Error\MissingHelperException $exception) {
  66. if ($this->_View->plugin) {
  67. $this->load($this->_View->plugin . '.' . $helper);
  68. return true;
  69. }
  70. }
  71. if (!empty($exception)) {
  72. throw $exception;
  73. }
  74. return true;
  75. }
  76. /**
  77. * Provide public read access to the loaded objects
  78. *
  79. * @param string $name Name of property to read
  80. * @return mixed
  81. */
  82. public function __get($name) {
  83. if (isset($this->_loaded[$name])) {
  84. return $this->_loaded[$name];
  85. }
  86. if (isset($this->$name)) {
  87. return $this->_loaded[$name];
  88. }
  89. return null;
  90. }
  91. /**
  92. * Resolve a helper classname.
  93. *
  94. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  95. *
  96. * @param string $class Partial classname to resolve.
  97. * @return string|false Either the correct classname or false.
  98. */
  99. protected function _resolveClassName($class) {
  100. return App::classname($class, 'View/Helper', 'Helper');
  101. }
  102. /**
  103. * Throws an exception when a helper is missing.
  104. *
  105. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  106. *
  107. * @param string $class The classname that is missing.
  108. * @param string $plugin The plugin the helper is missing in.
  109. * @throws Cake\Error\MissingHelperException
  110. */
  111. protected function _throwMissingClassError($class, $plugin) {
  112. throw new Error\MissingHelperException([
  113. 'class' => $class,
  114. 'plugin' => $plugin
  115. ]);
  116. }
  117. /**
  118. * Create the helper instance.
  119. *
  120. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  121. * Enabled helpers will be registered with the event manager.
  122. *
  123. * @param string $class The classname that is missing.
  124. * @param array $settings An array of settings to use for the helper.
  125. * @return Component The constructed helper class.
  126. */
  127. protected function _create($class, $settings) {
  128. $instance = new $class($this->_View, $settings);
  129. $vars = array('request', 'theme', 'plugin');
  130. foreach ($vars as $var) {
  131. $instance->{$var} = $this->_View->{$var};
  132. }
  133. $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
  134. if ($enable) {
  135. $this->_eventManager->attach($instance);
  136. }
  137. return $instance;
  138. }
  139. /**
  140. * Destroys all objects in the registry.
  141. *
  142. * Removes all attached listeners and destroys all stored instances.
  143. *
  144. * @return void
  145. */
  146. public function reset() {
  147. foreach ($this->_loaded as $helper) {
  148. $this->_eventManager->detach($helper);
  149. }
  150. parent::reset();
  151. }
  152. }