web/lib/Zend/View/Helper/Navigation.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_View
       
    17  * @subpackage Helper
       
    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: Navigation.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_View_Helper_Navigation_HelperAbstract
       
    25  */
       
    26 require_once 'Zend/View/Helper/Navigation/HelperAbstract.php';
       
    27 
       
    28 /**
       
    29  * Proxy helper for retrieving navigational helpers and forwarding calls
       
    30  *
       
    31  * @category   Zend
       
    32  * @package    Zend_View
       
    33  * @subpackage Helper
       
    34  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    35  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    36  */
       
    37 class Zend_View_Helper_Navigation
       
    38     extends Zend_View_Helper_Navigation_HelperAbstract
       
    39 {
       
    40     /**
       
    41      * View helper namespace
       
    42      *
       
    43      * @var string
       
    44      */
       
    45     const NS = 'Zend_View_Helper_Navigation';
       
    46 
       
    47     /**
       
    48      * Default proxy to use in {@link render()}
       
    49      *
       
    50      * @var string
       
    51      */
       
    52     protected $_defaultProxy = 'menu';
       
    53 
       
    54     /**
       
    55      * Contains references to proxied helpers
       
    56      *
       
    57      * @var array
       
    58      */
       
    59     protected $_helpers = array();
       
    60 
       
    61     /**
       
    62      * Whether container should be injected when proxying
       
    63      *
       
    64      * @var bool
       
    65      */
       
    66     protected $_injectContainer = true;
       
    67 
       
    68     /**
       
    69      * Whether ACL should be injected when proxying
       
    70      *
       
    71      * @var bool
       
    72      */
       
    73     protected $_injectAcl = true;
       
    74 
       
    75     /**
       
    76      * Whether translator should be injected when proxying
       
    77      *
       
    78      * @var bool
       
    79      */
       
    80     protected $_injectTranslator = true;
       
    81 
       
    82     /**
       
    83      * Helper entry point
       
    84      *
       
    85      * @param  Zend_Navigation_Container $container  [optional] container to
       
    86      *                                               operate on
       
    87      * @return Zend_View_Helper_Navigation           fluent interface, returns
       
    88      *                                               self
       
    89      */
       
    90     public function navigation(Zend_Navigation_Container $container = null)
       
    91     {
       
    92         if (null !== $container) {
       
    93             $this->setContainer($container);
       
    94         }
       
    95 
       
    96         return $this;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Magic overload: Proxy to other navigation helpers or the container
       
   101      *
       
   102      * Examples of usage from a view script or layout:
       
   103      * <code>
       
   104      * // proxy to Menu helper and render container:
       
   105      * echo $this->navigation()->menu();
       
   106      *
       
   107      * // proxy to Breadcrumbs helper and set indentation:
       
   108      * $this->navigation()->breadcrumbs()->setIndent(8);
       
   109      *
       
   110      * // proxy to container and find all pages with 'blog' route:
       
   111      * $blogPages = $this->navigation()->findAllByRoute('blog');
       
   112      * </code>
       
   113      *
       
   114      * @param  string $method             helper name or method name in
       
   115      *                                    container
       
   116      * @param  array  $arguments          [optional] arguments to pass
       
   117      * @return mixed                      returns what the proxied call returns
       
   118      * @throws Zend_View_Exception        if proxying to a helper, and the
       
   119      *                                    helper is not an instance of the
       
   120      *                                    interface specified in
       
   121      *                                    {@link findHelper()}
       
   122      * @throws Zend_Navigation_Exception  if method does not exist in container
       
   123      */
       
   124     public function __call($method, array $arguments = array())
       
   125     {
       
   126         // check if call should proxy to another helper
       
   127         if ($helper = $this->findHelper($method, false)) {
       
   128             return call_user_func_array(array($helper, $method), $arguments);
       
   129         }
       
   130 
       
   131         // default behaviour: proxy call to container
       
   132         return parent::__call($method, $arguments);
       
   133     }
       
   134 
       
   135     /**
       
   136      * Returns the helper matching $proxy
       
   137      *
       
   138      * The helper must implement the interface
       
   139      * {@link Zend_View_Helper_Navigation_Helper}.
       
   140      *
       
   141      * @param string $proxy                        helper name
       
   142      * @param bool   $strict                       [optional] whether
       
   143      *                                             exceptions should be
       
   144      *                                             thrown if something goes
       
   145      *                                             wrong. Default is true.
       
   146      * @return Zend_View_Helper_Navigation_Helper  helper instance
       
   147      * @throws Zend_Loader_PluginLoader_Exception  if $strict is true and
       
   148      *                                             helper cannot be found
       
   149      * @throws Zend_View_Exception                 if $strict is true and
       
   150      *                                             helper does not implement
       
   151      *                                             the specified interface
       
   152      */
       
   153     public function findHelper($proxy, $strict = true)
       
   154     {
       
   155         if (isset($this->_helpers[$proxy])) {
       
   156             return $this->_helpers[$proxy];
       
   157         }
       
   158 
       
   159         if (!$this->view->getPluginLoader('helper')->getPaths(self::NS)) {
       
   160             $this->view->addHelperPath(
       
   161                     str_replace('_', '/', self::NS),
       
   162                     self::NS);
       
   163         }
       
   164 
       
   165         if ($strict) {
       
   166             $helper = $this->view->getHelper($proxy);
       
   167         } else {
       
   168             try {
       
   169                 $helper = $this->view->getHelper($proxy);
       
   170             } catch (Zend_Loader_PluginLoader_Exception $e) {
       
   171                 return null;
       
   172             }
       
   173         }
       
   174 
       
   175         if (!$helper instanceof Zend_View_Helper_Navigation_Helper) {
       
   176             if ($strict) {
       
   177                 require_once 'Zend/View/Exception.php';
       
   178                 $e = new Zend_View_Exception(sprintf(
       
   179                         'Proxy helper "%s" is not an instance of ' .
       
   180                         'Zend_View_Helper_Navigation_Helper',
       
   181                         get_class($helper)));
       
   182                 $e->setView($this->view);
       
   183                 throw $e;
       
   184             }
       
   185 
       
   186             return null;
       
   187         }
       
   188 
       
   189         $this->_inject($helper);
       
   190         $this->_helpers[$proxy] = $helper;
       
   191 
       
   192         return $helper;
       
   193     }
       
   194 
       
   195     /**
       
   196      * Injects container, ACL, and translator to the given $helper if this
       
   197      * helper is configured to do so
       
   198      *
       
   199      * @param  Zend_View_Helper_Navigation_Helper $helper  helper instance
       
   200      * @return void
       
   201      */
       
   202     protected function _inject(Zend_View_Helper_Navigation_Helper $helper)
       
   203     {
       
   204         if ($this->getInjectContainer() && !$helper->hasContainer()) {
       
   205             $helper->setContainer($this->getContainer());
       
   206         }
       
   207 
       
   208         if ($this->getInjectAcl()) {
       
   209             if (!$helper->hasAcl()) {
       
   210                 $helper->setAcl($this->getAcl());
       
   211             }
       
   212             if (!$helper->hasRole()) {
       
   213                 $helper->setRole($this->getRole());
       
   214             }
       
   215         }
       
   216 
       
   217         if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
       
   218             $helper->setTranslator($this->getTranslator());
       
   219         }
       
   220     }
       
   221 
       
   222     // Accessors:
       
   223 
       
   224     /**
       
   225      * Sets the default proxy to use in {@link render()}
       
   226      *
       
   227      * @param  string $proxy                default proxy
       
   228      * @return Zend_View_Helper_Navigation  fluent interface, returns self
       
   229      */
       
   230     public function setDefaultProxy($proxy)
       
   231     {
       
   232         $this->_defaultProxy = (string) $proxy;
       
   233         return $this;
       
   234     }
       
   235 
       
   236     /**
       
   237      * Returns the default proxy to use in {@link render()}
       
   238      *
       
   239      * @return string  the default proxy to use in {@link render()}
       
   240      */
       
   241     public function getDefaultProxy()
       
   242     {
       
   243         return $this->_defaultProxy;
       
   244     }
       
   245 
       
   246     /**
       
   247      * Sets whether container should be injected when proxying
       
   248      *
       
   249      * @param bool $injectContainer         [optional] whether container should
       
   250      *                                      be injected when proxying. Default
       
   251      *                                      is true.
       
   252      * @return Zend_View_Helper_Navigation  fluent interface, returns self
       
   253      */
       
   254     public function setInjectContainer($injectContainer = true)
       
   255     {
       
   256         $this->_injectContainer = (bool) $injectContainer;
       
   257         return $this;
       
   258     }
       
   259 
       
   260     /**
       
   261      * Returns whether container should be injected when proxying
       
   262      *
       
   263      * @return bool  whether container should be injected when proxying
       
   264      */
       
   265     public function getInjectContainer()
       
   266     {
       
   267         return $this->_injectContainer;
       
   268     }
       
   269 
       
   270     /**
       
   271      * Sets whether ACL should be injected when proxying
       
   272      *
       
   273      * @param  bool $injectAcl              [optional] whether ACL should be
       
   274      *                                      injected when proxying. Default is
       
   275      *                                      true.
       
   276      * @return Zend_View_Helper_Navigation  fluent interface, returns self
       
   277      */
       
   278     public function setInjectAcl($injectAcl = true)
       
   279     {
       
   280         $this->_injectAcl = (bool) $injectAcl;
       
   281         return $this;
       
   282     }
       
   283 
       
   284     /**
       
   285      * Returns whether ACL should be injected when proxying
       
   286      *
       
   287      * @return bool  whether ACL should be injected when proxying
       
   288      */
       
   289     public function getInjectAcl()
       
   290     {
       
   291         return $this->_injectAcl;
       
   292     }
       
   293 
       
   294     /**
       
   295      * Sets whether translator should be injected when proxying
       
   296      *
       
   297      * @param  bool $injectTranslator       [optional] whether translator should
       
   298      *                                      be injected when proxying. Default
       
   299      *                                      is true.
       
   300      * @return Zend_View_Helper_Navigation  fluent interface, returns self
       
   301      */
       
   302     public function setInjectTranslator($injectTranslator = true)
       
   303     {
       
   304         $this->_injectTranslator = (bool) $injectTranslator;
       
   305         return $this;
       
   306     }
       
   307 
       
   308     /**
       
   309      * Returns whether translator should be injected when proxying
       
   310      *
       
   311      * @return bool  whether translator should be injected when proxying
       
   312      */
       
   313     public function getInjectTranslator()
       
   314     {
       
   315         return $this->_injectTranslator;
       
   316     }
       
   317 
       
   318     // Zend_View_Helper_Navigation_Helper:
       
   319 
       
   320     /**
       
   321      * Renders helper
       
   322      *
       
   323      * @param  Zend_Navigation_Container $container  [optional] container to
       
   324      *                                               render. Default is to
       
   325      *                                               render the container
       
   326      *                                               registered in the helper.
       
   327      * @return string                                helper output
       
   328      * @throws Zend_Loader_PluginLoader_Exception    if helper cannot be found
       
   329      * @throws Zend_View_Exception                   if helper doesn't implement
       
   330      *                                               the interface specified in
       
   331      *                                               {@link findHelper()}
       
   332      */
       
   333     public function render(Zend_Navigation_Container $container = null)
       
   334     {
       
   335         $helper = $this->findHelper($this->getDefaultProxy());
       
   336         return $helper->render($container);
       
   337     }
       
   338 }