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