HelperRegistry.php 4.6 KB

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