~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Controller/Action/HelperBroker.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Controller
 
17
 * @subpackage Zend_Controller_Action
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 */
 
21
 
 
22
/**
 
23
 * @see Zend_Controller_Action_HelperBroker_PriorityStack
 
24
 */
 
25
require_once 'Zend/Controller/Action/HelperBroker/PriorityStack.php';
 
26
 
 
27
/**
 
28
 * @see Zend_Loader
 
29
 */
 
30
require_once 'Zend/Loader.php';
 
31
 
 
32
/**
 
33
 * @category   Zend
 
34
 * @package    Zend_Controller
 
35
 * @subpackage Zend_Controller_Action
 
36
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
37
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
38
 */
 
39
class Zend_Controller_Action_HelperBroker
 
40
{
 
41
    /**
 
42
     * $_actionController - ActionController reference
 
43
     *
 
44
     * @var Zend_Controller_Action
 
45
     */
 
46
    protected $_actionController;
 
47
 
 
48
    /**
 
49
     * @var Zend_Loader_PluginLoader_Interface
 
50
     */
 
51
    protected static $_pluginLoader;
 
52
 
 
53
    /**
 
54
     * $_helpers - Helper array
 
55
     *
 
56
     * @var Zend_Controller_Action_HelperBroker_PriorityStack
 
57
     */
 
58
    protected static $_stack = null;
 
59
 
 
60
    /**
 
61
     * Set PluginLoader for use with broker
 
62
     * 
 
63
     * @param  Zend_Loader_PluginLoader_Interface $loader 
 
64
     * @return void
 
65
     */
 
66
    public static function setPluginLoader($loader)
 
67
    {
 
68
        if ((null !== $loader) && (!$loader instanceof Zend_Loader_PluginLoader_Interface)) {
 
69
            require_once 'Zend/Controller/Action/Exception.php';
 
70
            throw new Zend_Controller_Action_Exception('Invalid plugin loader provided to HelperBroker');
 
71
        }
 
72
        self::$_pluginLoader = $loader;
 
73
    }
 
74
 
 
75
    /**
 
76
     * Retrieve PluginLoader
 
77
     * 
 
78
     * @return Zend_Loader_PluginLoader
 
79
     */
 
80
    public static function getPluginLoader()
 
81
    {
 
82
        if (null === self::$_pluginLoader) {
 
83
            require_once 'Zend/Loader/PluginLoader.php';
 
84
            self::$_pluginLoader = new Zend_Loader_PluginLoader(array(
 
85
                'Zend_Controller_Action_Helper' => 'Zend/Controller/Action/Helper/',
 
86
            ));
 
87
        }
 
88
        return self::$_pluginLoader;
 
89
    }
 
90
 
 
91
    /**
 
92
     * addPrefix() - Add repository of helpers by prefix
 
93
     *
 
94
     * @param string $prefix
 
95
     */
 
96
    static public function addPrefix($prefix)
 
97
    {
 
98
        $prefix = rtrim($prefix, '_');
 
99
        $path   = str_replace('_', DIRECTORY_SEPARATOR, $prefix);
 
100
        self::getPluginLoader()->addPrefixPath($prefix, $path);
 
101
    }
 
102
 
 
103
    /**
 
104
     * addPath() - Add path to repositories where Action_Helpers could be found.
 
105
     *
 
106
     * @param string $path
 
107
     * @param string $prefix Optional; defaults to 'Zend_Controller_Action_Helper'
 
108
     * @return void
 
109
     */
 
110
    static public function addPath($path, $prefix = 'Zend_Controller_Action_Helper')
 
111
    {
 
112
        self::getPluginLoader()->addPrefixPath($prefix, $path);
 
113
    }
 
114
 
 
115
    /**
 
116
     * addHelper() - Add helper objects
 
117
     *
 
118
     * @param Zend_Controller_Action_Helper_Abstract $helper
 
119
     * @return void
 
120
     */
 
121
    static public function addHelper(Zend_Controller_Action_Helper_Abstract $helper)
 
122
    {
 
123
        self::getStack()->push($helper);
 
124
        return;
 
125
    }
 
126
 
 
127
    /**
 
128
     * resetHelpers()
 
129
     *
 
130
     * @return void
 
131
     */
 
132
    static public function resetHelpers()
 
133
    {
 
134
        self::$_stack = null;
 
135
        return;
 
136
    }
 
137
 
 
138
    /**
 
139
     * Retrieve or initialize a helper statically
 
140
     *
 
141
     * Retrieves a helper object statically, loading on-demand if the helper
 
142
     * does not already exist in the stack. Always returns a helper, unless
 
143
     * the helper class cannot be found.
 
144
     *
 
145
     * @param  string $name
 
146
     * @return Zend_Controller_Action_Helper_Abstract
 
147
     */
 
148
    public static function getStaticHelper($name)
 
149
    {
 
150
        $name  = self::_normalizeHelperName($name);
 
151
        $stack = self::getStack();
 
152
        
 
153
        if (!isset($stack->{$name})) {
 
154
            self::_loadHelper($name);
 
155
        }
 
156
 
 
157
        return $stack->{$name};
 
158
    }
 
159
 
 
160
    /**
 
161
     * getExistingHelper() - get helper by name
 
162
     *
 
163
     * Static method to retrieve helper object. Only retrieves helpers already
 
164
     * initialized with the broker (either via addHelper() or on-demand loading
 
165
     * via getHelper()).
 
166
     *
 
167
     * Throws an exception if the referenced helper does not exist in the
 
168
     * stack; use {@link hasHelper()} to check if the helper is registered
 
169
     * prior to retrieving it.
 
170
     *
 
171
     * @param  string $name
 
172
     * @return Zend_Controller_Action_Helper_Abstract
 
173
     * @throws Zend_Controller_Action_Exception
 
174
     */
 
175
    public static function getExistingHelper($name)
 
176
    {
 
177
        $name  = self::_normalizeHelperName($name);
 
178
        $stack = self::getStack();
 
179
        
 
180
        if (!isset($stack->{$name})) {
 
181
            require_once 'Zend/Controller/Action/Exception.php';
 
182
            throw new Zend_Controller_Action_Exception('Action helper "' . $name . '" has not been registered with the helper broker');
 
183
        }
 
184
 
 
185
        return $stack->{$name};
 
186
    }
 
187
 
 
188
    /**
 
189
     * Return all registered helpers as helper => object pairs
 
190
     *
 
191
     * @return array
 
192
     */
 
193
    public static function getExistingHelpers()
 
194
    {
 
195
        return self::getStack()->getHelpersByName();
 
196
    }
 
197
 
 
198
    /**
 
199
     * Is a particular helper loaded in the broker?
 
200
     *
 
201
     * @param  string $name
 
202
     * @return boolean
 
203
     */
 
204
    public static function hasHelper($name)
 
205
    {
 
206
        $name = self::_normalizeHelperName($name);
 
207
        return isset(self::getStack()->{$name});
 
208
    }
 
209
 
 
210
    /**
 
211
     * Remove a particular helper from the broker
 
212
     *
 
213
     * @param  string $name
 
214
     * @return boolean
 
215
     */
 
216
    public static function removeHelper($name)
 
217
    {
 
218
        $name = self::_normalizeHelperName($name);
 
219
        $stack = self::getStack();
 
220
        if (isset($stack->{$name})) {
 
221
            unset($stack->{$name});
 
222
        }
 
223
 
 
224
        return false;
 
225
    }
 
226
 
 
227
    /**
 
228
     * Lazy load the priority stack and return it
 
229
     *
 
230
     * @return Zend_Controller_Action_HelperBroker_PriorityStack
 
231
     */
 
232
    public static function getStack()
 
233
    {
 
234
        if (self::$_stack == null) {
 
235
            self::$_stack = new Zend_Controller_Action_HelperBroker_PriorityStack();
 
236
        }
 
237
        
 
238
        return self::$_stack;
 
239
    }
 
240
    
 
241
    /**
 
242
     * Constructor
 
243
     *
 
244
     * @param Zend_Controller_Action $actionController
 
245
     * @return void
 
246
     */
 
247
    public function __construct(Zend_Controller_Action $actionController)
 
248
    {
 
249
        $this->_actionController = $actionController;
 
250
        foreach (self::getStack() as $helper) {
 
251
            $helper->setActionController($actionController);
 
252
            $helper->init();
 
253
        }
 
254
    }
 
255
 
 
256
    /**
 
257
     * notifyPreDispatch() - called by action controller dispatch method
 
258
     *
 
259
     * @return void
 
260
     */
 
261
    public function notifyPreDispatch()
 
262
    {
 
263
        foreach (self::getStack() as $helper) {
 
264
            $helper->preDispatch();
 
265
        }
 
266
    }
 
267
 
 
268
    /**
 
269
     * notifyPostDispatch() - called by action controller dispatch method
 
270
     *
 
271
     * @return void
 
272
     */
 
273
    public function notifyPostDispatch()
 
274
    {
 
275
        foreach (self::getStack() as $helper) {
 
276
            $helper->postDispatch();
 
277
        }
 
278
    }
 
279
 
 
280
    /**
 
281
     * getHelper() - get helper by name
 
282
     *
 
283
     * @param  string $name
 
284
     * @return Zend_Controller_Action_Helper_Abstract
 
285
     */
 
286
    public function getHelper($name)
 
287
    {
 
288
        $name  = self::_normalizeHelperName($name);
 
289
        $stack = self::getStack();
 
290
 
 
291
        if (!isset($stack->{$name})) {
 
292
            self::_loadHelper($name);
 
293
        }
 
294
 
 
295
        $helper = $stack->{$name};
 
296
 
 
297
        $initialize = false;
 
298
        if (null === ($actionController = $helper->getActionController())) {
 
299
            $initialize = true;
 
300
        } elseif ($actionController !== $this->_actionController) {
 
301
            $initialize = true;
 
302
        }
 
303
 
 
304
        if ($initialize) {
 
305
            $helper->setActionController($this->_actionController)
 
306
                   ->init();
 
307
        }
 
308
 
 
309
        return $helper;
 
310
    }
 
311
 
 
312
    /**
 
313
     * Method overloading
 
314
     *
 
315
     * @param  string $method
 
316
     * @param  array $args
 
317
     * @return mixed
 
318
     * @throws Zend_Controller_Action_Exception if helper does not have a direct() method
 
319
     */
 
320
    public function __call($method, $args)
 
321
    {
 
322
        $helper = $this->getHelper($method);
 
323
        if (!method_exists($helper, 'direct')) {
 
324
            require_once 'Zend/Controller/Action/Exception.php';
 
325
            throw new Zend_Controller_Action_Exception('Helper "' . $method . '" does not support overloading via direct()');
 
326
        }
 
327
        return call_user_func_array(array($helper, 'direct'), $args);
 
328
    }
 
329
 
 
330
    /**
 
331
     * Retrieve helper by name as object property
 
332
     *
 
333
     * @param  string $name
 
334
     * @return Zend_Controller_Action_Helper_Abstract
 
335
     */
 
336
    public function __get($name)
 
337
    {
 
338
        return $this->getHelper($name);
 
339
    }
 
340
 
 
341
    /**
 
342
     * Normalize helper name for lookups
 
343
     *
 
344
     * @param  string $name
 
345
     * @return string
 
346
     */
 
347
    protected static function _normalizeHelperName($name)
 
348
    {
 
349
        if (strpos($name, '_') !== false) {
 
350
            $name = str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
 
351
        }
 
352
 
 
353
        return ucfirst($name);
 
354
    }
 
355
 
 
356
    /**
 
357
     * Load a helper
 
358
     *
 
359
     * @param  string $name
 
360
     * @return void
 
361
     */
 
362
    protected static function _loadHelper($name)
 
363
    {
 
364
        try {
 
365
            $class = self::getPluginLoader()->load($name);
 
366
        } catch (Zend_Loader_PluginLoader_Exception $e) {
 
367
            require_once 'Zend/Controller/Action/Exception.php';
 
368
            throw new Zend_Controller_Action_Exception('Action Helper by name ' . $name . ' not found');
 
369
        }
 
370
 
 
371
        $helper = new $class();
 
372
 
 
373
        if (!$helper instanceof Zend_Controller_Action_Helper_Abstract) {
 
374
            require_once 'Zend/Controller/Action/Exception.php';
 
375
            throw new Zend_Controller_Action_Exception('Helper name ' . $name . ' -> class ' . $class . ' is not of type Zend_Controller_Action_Helper_Abstract');
 
376
        }
 
377
 
 
378
        self::getStack()->push($helper);
 
379
    }
 
380
}