1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 2.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\View;
16:
17: use Cake\Core\App;
18: use Cake\Core\ObjectRegistry;
19: use Cake\Event\EventDispatcherInterface;
20: use Cake\Event\EventDispatcherTrait;
21: use Cake\View\Exception\MissingHelperException;
22:
23: /**
24: * HelperRegistry is used as a registry for loaded helpers and handles loading
25: * and constructing helper class objects.
26: */
27: class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
28: {
29: use EventDispatcherTrait;
30:
31: /**
32: * View object to use when making helpers.
33: *
34: * @var \Cake\View\View
35: */
36: protected $_View;
37:
38: /**
39: * Constructor
40: *
41: * @param \Cake\View\View $view View object.
42: */
43: public function __construct(View $view)
44: {
45: $this->_View = $view;
46: $this->setEventManager($view->getEventManager());
47: }
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 bool whether the helper could be loaded or not
56: * @throws \Cake\View\Exception\MissingHelperException When a helper could not be found.
57: * App helpers are searched, and then plugin helpers.
58: */
59: public function __isset($helper)
60: {
61: if (isset($this->_loaded[$helper])) {
62: return true;
63: }
64:
65: try {
66: $this->load($helper);
67: } catch (Exception\MissingHelperException $exception) {
68: if ($this->_View->getPlugin()) {
69: $this->load($this->_View->getPlugin() . '.' . $helper);
70:
71: return true;
72: }
73: }
74:
75: if (!empty($exception)) {
76: throw $exception;
77: }
78:
79: return true;
80: }
81:
82: /**
83: * Provide public read access to the loaded objects
84: *
85: * @param string $name Name of property to read
86: * @return mixed
87: */
88: public function __get($name)
89: {
90: if (isset($this->_loaded[$name])) {
91: return $this->_loaded[$name];
92: }
93: if (isset($this->{$name})) {
94: return $this->_loaded[$name];
95: }
96:
97: return null;
98: }
99:
100: /**
101: * Resolve a helper classname.
102: *
103: * Part of the template method for Cake\Core\ObjectRegistry::load()
104: *
105: * @param string $class Partial classname to resolve.
106: * @return string|false Either the correct classname or false.
107: */
108: protected function _resolveClassName($class)
109: {
110: return App::className($class, 'View/Helper', 'Helper');
111: }
112:
113: /**
114: * Throws an exception when a helper is missing.
115: *
116: * Part of the template method for Cake\Core\ObjectRegistry::load()
117: * and Cake\Core\ObjectRegistry::unload()
118: *
119: * @param string $class The classname that is missing.
120: * @param string $plugin The plugin the helper is missing in.
121: * @return void
122: * @throws \Cake\View\Exception\MissingHelperException
123: */
124: protected function _throwMissingClassError($class, $plugin)
125: {
126: throw new MissingHelperException([
127: 'class' => $class . 'Helper',
128: 'plugin' => $plugin
129: ]);
130: }
131:
132: /**
133: * Create the helper instance.
134: *
135: * Part of the template method for Cake\Core\ObjectRegistry::load()
136: * Enabled helpers will be registered with the event manager.
137: *
138: * @param string $class The class to create.
139: * @param string $alias The alias of the loaded helper.
140: * @param array $settings An array of settings to use for the helper.
141: * @return \Cake\View\Helper The constructed helper class.
142: */
143: protected function _create($class, $alias, $settings)
144: {
145: $instance = new $class($this->_View, $settings);
146:
147: $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
148: if ($enable) {
149: $this->getEventManager()->on($instance);
150: }
151:
152: return $instance;
153: }
154: }
155: