upgrade zend (#1559)
[openemr.git] / vendor / zendframework / zend-view / src / Helper / Navigation.php
blob5119be28ff3a4bc44abb2806a1afe2879b75eaef
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\View\Helper;
12 use Zend\Navigation\AbstractContainer;
13 use Zend\View\Exception;
14 use Zend\View\Helper\Navigation\AbstractHelper as AbstractNavigationHelper;
15 use Zend\View\Helper\Navigation\HelperInterface as NavigationHelper;
16 use Zend\View\Renderer\RendererInterface as Renderer;
18 /**
19 * Proxy helper for retrieving navigational helpers and forwarding calls
21 * @method \Zend\View\Helper\Navigation\Breadcrumbs breadcrumbs($container = null)
22 * @method \Zend\View\Helper\Navigation\Links links($container = null)
23 * @method \Zend\View\Helper\Navigation\Menu menu($container = null)
24 * @method \Zend\View\Helper\Navigation\Sitemap sitemap($container = null)
26 class Navigation extends AbstractNavigationHelper
28 /**
29 * View helper namespace
31 * @var string
33 const NS = 'Zend\View\Helper\Navigation';
35 /**
36 * Default proxy to use in {@link render()}
38 * @var string
40 protected $defaultProxy = 'menu';
42 /**
43 * Indicates whether or not a given helper has been injected
45 * @var array
47 protected $injected = [];
49 /**
50 * Whether ACL should be injected when proxying
52 * @var bool
54 protected $injectAcl = true;
56 /**
57 * Whether container should be injected when proxying
59 * @var bool
61 protected $injectContainer = true;
63 /**
64 * Whether translator should be injected when proxying
66 * @var bool
68 protected $injectTranslator = true;
70 /**
71 * @var Navigation\PluginManager
73 protected $plugins;
75 /**
76 * Helper entry point
78 * @param string|AbstractContainer $container container to operate on
79 * @return Navigation
81 public function __invoke($container = null)
83 if (null !== $container) {
84 $this->setContainer($container);
87 return $this;
90 /**
91 * Magic overload: Proxy to other navigation helpers or the container
93 * Examples of usage from a view script or layout:
94 * <code>
95 * // proxy to Menu helper and render container:
96 * echo $this->navigation()->menu();
98 * // proxy to Breadcrumbs helper and set indentation:
99 * $this->navigation()->breadcrumbs()->setIndent(8);
101 * // proxy to container and find all pages with 'blog' route:
102 * $blogPages = $this->navigation()->findAllByRoute('blog');
103 * </code>
105 * @param string $method helper name or method name in container
106 * @param array $arguments [optional] arguments to pass
107 * @throws \Zend\View\Exception\ExceptionInterface if proxying to a helper, and the
108 * helper is not an instance of the
109 * interface specified in
110 * {@link findHelper()}
111 * @throws \Zend\Navigation\Exception\ExceptionInterface if method does not exist in container
112 * @return mixed returns what the proxied call returns
114 public function __call($method, array $arguments = [])
116 // check if call should proxy to another helper
117 $helper = $this->findHelper($method, false);
118 if ($helper) {
119 if (method_exists($helper, 'setServiceLocator') && $this->getServiceLocator()) {
120 $helper->setServiceLocator($this->getServiceLocator());
122 return call_user_func_array($helper, $arguments);
125 // default behaviour: proxy call to container
126 return parent::__call($method, $arguments);
130 * Renders helper
132 * @param AbstractContainer $container
133 * @return string
134 * @throws Exception\RuntimeException
136 public function render($container = null)
138 return $this->findHelper($this->getDefaultProxy())->render($container);
142 * Returns the helper matching $proxy
144 * The helper must implement the interface
145 * {@link Zend\View\Helper\Navigation\Helper}.
147 * @param string $proxy helper name
148 * @param bool $strict [optional] whether exceptions should be
149 * thrown if something goes
150 * wrong. Default is true.
151 * @throws Exception\RuntimeException if $strict is true and helper cannot be found
152 * @return \Zend\View\Helper\Navigation\HelperInterface helper instance
154 public function findHelper($proxy, $strict = true)
156 $plugins = $this->getPluginManager();
157 if (! $plugins->has($proxy)) {
158 if ($strict) {
159 throw new Exception\RuntimeException(sprintf(
160 'Failed to find plugin for %s',
161 $proxy
164 return false;
167 $helper = $plugins->get($proxy);
168 $container = $this->getContainer();
169 $hash = spl_object_hash($container) . spl_object_hash($helper);
171 if (! isset($this->injected[$hash])) {
172 $helper->setContainer();
173 $this->inject($helper);
174 $this->injected[$hash] = true;
175 } else {
176 if ($this->getInjectContainer()) {
177 $helper->setContainer($container);
181 return $helper;
185 * Injects container, ACL, and translator to the given $helper if this
186 * helper is configured to do so
188 * @param NavigationHelper $helper helper instance
189 * @return void
191 protected function inject(NavigationHelper $helper)
193 if ($this->getInjectContainer() && ! $helper->hasContainer()) {
194 $helper->setContainer($this->getContainer());
197 if ($this->getInjectAcl()) {
198 if (! $helper->hasAcl()) {
199 $helper->setAcl($this->getAcl());
201 if (! $helper->hasRole()) {
202 $helper->setRole($this->getRole());
206 if ($this->getInjectTranslator() && ! $helper->hasTranslator()) {
207 $helper->setTranslator(
208 $this->getTranslator(),
209 $this->getTranslatorTextDomain()
215 * Sets the default proxy to use in {@link render()}
217 * @param string $proxy default proxy
218 * @return Navigation
220 public function setDefaultProxy($proxy)
222 $this->defaultProxy = (string) $proxy;
223 return $this;
227 * Returns the default proxy to use in {@link render()}
229 * @return string
231 public function getDefaultProxy()
233 return $this->defaultProxy;
237 * Sets whether container should be injected when proxying
239 * @param bool $injectContainer
240 * @return Navigation
242 public function setInjectContainer($injectContainer = true)
244 $this->injectContainer = (bool) $injectContainer;
245 return $this;
249 * Returns whether container should be injected when proxying
251 * @return bool
253 public function getInjectContainer()
255 return $this->injectContainer;
259 * Sets whether ACL should be injected when proxying
261 * @param bool $injectAcl
262 * @return Navigation
264 public function setInjectAcl($injectAcl = true)
266 $this->injectAcl = (bool) $injectAcl;
267 return $this;
271 * Returns whether ACL should be injected when proxying
273 * @return bool
275 public function getInjectAcl()
277 return $this->injectAcl;
281 * Sets whether translator should be injected when proxying
283 * @param bool $injectTranslator
284 * @return Navigation
286 public function setInjectTranslator($injectTranslator = true)
288 $this->injectTranslator = (bool) $injectTranslator;
289 return $this;
293 * Returns whether translator should be injected when proxying
295 * @return bool
297 public function getInjectTranslator()
299 return $this->injectTranslator;
303 * Set manager for retrieving navigation helpers
305 * @param Navigation\PluginManager $plugins
306 * @return Navigation
308 public function setPluginManager(Navigation\PluginManager $plugins)
310 $renderer = $this->getView();
311 if ($renderer) {
312 $plugins->setRenderer($renderer);
314 $this->plugins = $plugins;
316 return $this;
320 * Retrieve plugin loader for navigation helpers
322 * Lazy-loads an instance of Navigation\HelperLoader if none currently
323 * registered.
325 * @return Navigation\PluginManager
327 public function getPluginManager()
329 if (null === $this->plugins) {
330 $this->setPluginManager(new Navigation\PluginManager($this->getServiceLocator()));
333 return $this->plugins;
337 * Set the View object
339 * @param Renderer $view
340 * @return self
342 public function setView(Renderer $view)
344 parent::setView($view);
345 if ($view && $this->plugins) {
346 $this->plugins->setRenderer($view);
348 return $this;