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: * Loads/constructs a helper. Will return the instance in the registry if it already exists.
48: * By setting `$enable` to false you can disable callbacks for a helper. Alternatively you
49: * can set `$settings['enabled'] = false` to disable callbacks. This alias is provided so that when
50: * declaring $helpers arrays you can disable callbacks on helpers.
51: *
52: * You can alias your helper as an existing helper by setting the 'className' key, i.e.,
53: * {{{
54: * public $helpers = array(
55: * 'Html' => array(
56: * 'className' => 'AliasedHtml'
57: * );
58: * );
59: * }}}
60: * All calls to the `Html` helper would use `AliasedHtml` instead.
61: *
62: * @param string $helper Helper name to load
63: * @param array $settings Settings for the helper.
64: * @return Helper A helper object, Either the existing loaded helper or a new one.
65: * @throws MissingHelperException when the helper could not be found
66: */
67: public function load($helper, $settings = array()) {
68: if (is_array($settings) && isset($settings['className'])) {
69: $alias = $helper;
70: $helper = $settings['className'];
71: }
72: list($plugin, $name) = pluginSplit($helper, true);
73: if (!isset($alias)) {
74: $alias = $name;
75: }
76:
77: if (isset($this->_loaded[$alias])) {
78: return $this->_loaded[$alias];
79: }
80: $helperClass = $name . 'Helper';
81: App::uses($helperClass, $plugin . 'View/Helper');
82: if (!class_exists($helperClass)) {
83: throw new MissingHelperException(array(
84: 'class' => $helperClass,
85: 'plugin' => substr($plugin, 0, -1)
86: ));
87: }
88: $this->_loaded[$alias] = new $helperClass($this->_View, $settings);
89:
90: $vars = array('request', 'theme', 'plugin');
91: foreach ($vars as $var) {
92: $this->_loaded[$alias]->{$var} = $this->_View->{$var};
93: }
94: $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
95: if ($enable) {
96: $this->enable($alias);
97: }
98: return $this->_loaded[$alias];
99: }
100:
101: /**
102: * Returns a list of all events that will fire in the View during it's lifecycle.
103: *
104: * @return array
105: */
106: public function implementedEvents() {
107: return array(
108: 'View.beforeRenderFile' => 'trigger',
109: 'View.afterRenderFile' => 'trigger',
110: 'View.beforeRender' => 'trigger',
111: 'View.afterRender' => 'trigger',
112: 'View.beforeLayout' => 'trigger',
113: 'View.afterLayout' => 'trigger'
114: );
115: }
116:
117: /**
118: * Trigger a callback method on every object in the collection.
119: * Used to trigger methods on objects in the collection. Will fire the methods in the
120: * order they were attached.
121: *
122: * ### Options
123: *
124: * - `breakOn` Set to the value or values you want the callback propagation to stop on.
125: * Can either be a scalar value, or an array of values to break on. Defaults to `false`.
126: *
127: * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
128: * will be returned. If used in combination with `collectReturn` the collected results will be returned.
129: * Defaults to `false`.
130: *
131: * - `collectReturn` Set to true to collect the return of each object into an array.
132: * This array of return values will be returned from the trigger() call. Defaults to `false`.
133: *
134: * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
135: * Setting modParams to an integer value will allow you to modify the parameter with that index.
136: * Any non-null value will modify the parameter index indicated.
137: * Defaults to false.
138: *
139: *
140: * @param string $callback|CakeEvent Method to fire on all the objects. Its assumed all the objects implement
141: * the method you are calling. If an instance of CakeEvent is provided, then then Event name will parsed to
142: * get the callback name. This is done by getting the last word after any dot in the event name
143: * (eg. `Model.afterSave` event will trigger the `afterSave` callback)
144: * @param array $params Array of parameters for the triggered callback.
145: * @param array $options Array of options.
146: * @return mixed Either the last result or all results if collectReturn is on.
147: * @throws CakeException when modParams is used with an index that does not exist.
148: */
149: public function trigger($callback, $params = array(), $options = array()) {
150: if ($callback instanceof CakeEvent) {
151: $callback->omitSubject = true;
152: }
153: return parent::trigger($callback, $params, $options);
154: }
155:
156: }
157: