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.
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
23
* @see Zend_Controller_Action_HelperBroker_PriorityStack
25
require_once 'Zend/Controller/Action/HelperBroker/PriorityStack.php';
30
require_once 'Zend/Loader.php';
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
39
class Zend_Controller_Action_HelperBroker
42
* $_actionController - ActionController reference
44
* @var Zend_Controller_Action
46
protected $_actionController;
49
* @var Zend_Loader_PluginLoader_Interface
51
protected static $_pluginLoader;
54
* $_helpers - Helper array
56
* @var Zend_Controller_Action_HelperBroker_PriorityStack
58
protected static $_stack = null;
61
* Set PluginLoader for use with broker
63
* @param Zend_Loader_PluginLoader_Interface $loader
66
public static function setPluginLoader($loader)
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');
72
self::$_pluginLoader = $loader;
76
* Retrieve PluginLoader
78
* @return Zend_Loader_PluginLoader
80
public static function getPluginLoader()
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/',
88
return self::$_pluginLoader;
92
* addPrefix() - Add repository of helpers by prefix
94
* @param string $prefix
96
static public function addPrefix($prefix)
98
$prefix = rtrim($prefix, '_');
99
$path = str_replace('_', DIRECTORY_SEPARATOR, $prefix);
100
self::getPluginLoader()->addPrefixPath($prefix, $path);
104
* addPath() - Add path to repositories where Action_Helpers could be found.
106
* @param string $path
107
* @param string $prefix Optional; defaults to 'Zend_Controller_Action_Helper'
110
static public function addPath($path, $prefix = 'Zend_Controller_Action_Helper')
112
self::getPluginLoader()->addPrefixPath($prefix, $path);
116
* addHelper() - Add helper objects
118
* @param Zend_Controller_Action_Helper_Abstract $helper
121
static public function addHelper(Zend_Controller_Action_Helper_Abstract $helper)
123
self::getStack()->push($helper);
132
static public function resetHelpers()
134
self::$_stack = null;
139
* Retrieve or initialize a helper statically
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.
145
* @param string $name
146
* @return Zend_Controller_Action_Helper_Abstract
148
public static function getStaticHelper($name)
150
$name = self::_normalizeHelperName($name);
151
$stack = self::getStack();
153
if (!isset($stack->{$name})) {
154
self::_loadHelper($name);
157
return $stack->{$name};
161
* getExistingHelper() - get helper by name
163
* Static method to retrieve helper object. Only retrieves helpers already
164
* initialized with the broker (either via addHelper() or on-demand loading
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.
171
* @param string $name
172
* @return Zend_Controller_Action_Helper_Abstract
173
* @throws Zend_Controller_Action_Exception
175
public static function getExistingHelper($name)
177
$name = self::_normalizeHelperName($name);
178
$stack = self::getStack();
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');
185
return $stack->{$name};
189
* Return all registered helpers as helper => object pairs
193
public static function getExistingHelpers()
195
return self::getStack()->getHelpersByName();
199
* Is a particular helper loaded in the broker?
201
* @param string $name
204
public static function hasHelper($name)
206
$name = self::_normalizeHelperName($name);
207
return isset(self::getStack()->{$name});
211
* Remove a particular helper from the broker
213
* @param string $name
216
public static function removeHelper($name)
218
$name = self::_normalizeHelperName($name);
219
$stack = self::getStack();
220
if (isset($stack->{$name})) {
221
unset($stack->{$name});
228
* Lazy load the priority stack and return it
230
* @return Zend_Controller_Action_HelperBroker_PriorityStack
232
public static function getStack()
234
if (self::$_stack == null) {
235
self::$_stack = new Zend_Controller_Action_HelperBroker_PriorityStack();
238
return self::$_stack;
244
* @param Zend_Controller_Action $actionController
247
public function __construct(Zend_Controller_Action $actionController)
249
$this->_actionController = $actionController;
250
foreach (self::getStack() as $helper) {
251
$helper->setActionController($actionController);
257
* notifyPreDispatch() - called by action controller dispatch method
261
public function notifyPreDispatch()
263
foreach (self::getStack() as $helper) {
264
$helper->preDispatch();
269
* notifyPostDispatch() - called by action controller dispatch method
273
public function notifyPostDispatch()
275
foreach (self::getStack() as $helper) {
276
$helper->postDispatch();
281
* getHelper() - get helper by name
283
* @param string $name
284
* @return Zend_Controller_Action_Helper_Abstract
286
public function getHelper($name)
288
$name = self::_normalizeHelperName($name);
289
$stack = self::getStack();
291
if (!isset($stack->{$name})) {
292
self::_loadHelper($name);
295
$helper = $stack->{$name};
298
if (null === ($actionController = $helper->getActionController())) {
300
} elseif ($actionController !== $this->_actionController) {
305
$helper->setActionController($this->_actionController)
315
* @param string $method
318
* @throws Zend_Controller_Action_Exception if helper does not have a direct() method
320
public function __call($method, $args)
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()');
327
return call_user_func_array(array($helper, 'direct'), $args);
331
* Retrieve helper by name as object property
333
* @param string $name
334
* @return Zend_Controller_Action_Helper_Abstract
336
public function __get($name)
338
return $this->getHelper($name);
342
* Normalize helper name for lookups
344
* @param string $name
347
protected static function _normalizeHelperName($name)
349
if (strpos($name, '_') !== false) {
350
$name = str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
353
return ucfirst($name);
359
* @param string $name
362
protected static function _loadHelper($name)
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');
371
$helper = new $class();
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');
378
self::getStack()->push($helper);