統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / View / HelperCollection.php @ master

履歴 | 表示 | アノテート | ダウンロード (6.453 KB)

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