PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/View/Helper/Navigation.php

https://github.com/lcf/zf2
PHP | 340 lines | 133 code | 33 blank | 174 comment | 16 complexity | 126b53b9a6cbaea0d1fe6e08f9f7d935 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. $container = $this->getContainer();
  170. $hash = spl_object_hash($container);
  171. if (!isset($this->injected[$hash])) {
  172. $helper->setContainer();
  173. $this->inject($helper);
  174. $this->injected[$hash] = true;
  175. }
  176. return $helper;
  177. }
  178. /**
  179. * Injects container, ACL, and translator to the given $helper if this
  180. * helper is configured to do so
  181. *
  182. * @param NavigationHelper $helper helper instance
  183. * @return void
  184. */
  185. protected function inject(NavigationHelper $helper)
  186. {
  187. if ($this->getInjectContainer() && !$helper->hasContainer()) {
  188. $helper->setContainer($this->getContainer());
  189. }
  190. if ($this->getInjectAcl()) {
  191. if (!$helper->hasAcl()) {
  192. $helper->setAcl($this->getAcl());
  193. }
  194. if (!$helper->hasRole()) {
  195. $helper->setRole($this->getRole());
  196. }
  197. }
  198. if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
  199. $helper->setTranslator(
  200. $this->getTranslator(), $this->getTranslatorTextDomain()
  201. );
  202. }
  203. }
  204. // Accessors:
  205. /**
  206. * Sets the default proxy to use in {@link render()}
  207. *
  208. * @param string $proxy default proxy
  209. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  210. */
  211. public function setDefaultProxy($proxy)
  212. {
  213. $this->defaultProxy = (string) $proxy;
  214. return $this;
  215. }
  216. /**
  217. * Returns the default proxy to use in {@link render()}
  218. *
  219. * @return string the default proxy to use in {@link render()}
  220. */
  221. public function getDefaultProxy()
  222. {
  223. return $this->defaultProxy;
  224. }
  225. /**
  226. * Sets whether container should be injected when proxying
  227. *
  228. * @param bool $injectContainer [optional] whether container should
  229. * be injected when proxying. Default
  230. * is true.
  231. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  232. */
  233. public function setInjectContainer($injectContainer = true)
  234. {
  235. $this->injectContainer = (bool) $injectContainer;
  236. return $this;
  237. }
  238. /**
  239. * Returns whether container should be injected when proxying
  240. *
  241. * @return bool whether container should be injected when proxying
  242. */
  243. public function getInjectContainer()
  244. {
  245. return $this->injectContainer;
  246. }
  247. /**
  248. * Sets whether ACL should be injected when proxying
  249. *
  250. * @param bool $injectAcl [optional] whether ACL should be
  251. * injected when proxying. Default is
  252. * true.
  253. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  254. */
  255. public function setInjectAcl($injectAcl = true)
  256. {
  257. $this->injectAcl = (bool) $injectAcl;
  258. return $this;
  259. }
  260. /**
  261. * Returns whether ACL should be injected when proxying
  262. *
  263. * @return bool whether ACL should be injected when proxying
  264. */
  265. public function getInjectAcl()
  266. {
  267. return $this->injectAcl;
  268. }
  269. /**
  270. * Sets whether translator should be injected when proxying
  271. *
  272. * @param bool $injectTranslator [optional] whether translator should
  273. * be injected when proxying. Default
  274. * is true.
  275. * @return Navigation fluent interface, returns self
  276. */
  277. public function setInjectTranslator($injectTranslator = true)
  278. {
  279. $this->injectTranslator = (bool) $injectTranslator;
  280. return $this;
  281. }
  282. /**
  283. * Returns whether translator should be injected when proxying
  284. *
  285. * @return bool whether translator should be injected when proxying
  286. */
  287. public function getInjectTranslator()
  288. {
  289. return $this->injectTranslator;
  290. }
  291. // Zend\View\Helper\Navigation\Helper:
  292. /**
  293. * Renders helper
  294. *
  295. * @param \Zend\Navigation\AbstractContainer $container [optional] container to
  296. * render. Default is to
  297. * render the container
  298. * registered in the helper.
  299. * @return string helper output
  300. * @throws Exception\RuntimeException if helper cannot be found
  301. */
  302. public function render($container = null)
  303. {
  304. $helper = $this->findHelper($this->getDefaultProxy());
  305. return $helper->render($container);
  306. }
  307. }