File indexing completed on 2025-01-26 05:24:54
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Controller 0017 * @subpackage Zend_Controller_Action 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @see Zend_Controller_Action_HelperBroker_PriorityStack 0025 */ 0026 // require_once 'Zend/Controller/Action/HelperBroker/PriorityStack.php'; 0027 0028 /** 0029 * @see Zend_Loader 0030 */ 0031 // require_once 'Zend/Loader.php'; 0032 0033 /** 0034 * @category Zend 0035 * @package Zend_Controller 0036 * @subpackage Zend_Controller_Action 0037 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0038 * @license http://framework.zend.com/license/new-bsd New BSD License 0039 */ 0040 class Zend_Controller_Action_HelperBroker 0041 { 0042 /** 0043 * $_actionController - ActionController reference 0044 * 0045 * @var Zend_Controller_Action 0046 */ 0047 protected $_actionController; 0048 0049 /** 0050 * @var Zend_Loader_PluginLoader_Interface 0051 */ 0052 protected static $_pluginLoader; 0053 0054 /** 0055 * $_helpers - Helper array 0056 * 0057 * @var Zend_Controller_Action_HelperBroker_PriorityStack 0058 */ 0059 protected static $_stack = null; 0060 0061 /** 0062 * Set PluginLoader for use with broker 0063 * 0064 * @param Zend_Loader_PluginLoader_Interface $loader 0065 * @return void 0066 */ 0067 public static function setPluginLoader($loader) 0068 { 0069 if ((null !== $loader) && (!$loader instanceof Zend_Loader_PluginLoader_Interface)) { 0070 // require_once 'Zend/Controller/Action/Exception.php'; 0071 throw new Zend_Controller_Action_Exception('Invalid plugin loader provided to HelperBroker'); 0072 } 0073 self::$_pluginLoader = $loader; 0074 } 0075 0076 /** 0077 * Retrieve PluginLoader 0078 * 0079 * @return Zend_Loader_PluginLoader 0080 */ 0081 public static function getPluginLoader() 0082 { 0083 if (null === self::$_pluginLoader) { 0084 // require_once 'Zend/Loader/PluginLoader.php'; 0085 self::$_pluginLoader = new Zend_Loader_PluginLoader(array( 0086 'Zend_Controller_Action_Helper' => 'Zend/Controller/Action/Helper/', 0087 )); 0088 } 0089 return self::$_pluginLoader; 0090 } 0091 0092 /** 0093 * addPrefix() - Add repository of helpers by prefix 0094 * 0095 * @param string $prefix 0096 */ 0097 static public function addPrefix($prefix) 0098 { 0099 $prefix = rtrim($prefix, '_'); 0100 $path = str_replace('_', DIRECTORY_SEPARATOR, $prefix); 0101 self::getPluginLoader()->addPrefixPath($prefix, $path); 0102 } 0103 0104 /** 0105 * addPath() - Add path to repositories where Action_Helpers could be found. 0106 * 0107 * @param string $path 0108 * @param string $prefix Optional; defaults to 'Zend_Controller_Action_Helper' 0109 * @return void 0110 */ 0111 static public function addPath($path, $prefix = 'Zend_Controller_Action_Helper') 0112 { 0113 self::getPluginLoader()->addPrefixPath($prefix, $path); 0114 } 0115 0116 /** 0117 * addHelper() - Add helper objects 0118 * 0119 * @param Zend_Controller_Action_Helper_Abstract $helper 0120 * @return void 0121 */ 0122 static public function addHelper(Zend_Controller_Action_Helper_Abstract $helper) 0123 { 0124 self::getStack()->push($helper); 0125 return; 0126 } 0127 0128 /** 0129 * resetHelpers() 0130 * 0131 * @return void 0132 */ 0133 static public function resetHelpers() 0134 { 0135 self::$_stack = null; 0136 return; 0137 } 0138 0139 /** 0140 * Retrieve or initialize a helper statically 0141 * 0142 * Retrieves a helper object statically, loading on-demand if the helper 0143 * does not already exist in the stack. Always returns a helper, unless 0144 * the helper class cannot be found. 0145 * 0146 * @param string $name 0147 * @return Zend_Controller_Action_Helper_Abstract 0148 */ 0149 public static function getStaticHelper($name) 0150 { 0151 $name = self::_normalizeHelperName($name); 0152 $stack = self::getStack(); 0153 0154 if (!isset($stack->{$name})) { 0155 self::_loadHelper($name); 0156 } 0157 0158 return $stack->{$name}; 0159 } 0160 0161 /** 0162 * getExistingHelper() - get helper by name 0163 * 0164 * Static method to retrieve helper object. Only retrieves helpers already 0165 * initialized with the broker (either via addHelper() or on-demand loading 0166 * via getHelper()). 0167 * 0168 * Throws an exception if the referenced helper does not exist in the 0169 * stack; use {@link hasHelper()} to check if the helper is registered 0170 * prior to retrieving it. 0171 * 0172 * @param string $name 0173 * @return Zend_Controller_Action_Helper_Abstract 0174 * @throws Zend_Controller_Action_Exception 0175 */ 0176 public static function getExistingHelper($name) 0177 { 0178 $name = self::_normalizeHelperName($name); 0179 $stack = self::getStack(); 0180 0181 if (!isset($stack->{$name})) { 0182 // require_once 'Zend/Controller/Action/Exception.php'; 0183 throw new Zend_Controller_Action_Exception('Action helper "' . $name . '" has not been registered with the helper broker'); 0184 } 0185 0186 return $stack->{$name}; 0187 } 0188 0189 /** 0190 * Return all registered helpers as helper => object pairs 0191 * 0192 * @return array 0193 */ 0194 public static function getExistingHelpers() 0195 { 0196 return self::getStack()->getHelpersByName(); 0197 } 0198 0199 /** 0200 * Is a particular helper loaded in the broker? 0201 * 0202 * @param string $name 0203 * @return boolean 0204 */ 0205 public static function hasHelper($name) 0206 { 0207 $name = self::_normalizeHelperName($name); 0208 return isset(self::getStack()->{$name}); 0209 } 0210 0211 /** 0212 * Remove a particular helper from the broker 0213 * 0214 * @param string $name 0215 * @return boolean 0216 */ 0217 public static function removeHelper($name) 0218 { 0219 $name = self::_normalizeHelperName($name); 0220 $stack = self::getStack(); 0221 if (isset($stack->{$name})) { 0222 unset($stack->{$name}); 0223 } 0224 0225 return false; 0226 } 0227 0228 /** 0229 * Lazy load the priority stack and return it 0230 * 0231 * @return Zend_Controller_Action_HelperBroker_PriorityStack 0232 */ 0233 public static function getStack() 0234 { 0235 if (self::$_stack == null) { 0236 self::$_stack = new Zend_Controller_Action_HelperBroker_PriorityStack(); 0237 } 0238 0239 return self::$_stack; 0240 } 0241 0242 /** 0243 * Constructor 0244 * 0245 * @param Zend_Controller_Action $actionController 0246 * @return void 0247 */ 0248 public function __construct(Zend_Controller_Action $actionController) 0249 { 0250 $this->_actionController = $actionController; 0251 foreach (self::getStack() as $helper) { 0252 $helper->setActionController($actionController); 0253 $helper->init(); 0254 } 0255 } 0256 0257 /** 0258 * notifyPreDispatch() - called by action controller dispatch method 0259 * 0260 * @return void 0261 */ 0262 public function notifyPreDispatch() 0263 { 0264 foreach (self::getStack() as $helper) { 0265 $helper->preDispatch(); 0266 } 0267 } 0268 0269 /** 0270 * notifyPostDispatch() - called by action controller dispatch method 0271 * 0272 * @return void 0273 */ 0274 public function notifyPostDispatch() 0275 { 0276 foreach (self::getStack() as $helper) { 0277 $helper->postDispatch(); 0278 } 0279 } 0280 0281 /** 0282 * getHelper() - get helper by name 0283 * 0284 * @param string $name 0285 * @return Zend_Controller_Action_Helper_Abstract 0286 */ 0287 public function getHelper($name) 0288 { 0289 $name = self::_normalizeHelperName($name); 0290 $stack = self::getStack(); 0291 0292 if (!isset($stack->{$name})) { 0293 self::_loadHelper($name); 0294 } 0295 0296 $helper = $stack->{$name}; 0297 0298 $initialize = false; 0299 if (null === ($actionController = $helper->getActionController())) { 0300 $initialize = true; 0301 } elseif ($actionController !== $this->_actionController) { 0302 $initialize = true; 0303 } 0304 0305 if ($initialize) { 0306 $helper->setActionController($this->_actionController) 0307 ->init(); 0308 } 0309 0310 return $helper; 0311 } 0312 0313 /** 0314 * Method overloading 0315 * 0316 * @param string $method 0317 * @param array $args 0318 * @return mixed 0319 * @throws Zend_Controller_Action_Exception if helper does not have a direct() method 0320 */ 0321 public function __call($method, $args) 0322 { 0323 $helper = $this->getHelper($method); 0324 if (!method_exists($helper, 'direct')) { 0325 // require_once 'Zend/Controller/Action/Exception.php'; 0326 throw new Zend_Controller_Action_Exception('Helper "' . $method . '" does not support overloading via direct()'); 0327 } 0328 return call_user_func_array(array($helper, 'direct'), $args); 0329 } 0330 0331 /** 0332 * Retrieve helper by name as object property 0333 * 0334 * @param string $name 0335 * @return Zend_Controller_Action_Helper_Abstract 0336 */ 0337 public function __get($name) 0338 { 0339 return $this->getHelper($name); 0340 } 0341 0342 /** 0343 * Normalize helper name for lookups 0344 * 0345 * @param string $name 0346 * @return string 0347 */ 0348 protected static function _normalizeHelperName($name) 0349 { 0350 if (strpos($name, '_') !== false) { 0351 $name = str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); 0352 } 0353 0354 return ucfirst($name); 0355 } 0356 0357 /** 0358 * Load a helper 0359 * 0360 * @param string $name 0361 * @return void 0362 */ 0363 protected static function _loadHelper($name) 0364 { 0365 try { 0366 $class = self::getPluginLoader()->load($name); 0367 } catch (Zend_Loader_PluginLoader_Exception $e) { 0368 // require_once 'Zend/Controller/Action/Exception.php'; 0369 throw new Zend_Controller_Action_Exception('Action Helper by name ' . $name . ' not found', 0, $e); 0370 } 0371 0372 $helper = new $class(); 0373 0374 if (!$helper instanceof Zend_Controller_Action_Helper_Abstract) { 0375 // require_once 'Zend/Controller/Action/Exception.php'; 0376 throw new Zend_Controller_Action_Exception('Helper name ' . $name . ' -> class ' . $class . ' is not of type Zend_Controller_Action_Helper_Abstract'); 0377 } 0378 0379 self::getStack()->push($helper); 0380 } 0381 }