/monica/monica/vendor/zendframework/zendframework/library/Zend/View/Helper/Navigation.php

https://bitbucket.org/alexandretaz/maniac_divers · PHP · 338 lines · 131 code · 33 blank · 174 comment · 16 complexity · 9d28e9ed83084737216f18e1dc70a9cc MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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. */
  9. namespace Zend\View\Helper;
  10. use Zend\Navigation\AbstractContainer;
  11. use Zend\ServiceManager\ServiceLocatorAwareInterface;
  12. use Zend\View\Exception;
  13. use Zend\View\Helper\Navigation\AbstractHelper as AbstractNavigationHelper;
  14. use Zend\View\Helper\Navigation\HelperInterface as NavigationHelper;
  15. /**
  16. * Proxy helper for retrieving navigational helpers and forwarding calls
  17. */
  18. class Navigation extends AbstractNavigationHelper
  19. {
  20. /**
  21. * View helper namespace
  22. *
  23. * @var string
  24. */
  25. const NS = 'Zend\View\Helper\Navigation';
  26. /**
  27. * @var Navigation\PluginManager
  28. */
  29. protected $plugins;
  30. /**
  31. * Default proxy to use in {@link render()}
  32. *
  33. * @var string
  34. */
  35. protected $defaultProxy = 'menu';
  36. /**
  37. * Indicates whether or not a given helper has been injected
  38. *
  39. * @var array
  40. */
  41. protected $injected = array();
  42. /**
  43. * Whether container should be injected when proxying
  44. *
  45. * @var bool
  46. */
  47. protected $injectContainer = true;
  48. /**
  49. * Whether ACL should be injected when proxying
  50. *
  51. * @var bool
  52. */
  53. protected $injectAcl = true;
  54. /**
  55. * Whether translator should be injected when proxying
  56. *
  57. * @var bool
  58. */
  59. protected $injectTranslator = true;
  60. /**
  61. * Helper entry point
  62. *
  63. * @param string|AbstractContainer $container container to operate on
  64. * @return Navigation
  65. */
  66. public function __invoke($container = null)
  67. {
  68. if (null !== $container) {
  69. $this->setContainer($container);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Magic overload: Proxy to other navigation helpers or the container
  75. *
  76. * Examples of usage from a view script or layout:
  77. * <code>
  78. * // proxy to Menu helper and render container:
  79. * echo $this->navigation()->menu();
  80. *
  81. * // proxy to Breadcrumbs helper and set indentation:
  82. * $this->navigation()->breadcrumbs()->setIndent(8);
  83. *
  84. * // proxy to container and find all pages with 'blog' route:
  85. * $blogPages = $this->navigation()->findAllByRoute('blog');
  86. * </code>
  87. *
  88. * @param string $method helper name or method name in
  89. * container
  90. * @param array $arguments [optional] arguments to pass
  91. * @return mixed returns what the proxied call returns
  92. * @throws \Zend\View\Exception\ExceptionInterface if proxying to a helper, and the
  93. * helper is not an instance of the
  94. * interface specified in
  95. * {@link findHelper()}
  96. * @throws \Zend\Navigation\Exception\ExceptionInterface if method does not exist in container
  97. */
  98. public function __call($method, array $arguments = array())
  99. {
  100. // check if call should proxy to another helper
  101. $helper = $this->findHelper($method, false);
  102. if ($helper) {
  103. if ($helper instanceof ServiceLocatorAwareInterface && $this->getServiceLocator()) {
  104. $helper->setServiceLocator($this->getServiceLocator());
  105. }
  106. return call_user_func_array($helper, $arguments);
  107. }
  108. // default behaviour: proxy call to container
  109. return parent::__call($method, $arguments);
  110. }
  111. /**
  112. * Set manager for retrieving navigation helpers
  113. *
  114. * @param Navigation\PluginManager $plugins
  115. * @return Navigation
  116. */
  117. public function setPluginManager(Navigation\PluginManager $plugins)
  118. {
  119. $renderer = $this->getView();
  120. if ($renderer) {
  121. $plugins->setRenderer($renderer);
  122. }
  123. $this->plugins = $plugins;
  124. return $this;
  125. }
  126. /**
  127. * Retrieve plugin loader for navigation helpers
  128. *
  129. * Lazy-loads an instance of Navigation\HelperLoader if none currently
  130. * registered.
  131. *
  132. * @return Navigation\PluginManager
  133. */
  134. public function getPluginManager()
  135. {
  136. if (null === $this->plugins) {
  137. $this->setPluginManager(new Navigation\PluginManager());
  138. }
  139. return $this->plugins;
  140. }
  141. /**
  142. * Returns the helper matching $proxy
  143. *
  144. * The helper must implement the interface
  145. * {@link Zend\View\Helper\Navigation\Helper}.
  146. *
  147. * @param string $proxy helper name
  148. * @param bool $strict [optional] whether
  149. * exceptions should be
  150. * thrown if something goes
  151. * wrong. Default is true.
  152. * @return \Zend\View\Helper\Navigation\HelperInterface helper instance
  153. * @throws Exception\RuntimeException if $strict is true and
  154. * helper cannot be found
  155. */
  156. public function findHelper($proxy, $strict = true)
  157. {
  158. $plugins = $this->getPluginManager();
  159. if (!$plugins->has($proxy)) {
  160. if ($strict) {
  161. throw new Exception\RuntimeException(sprintf(
  162. 'Failed to find plugin for %s',
  163. $proxy
  164. ));
  165. }
  166. return false;
  167. }
  168. $helper = $plugins->get($proxy);
  169. $class = get_class($helper);
  170. if (!isset($this->injected[$class])) {
  171. $this->inject($helper);
  172. $this->injected[$class] = true;
  173. }
  174. return $helper;
  175. }
  176. /**
  177. * Injects container, ACL, and translator to the given $helper if this
  178. * helper is configured to do so
  179. *
  180. * @param NavigationHelper $helper helper instance
  181. * @return void
  182. */
  183. protected function inject(NavigationHelper $helper)
  184. {
  185. if ($this->getInjectContainer() && !$helper->hasContainer()) {
  186. $helper->setContainer($this->getContainer());
  187. }
  188. if ($this->getInjectAcl()) {
  189. if (!$helper->hasAcl()) {
  190. $helper->setAcl($this->getAcl());
  191. }
  192. if (!$helper->hasRole()) {
  193. $helper->setRole($this->getRole());
  194. }
  195. }
  196. if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
  197. $helper->setTranslator(
  198. $this->getTranslator(), $this->getTranslatorTextDomain()
  199. );
  200. }
  201. }
  202. // Accessors:
  203. /**
  204. * Sets the default proxy to use in {@link render()}
  205. *
  206. * @param string $proxy default proxy
  207. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  208. */
  209. public function setDefaultProxy($proxy)
  210. {
  211. $this->defaultProxy = (string) $proxy;
  212. return $this;
  213. }
  214. /**
  215. * Returns the default proxy to use in {@link render()}
  216. *
  217. * @return string the default proxy to use in {@link render()}
  218. */
  219. public function getDefaultProxy()
  220. {
  221. return $this->defaultProxy;
  222. }
  223. /**
  224. * Sets whether container should be injected when proxying
  225. *
  226. * @param bool $injectContainer [optional] whether container should
  227. * be injected when proxying. Default
  228. * is true.
  229. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  230. */
  231. public function setInjectContainer($injectContainer = true)
  232. {
  233. $this->injectContainer = (bool) $injectContainer;
  234. return $this;
  235. }
  236. /**
  237. * Returns whether container should be injected when proxying
  238. *
  239. * @return bool whether container should be injected when proxying
  240. */
  241. public function getInjectContainer()
  242. {
  243. return $this->injectContainer;
  244. }
  245. /**
  246. * Sets whether ACL should be injected when proxying
  247. *
  248. * @param bool $injectAcl [optional] whether ACL should be
  249. * injected when proxying. Default is
  250. * true.
  251. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  252. */
  253. public function setInjectAcl($injectAcl = true)
  254. {
  255. $this->injectAcl = (bool) $injectAcl;
  256. return $this;
  257. }
  258. /**
  259. * Returns whether ACL should be injected when proxying
  260. *
  261. * @return bool whether ACL should be injected when proxying
  262. */
  263. public function getInjectAcl()
  264. {
  265. return $this->injectAcl;
  266. }
  267. /**
  268. * Sets whether translator should be injected when proxying
  269. *
  270. * @param bool $injectTranslator [optional] whether translator should
  271. * be injected when proxying. Default
  272. * is true.
  273. * @return Navigation fluent interface, returns self
  274. */
  275. public function setInjectTranslator($injectTranslator = true)
  276. {
  277. $this->injectTranslator = (bool) $injectTranslator;
  278. return $this;
  279. }
  280. /**
  281. * Returns whether translator should be injected when proxying
  282. *
  283. * @return bool whether translator should be injected when proxying
  284. */
  285. public function getInjectTranslator()
  286. {
  287. return $this->injectTranslator;
  288. }
  289. // Zend\View\Helper\Navigation\Helper:
  290. /**
  291. * Renders helper
  292. *
  293. * @param \Zend\Navigation\AbstractContainer $container [optional] container to
  294. * render. Default is to
  295. * render the container
  296. * registered in the helper.
  297. * @return string helper output
  298. * @throws Exception\RuntimeException if helper cannot be found
  299. */
  300. public function render($container = null)
  301. {
  302. $helper = $this->findHelper($this->getDefaultProxy());
  303. return $helper->render($container);
  304. }
  305. }