Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / View / Helper / Navigation.php
blobcaf0f71e8b5ff12186bff3247564e3983a779402
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-2013 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\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;
18 /**
19 * Proxy helper for retrieving navigational helpers and forwarding calls
21 class Navigation extends AbstractNavigationHelper
23 /**
24 * View helper namespace
26 * @var string
28 const NS = 'Zend\View\Helper\Navigation';
30 /**
31 * Default proxy to use in {@link render()}
33 * @var string
35 protected $defaultProxy = 'menu';
37 /**
38 * Indicates whether or not a given helper has been injected
40 * @var array
42 protected $injected = array();
44 /**
45 * Whether ACL should be injected when proxying
47 * @var bool
49 protected $injectAcl = true;
51 /**
52 * Whether container should be injected when proxying
54 * @var bool
56 protected $injectContainer = true;
58 /**
59 * Whether translator should be injected when proxying
61 * @var bool
63 protected $injectTranslator = true;
65 /**
66 * @var Navigation\PluginManager
68 protected $plugins;
70 /**
71 * Helper entry point
73 * @param string|AbstractContainer $container container to operate on
74 * @return Navigation
76 public function __invoke($container = null)
78 if (null !== $container) {
79 $this->setContainer($container);
82 return $this;
85 /**
86 * Magic overload: Proxy to other navigation helpers or the container
88 * Examples of usage from a view script or layout:
89 * <code>
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');
98 * </code>
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);
113 if ($helper) {
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);
125 * Renders helper
127 * @param AbstractContainer $container
128 * @return string
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)) {
153 if ($strict) {
154 throw new Exception\RuntimeException(sprintf(
155 'Failed to find plugin for %s',
156 $proxy
159 return false;
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;
170 } else {
171 if ($this->getInjectContainer()) {
172 $helper->setContainer($container);
176 return $helper;
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
184 * @return void
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
212 * @return Navigation
214 public function setDefaultProxy($proxy)
216 $this->defaultProxy = (string) $proxy;
217 return $this;
221 * Returns the default proxy to use in {@link render()}
223 * @return string
225 public function getDefaultProxy()
227 return $this->defaultProxy;
231 * Sets whether container should be injected when proxying
233 * @param bool $injectContainer
234 * @return Navigation
236 public function setInjectContainer($injectContainer = true)
238 $this->injectContainer = (bool) $injectContainer;
239 return $this;
243 * Returns whether container should be injected when proxying
245 * @return bool
247 public function getInjectContainer()
249 return $this->injectContainer;
253 * Sets whether ACL should be injected when proxying
255 * @param bool $injectAcl
256 * @return Navigation
258 public function setInjectAcl($injectAcl = true)
260 $this->injectAcl = (bool) $injectAcl;
261 return $this;
265 * Returns whether ACL should be injected when proxying
267 * @return bool
269 public function getInjectAcl()
271 return $this->injectAcl;
275 * Sets whether translator should be injected when proxying
277 * @param bool $injectTranslator
278 * @return Navigation
280 public function setInjectTranslator($injectTranslator = true)
282 $this->injectTranslator = (bool) $injectTranslator;
283 return $this;
287 * Returns whether translator should be injected when proxying
289 * @return bool
291 public function getInjectTranslator()
293 return $this->injectTranslator;
297 * Set manager for retrieving navigation helpers
299 * @param Navigation\PluginManager $plugins
300 * @return Navigation
302 public function setPluginManager(Navigation\PluginManager $plugins)
304 $renderer = $this->getView();
305 if ($renderer) {
306 $plugins->setRenderer($renderer);
308 $this->plugins = $plugins;
310 return $this;
314 * Retrieve plugin loader for navigation helpers
316 * Lazy-loads an instance of Navigation\HelperLoader if none currently
317 * registered.
319 * @return Navigation\PluginManager
321 public function getPluginManager()
323 if (null === $this->plugins) {
324 $this->setPluginManager(new Navigation\PluginManager());
327 return $this->plugins;