1: <?php
2: /**
3: * Helpers collection is used as a registry for loaded helpers and handles loading
4: * and constructing helper class objects.
5: *
6: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
7: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
8: *
9: * Licensed under The MIT License
10: * Redistributions of files must retain the above copyright notice.
11: *
12: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
13: * @link http://cakephp.org CakePHP(tm) Project
14: * @package Cake.View
15: * @since CakePHP(tm) v 2.0
16: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
17: */
18:
19: App::uses('ObjectCollection', 'Utility');
20: App::uses('CakeEventListener', 'Event');
21:
22: /**
23: * Helpers collection is used as a registry for loaded helpers and handles loading
24: * and constructing helper class objects.
25: *
26: * @package Cake.View
27: */
28: class HelperCollection extends ObjectCollection implements CakeEventListener {
29:
30: /**
31: * View object to use when making helpers.
32: *
33: * @var View
34: */
35: protected $_View;
36:
37: /**
38: * Constructor
39: *
40: * @param View $view
41: */
42: public function __construct(View $view) {
43: $this->_View = $view;
44: }
45:
46: /**
47: * Tries to lazy load a helper based on its name, if it cannot be found
48: * in the application folder, then it tries looking under the current plugin
49: * if any
50: *
51: * @param string $helper The helper name to be loaded
52: * @return boolean wheter the helper could be loaded or not
53: * @throws MissingHelperException When a helper could not be found.
54: * App helpers are searched, and then plugin helpers.
55: */
56: public function __isset($helper) {
57: if (parent::__isset($helper)) {
58: return true;
59: }
60:
61: try {
62: $this->load($helper);
63: } catch (MissingHelperException $exception) {
64: if ($this->_View->plugin) {
65: $this->load($this->_View->plugin . '.' . $helper);
66: return true;
67: }
68: }
69:
70: if (!empty($exception)) {
71: throw $exception;
72: }
73:
74: return true;
75: }
76:
77: /**
78: * Provide public read access to the loaded objects
79: *
80: * @param string $name Name of property to read
81: * @return mixed
82: */
83: public function __get($name) {
84: if ($result = parent::__get($name)) {
85: return $result;
86: }
87: if ($this->__isset($name)) {
88: return $this->_loaded[$name];
89: }
90: return null;
91: }
92:
93: /**
94: * Loads/constructs a helper. Will return the instance in the registry if it already exists.
95: * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
96: * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
97: * declaring $helpers arrays you can disable callbacks on helpers.
98: *
99: * You can alias your helper as an existing helper by setting the 'className' key, i.e.,
100: * {{{
101: * public $helpers = array(
102: * 'Html' => array(
103: * 'className' => 'AliasedHtml'
104: * );
105: * );
106: * }}}
107: * All calls to the `Html` helper would use `AliasedHtml` instead.
108: *
109: * @param string $helper Helper name to load
110: * @param array $settings Settings for the helper.
111: * @return Helper A helper object, Either the existing loaded helper or a new one.
112: * @throws MissingHelperException when the helper could not be found
113: */
114: public function load($helper, $settings = array()) {
115: if (is_array($settings) && isset($settings['className'])) {
116: $alias = $helper;
117: $helper = $settings['className'];
118: }
119: list($plugin, $name) = pluginSplit($helper, true);
120: if (!isset($alias)) {
121: $alias = $name;
122: }
123:
124: if (isset($this->_loaded[$alias])) {
125: return $this->_loaded[$alias];
126: }
127: $helperClass = $name . 'Helper';
128: App::uses($helperClass, $plugin . 'View/Helper');
129: if (!class_exists($helperClass)) {
130: throw new MissingHelperException(array(
131: 'class' => $helperClass,
132: 'plugin' => substr($plugin, 0, -1)
133: ));
134: }
135: $this->_loaded[$alias] = new $helperClass($this->_View, $settings);
136:
137: $vars = array('request', 'theme', 'plugin');
138: foreach ($vars as $var) {
139: $this->_loaded[$alias]->{$var} = $this->_View->{$var};
140: }
141: $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
142: if ($enable) {
143: $this->enable($alias);
144: }
145: return $this->_loaded[$alias];
146: }
147:
148: /**
149: * Returns a list of all events that will fire in the View during it's lifecycle.
150: *
151: * @return array
152: */
153: public function implementedEvents() {
154: return array(
155: 'View.beforeRenderFile' => 'trigger',
156: 'View.afterRenderFile' => 'trigger',
157: 'View.beforeRender' => 'trigger',
158: 'View.afterRender' => 'trigger',
159: 'View.beforeLayout' => 'trigger',
160: 'View.afterLayout' => 'trigger'
161: );
162: }
163:
164: /**
165: * Trigger a callback method on every object in the collection.
166: * Used to trigger methods on objects in the collection. Will fire the methods in the
167: * order they were attached.
168: *
169: * ### Options
170: *
171: * - `breakOn` Set to the value or values you want the callback propagation to stop on.
172: * Can either be a scalar value, or an array of values to break on. Defaults to `false`.
173: *
174: * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
175: * will be returned. If used in combination with `collectReturn` the collected results will be returned.
176: * Defaults to `false`.
177: *
178: * - `collectReturn` Set to true to collect the return of each object into an array.
179: * This array of return values will be returned from the trigger() call. Defaults to `false`.
180: *
181: * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
182: * Setting modParams to an integer value will allow you to modify the parameter with that index.
183: * Any non-null value will modify the parameter index indicated.
184: * Defaults to false.
185: *
186: *
187: * @param string $callback|CakeEvent Method to fire on all the objects. Its assumed all the objects implement
188: * the method you are calling. If an instance of CakeEvent is provided, then then Event name will parsed to
189: * get the callback name. This is done by getting the last word after any dot in the event name
190: * (eg. `Model.afterSave` event will trigger the `afterSave` callback)
191: * @param array $params Array of parameters for the triggered callback.
192: * @param array $options Array of options.
193: * @return mixed Either the last result or all results if collectReturn is on.
194: * @throws CakeException when modParams is used with an index that does not exist.
195: */
196: public function trigger($callback, $params = array(), $options = array()) {
197: if ($callback instanceof CakeEvent) {
198: $callback->omitSubject = true;
199: }
200: return parent::trigger($callback, $params, $options);
201: }
202:
203: }
204: