PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/mrbanzai/zf2
PHP | 374 lines | 133 code | 37 blank | 204 comment | 15 complexity | 47b00ea11ec5d0ba9019553be44429de MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\View\Helper;
  25. use Zend\Loader\ShortNameLocator,
  26. Zend\Loader\PluginClassLoader,
  27. Zend\Navigation\Container,
  28. Zend\View\Helper\Navigation\AbstractHelper as AbstractNavigationHelper,
  29. Zend\View\Helper\Navigation\Helper as NavigationHelper,
  30. Zend\View\Exception;
  31. /**
  32. * Proxy helper for retrieving navigational helpers and forwarding calls
  33. *
  34. * @uses \Zend\View\Exception
  35. * @uses \Zend\View\Helper\Navigation\AbstractHelper
  36. * @category Zend
  37. * @package Zend_View
  38. * @subpackage Helper
  39. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Navigation extends AbstractNavigationHelper
  43. {
  44. /**
  45. * View helper namespace
  46. *
  47. * @var string
  48. */
  49. const NS = 'Zend\View\Helper\Navigation';
  50. /**
  51. * @var ShortNameLocator
  52. */
  53. protected $loader;
  54. /**
  55. * Default proxy to use in {@link render()}
  56. *
  57. * @var string
  58. */
  59. protected $_defaultProxy = 'menu';
  60. /**
  61. * Contains references to proxied helpers
  62. *
  63. * @var array
  64. */
  65. protected $_helpers = array();
  66. /**
  67. * Whether container should be injected when proxying
  68. *
  69. * @var bool
  70. */
  71. protected $_injectContainer = true;
  72. /**
  73. * Whether ACL should be injected when proxying
  74. *
  75. * @var bool
  76. */
  77. protected $_injectAcl = true;
  78. /**
  79. * Whether translator should be injected when proxying
  80. *
  81. * @var bool
  82. */
  83. protected $_injectTranslator = true;
  84. /**
  85. * Helper entry point
  86. *
  87. * @param \Zend\Navigation\Container $container [optional] container to
  88. * operate on
  89. * @return \Zend\View\Helper\Navigation fluent interface, returns
  90. * self
  91. */
  92. public function __invoke(Container $container = null)
  93. {
  94. if (null !== $container) {
  95. $this->setContainer($container);
  96. }
  97. return $this;
  98. }
  99. /**
  100. * Magic overload: Proxy to other navigation helpers or the container
  101. *
  102. * Examples of usage from a view script or layout:
  103. * <code>
  104. * // proxy to Menu helper and render container:
  105. * echo $this->navigation()->menu();
  106. *
  107. * // proxy to Breadcrumbs helper and set indentation:
  108. * $this->navigation()->breadcrumbs()->setIndent(8);
  109. *
  110. * // proxy to container and find all pages with 'blog' route:
  111. * $blogPages = $this->navigation()->findAllByRoute('blog');
  112. * </code>
  113. *
  114. * @param string $method helper name or method name in
  115. * container
  116. * @param array $arguments [optional] arguments to pass
  117. * @return mixed returns what the proxied call returns
  118. * @throws \Zend\View\Exception if proxying to a helper, and the
  119. * helper is not an instance of the
  120. * interface specified in
  121. * {@link findHelper()}
  122. * @throws \Zend\Navigation\Exception if method does not exist in container
  123. */
  124. public function __call($method, array $arguments = array())
  125. {
  126. // check if call should proxy to another helper
  127. if ($helper = $this->findHelper($method, false)) {
  128. return call_user_func_array($helper, $arguments);
  129. }
  130. // default behaviour: proxy call to container
  131. return parent::__call($method, $arguments);
  132. }
  133. /**
  134. * Set plugin loader for retrieving navigation helpers
  135. *
  136. * @param ShortNameLocator $loader
  137. * @return void
  138. */
  139. public function setPluginLoader(ShortNameLocator $loader)
  140. {
  141. $this->loader = $loader;
  142. return $this;
  143. }
  144. /**
  145. * Retrieve plugin loader for navigation helpers
  146. *
  147. * Lazy-loads an instance of Navigation\HelperLoader if none currently
  148. * registered.
  149. *
  150. * @return ShortNameLocator
  151. */
  152. public function getPluginLoader()
  153. {
  154. if (null === $this->loader) {
  155. $this->setPluginLoader(new Navigation\HelperLoader());
  156. }
  157. return $this->loader;
  158. }
  159. /**
  160. * Returns the helper matching $proxy
  161. *
  162. * The helper must implement the interface
  163. * {@link Zend\View\Helper\Navigation\Helper}.
  164. *
  165. * @param string $proxy helper name
  166. * @param bool $strict [optional] whether
  167. * exceptions should be
  168. * thrown if something goes
  169. * wrong. Default is true.
  170. * @return \Zend\View\Helper\Navigation\Helper helper instance
  171. * @throws \Zend\Loader\PluginLoader\Exception if $strict is true and
  172. * helper cannot be found
  173. * @throws Exception\InvalidArgumentException if $strict is true and
  174. * helper does not implement the specified interface
  175. */
  176. public function findHelper($proxy, $strict = true)
  177. {
  178. if (isset($this->_helpers[$proxy])) {
  179. return $this->_helpers[$proxy];
  180. }
  181. $loader = $this->getPluginLoader();
  182. if ($strict) {
  183. $class = $loader->load($proxy);
  184. } else {
  185. try {
  186. $class = $loader->load($proxy);
  187. } catch (\Zend\Loader\Exception $e) {
  188. return null;
  189. }
  190. }
  191. $helper = new $class();
  192. if (!$helper instanceof AbstractNavigationHelper) {
  193. if ($strict) {
  194. throw new Exception\InvalidArgumentException(sprintf(
  195. 'Proxy helper "%s" is not an instance of ' .
  196. 'Zend\View\Helper\Navigation\Helper',
  197. get_class($helper)
  198. ));
  199. }
  200. return null;
  201. }
  202. $helper->setView($this->view);
  203. $this->_inject($helper);
  204. $this->_helpers[$proxy] = $helper;
  205. return $helper;
  206. }
  207. /**
  208. * Injects container, ACL, and translator to the given $helper if this
  209. * helper is configured to do so
  210. *
  211. * @param \Zend\View\Helper\Navigation\Helper $helper helper instance
  212. * @return void
  213. */
  214. protected function _inject(NavigationHelper $helper)
  215. {
  216. if ($this->getInjectContainer() && !$helper->hasContainer()) {
  217. $helper->setContainer($this->getContainer());
  218. }
  219. if ($this->getInjectAcl()) {
  220. if (!$helper->hasAcl()) {
  221. $helper->setAcl($this->getAcl());
  222. }
  223. if (!$helper->hasRole()) {
  224. $helper->setRole($this->getRole());
  225. }
  226. }
  227. if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
  228. $helper->setTranslator($this->getTranslator());
  229. }
  230. }
  231. // Accessors:
  232. /**
  233. * Sets the default proxy to use in {@link render()}
  234. *
  235. * @param string $proxy default proxy
  236. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  237. */
  238. public function setDefaultProxy($proxy)
  239. {
  240. $this->_defaultProxy = (string) $proxy;
  241. return $this;
  242. }
  243. /**
  244. * Returns the default proxy to use in {@link render()}
  245. *
  246. * @return string the default proxy to use in {@link render()}
  247. */
  248. public function getDefaultProxy()
  249. {
  250. return $this->_defaultProxy;
  251. }
  252. /**
  253. * Sets whether container should be injected when proxying
  254. *
  255. * @param bool $injectContainer [optional] whether container should
  256. * be injected when proxying. Default
  257. * is true.
  258. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  259. */
  260. public function setInjectContainer($injectContainer = true)
  261. {
  262. $this->_injectContainer = (bool) $injectContainer;
  263. return $this;
  264. }
  265. /**
  266. * Returns whether container should be injected when proxying
  267. *
  268. * @return bool whether container should be injected when proxying
  269. */
  270. public function getInjectContainer()
  271. {
  272. return $this->_injectContainer;
  273. }
  274. /**
  275. * Sets whether ACL should be injected when proxying
  276. *
  277. * @param bool $injectAcl [optional] whether ACL should be
  278. * injected when proxying. Default is
  279. * true.
  280. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  281. */
  282. public function setInjectAcl($injectAcl = true)
  283. {
  284. $this->_injectAcl = (bool) $injectAcl;
  285. return $this;
  286. }
  287. /**
  288. * Returns whether ACL should be injected when proxying
  289. *
  290. * @return bool whether ACL should be injected when proxying
  291. */
  292. public function getInjectAcl()
  293. {
  294. return $this->_injectAcl;
  295. }
  296. /**
  297. * Sets whether translator should be injected when proxying
  298. *
  299. * @param bool $injectTranslator [optional] whether translator should
  300. * be injected when proxying. Default
  301. * is true.
  302. * @return \Zend\View\Helper\Navigation fluent interface, returns self
  303. */
  304. public function setInjectTranslator($injectTranslator = true)
  305. {
  306. $this->_injectTranslator = (bool) $injectTranslator;
  307. return $this;
  308. }
  309. /**
  310. * Returns whether translator should be injected when proxying
  311. *
  312. * @return bool whether translator should be injected when proxying
  313. */
  314. public function getInjectTranslator()
  315. {
  316. return $this->_injectTranslator;
  317. }
  318. // Zend\View\Helper\Navigation\Helper:
  319. /**
  320. * Renders helper
  321. *
  322. * @param \Zend\Navigation\Container $container [optional] container to
  323. * render. Default is to
  324. * render the container
  325. * registered in the helper.
  326. * @return string helper output
  327. * @throws \Zend\Loader\PluginLoader\Exception if helper cannot be found
  328. * @throws \Zend\View\Exception if helper doesn't implement
  329. * the interface specified in
  330. * {@link findHelper()}
  331. */
  332. public function render(Container $container = null)
  333. {
  334. $helper = $this->findHelper($this->getDefaultProxy());
  335. return $helper->render($container);
  336. }
  337. }