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