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