3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper
;
12 use Zend\Navigation\AbstractContainer
;
13 use Zend\ServiceManager\ServiceLocatorAwareInterface
;
14 use Zend\View\Exception
;
15 use Zend\View\Helper\Navigation\AbstractHelper
as AbstractNavigationHelper
;
16 use Zend\View\Helper\Navigation\HelperInterface
as NavigationHelper
;
19 * Proxy helper for retrieving navigational helpers and forwarding calls
21 class Navigation
extends AbstractNavigationHelper
24 * View helper namespace
28 const NS
= 'Zend\View\Helper\Navigation';
31 * Default proxy to use in {@link render()}
35 protected $defaultProxy = 'menu';
38 * Indicates whether or not a given helper has been injected
42 protected $injected = array();
45 * Whether ACL should be injected when proxying
49 protected $injectAcl = true;
52 * Whether container should be injected when proxying
56 protected $injectContainer = true;
59 * Whether translator should be injected when proxying
63 protected $injectTranslator = true;
66 * @var Navigation\PluginManager
73 * @param string|AbstractContainer $container container to operate on
76 public function __invoke($container = null)
78 if (null !== $container) {
79 $this->setContainer($container);
86 * Magic overload: Proxy to other navigation helpers or the container
88 * Examples of usage from a view script or layout:
90 * // proxy to Menu helper and render container:
91 * echo $this->navigation()->menu();
93 * // proxy to Breadcrumbs helper and set indentation:
94 * $this->navigation()->breadcrumbs()->setIndent(8);
96 * // proxy to container and find all pages with 'blog' route:
97 * $blogPages = $this->navigation()->findAllByRoute('blog');
100 * @param string $method helper name or method name in container
101 * @param array $arguments [optional] arguments to pass
102 * @throws \Zend\View\Exception\ExceptionInterface if proxying to a helper, and the
103 * helper is not an instance of the
104 * interface specified in
105 * {@link findHelper()}
106 * @throws \Zend\Navigation\Exception\ExceptionInterface if method does not exist in container
107 * @return mixed returns what the proxied call returns
109 public function __call($method, array $arguments = array())
111 // check if call should proxy to another helper
112 $helper = $this->findHelper($method, false);
114 if ($helper instanceof ServiceLocatorAwareInterface
&& $this->getServiceLocator()) {
115 $helper->setServiceLocator($this->getServiceLocator());
117 return call_user_func_array($helper, $arguments);
120 // default behaviour: proxy call to container
121 return parent
::__call($method, $arguments);
127 * @param AbstractContainer $container
129 * @throws Exception\RuntimeException
131 public function render($container = null)
133 return $this->findHelper($this->getDefaultProxy())->render($container);
137 * Returns the helper matching $proxy
139 * The helper must implement the interface
140 * {@link Zend\View\Helper\Navigation\Helper}.
142 * @param string $proxy helper name
143 * @param bool $strict [optional] whether exceptions should be
144 * thrown if something goes
145 * wrong. Default is true.
146 * @throws Exception\RuntimeException if $strict is true and helper cannot be found
147 * @return \Zend\View\Helper\Navigation\HelperInterface helper instance
149 public function findHelper($proxy, $strict = true)
151 $plugins = $this->getPluginManager();
152 if (!$plugins->has($proxy)) {
154 throw new Exception\
RuntimeException(sprintf(
155 'Failed to find plugin for %s',
162 $helper = $plugins->get($proxy);
163 $container = $this->getContainer();
164 $hash = spl_object_hash($container) . spl_object_hash($helper);
166 if (!isset($this->injected
[$hash])) {
167 $helper->setContainer();
168 $this->inject($helper);
169 $this->injected
[$hash] = true;
171 if ($this->getInjectContainer()) {
172 $helper->setContainer($container);
180 * Injects container, ACL, and translator to the given $helper if this
181 * helper is configured to do so
183 * @param NavigationHelper $helper helper instance
186 protected function inject(NavigationHelper
$helper)
188 if ($this->getInjectContainer() && !$helper->hasContainer()) {
189 $helper->setContainer($this->getContainer());
192 if ($this->getInjectAcl()) {
193 if (!$helper->hasAcl()) {
194 $helper->setAcl($this->getAcl());
196 if (!$helper->hasRole()) {
197 $helper->setRole($this->getRole());
201 if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
202 $helper->setTranslator(
203 $this->getTranslator(), $this->getTranslatorTextDomain()
209 * Sets the default proxy to use in {@link render()}
211 * @param string $proxy default proxy
214 public function setDefaultProxy($proxy)
216 $this->defaultProxy
= (string) $proxy;
221 * Returns the default proxy to use in {@link render()}
225 public function getDefaultProxy()
227 return $this->defaultProxy
;
231 * Sets whether container should be injected when proxying
233 * @param bool $injectContainer
236 public function setInjectContainer($injectContainer = true)
238 $this->injectContainer
= (bool) $injectContainer;
243 * Returns whether container should be injected when proxying
247 public function getInjectContainer()
249 return $this->injectContainer
;
253 * Sets whether ACL should be injected when proxying
255 * @param bool $injectAcl
258 public function setInjectAcl($injectAcl = true)
260 $this->injectAcl
= (bool) $injectAcl;
265 * Returns whether ACL should be injected when proxying
269 public function getInjectAcl()
271 return $this->injectAcl
;
275 * Sets whether translator should be injected when proxying
277 * @param bool $injectTranslator
280 public function setInjectTranslator($injectTranslator = true)
282 $this->injectTranslator
= (bool) $injectTranslator;
287 * Returns whether translator should be injected when proxying
291 public function getInjectTranslator()
293 return $this->injectTranslator
;
297 * Set manager for retrieving navigation helpers
299 * @param Navigation\PluginManager $plugins
302 public function setPluginManager(Navigation\PluginManager
$plugins)
304 $renderer = $this->getView();
306 $plugins->setRenderer($renderer);
308 $this->plugins
= $plugins;
314 * Retrieve plugin loader for navigation helpers
316 * Lazy-loads an instance of Navigation\HelperLoader if none currently
319 * @return Navigation\PluginManager
321 public function getPluginManager()
323 if (null === $this->plugins
) {
324 $this->setPluginManager(new Navigation\
PluginManager());
327 return $this->plugins
;