Navigation.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_View_Helper_Navigation_HelperAbstract
  24. */
  25. require_once 'Zend/View/Helper/Navigation/HelperAbstract.php';
  26. /**
  27. * Proxy helper for retrieving navigational helpers and forwarding calls
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage Helper
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @method Zend_View_Helper_Navigation_Breadcrumbs breadcrumbs
  35. * @method Zend_View_Helper_Navigation_Links links
  36. * @method Zend_View_Helper_Navigation_Menu menu
  37. * @method Zend_View_Helper_Navigation_Sitemap sitemap
  38. */
  39. class Zend_View_Helper_Navigation
  40. extends Zend_View_Helper_Navigation_HelperAbstract
  41. {
  42. /**
  43. * View helper namespace
  44. *
  45. * @var string
  46. */
  47. const NS = 'Zend_View_Helper_Navigation';
  48. /**
  49. * Default proxy to use in {@link render()}
  50. *
  51. * @var string
  52. */
  53. protected $_defaultProxy = 'menu';
  54. /**
  55. * Contains references to proxied helpers
  56. *
  57. * @var array
  58. */
  59. protected $_helpers = array();
  60. /**
  61. * Whether container should be injected when proxying
  62. *
  63. * @var bool
  64. */
  65. protected $_injectContainer = true;
  66. /**
  67. * Whether ACL should be injected when proxying
  68. *
  69. * @var bool
  70. */
  71. protected $_injectAcl = true;
  72. /**
  73. * Whether translator should be injected when proxying
  74. *
  75. * @var bool
  76. */
  77. protected $_injectTranslator = true;
  78. /**
  79. * Helper entry point
  80. *
  81. * @param Zend_Navigation_Container $container [optional] container to
  82. * operate on
  83. * @return Zend_View_Helper_Navigation fluent interface, returns
  84. * self
  85. */
  86. public function navigation(Zend_Navigation_Container $container = null)
  87. {
  88. if (null !== $container) {
  89. $this->setContainer($container);
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Magic overload: Proxy to other navigation helpers or the container
  95. *
  96. * Examples of usage from a view script or layout:
  97. * <code>
  98. * // proxy to Menu helper and render container:
  99. * echo $this->navigation()->menu();
  100. *
  101. * // proxy to Breadcrumbs helper and set indentation:
  102. * $this->navigation()->breadcrumbs()->setIndent(8);
  103. *
  104. * // proxy to container and find all pages with 'blog' route:
  105. * $blogPages = $this->navigation()->findAllByRoute('blog');
  106. * </code>
  107. *
  108. * @param string $method helper name or method name in
  109. * container
  110. * @param array $arguments [optional] arguments to pass
  111. * @return mixed returns what the proxied call returns
  112. * @throws Zend_View_Exception if proxying to a helper, and the
  113. * helper is not an instance of the
  114. * interface specified in
  115. * {@link findHelper()}
  116. * @throws Zend_Navigation_Exception if method does not exist in container
  117. */
  118. public function __call($method, array $arguments = array())
  119. {
  120. // check if call should proxy to another helper
  121. if ($helper = $this->findHelper($method, false)) {
  122. return call_user_func_array(array($helper, $method), $arguments);
  123. }
  124. // default behaviour: proxy call to container
  125. return parent::__call($method, $arguments);
  126. }
  127. /**
  128. * Returns the helper matching $proxy
  129. *
  130. * The helper must implement the interface
  131. * {@link Zend_View_Helper_Navigation_Helper}.
  132. *
  133. * @param string $proxy helper name
  134. * @param bool $strict [optional] whether
  135. * exceptions should be
  136. * thrown if something goes
  137. * wrong. Default is true.
  138. * @return Zend_View_Helper_Navigation_Helper helper instance
  139. * @throws Zend_Loader_PluginLoader_Exception if $strict is true and
  140. * helper cannot be found
  141. * @throws Zend_View_Exception if $strict is true and
  142. * helper does not implement
  143. * the specified interface
  144. */
  145. public function findHelper($proxy, $strict = true)
  146. {
  147. if (isset($this->_helpers[$proxy])) {
  148. return $this->_helpers[$proxy];
  149. }
  150. if (!$this->view->getPluginLoader('helper')->getPaths(self::NS)) {
  151. // Add navigation helper path at the beginning
  152. $paths = $this->view->getHelperPaths();
  153. $this->view->setHelperPath(null);
  154. $this->view->addHelperPath(
  155. str_replace('_', '/', self::NS),
  156. self::NS);
  157. foreach ($paths as $ns => $path) {
  158. $this->view->addHelperPath($path, $ns);
  159. }
  160. }
  161. if ($strict) {
  162. $helper = $this->view->getHelper($proxy);
  163. } else {
  164. try {
  165. $helper = $this->view->getHelper($proxy);
  166. } catch (Zend_Loader_PluginLoader_Exception $e) {
  167. return null;
  168. }
  169. }
  170. if (!$helper instanceof Zend_View_Helper_Navigation_Helper) {
  171. if ($strict) {
  172. require_once 'Zend/View/Exception.php';
  173. $e = new Zend_View_Exception(sprintf(
  174. 'Proxy helper "%s" is not an instance of ' .
  175. 'Zend_View_Helper_Navigation_Helper',
  176. get_class($helper)));
  177. $e->setView($this->view);
  178. throw $e;
  179. }
  180. return null;
  181. }
  182. $this->_inject($helper);
  183. $this->_helpers[$proxy] = $helper;
  184. return $helper;
  185. }
  186. /**
  187. * Injects container, ACL, and translator to the given $helper if this
  188. * helper is configured to do so
  189. *
  190. * @param Zend_View_Helper_Navigation_Helper $helper helper instance
  191. * @return void
  192. */
  193. protected function _inject(Zend_View_Helper_Navigation_Helper $helper)
  194. {
  195. if ($this->getInjectContainer() && !$helper->hasContainer()) {
  196. $helper->setContainer($this->getContainer());
  197. }
  198. if ($this->getInjectAcl()) {
  199. if (!$helper->hasAcl()) {
  200. $helper->setAcl($this->getAcl());
  201. }
  202. if (!$helper->hasRole()) {
  203. $helper->setRole($this->getRole());
  204. }
  205. }
  206. if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
  207. $helper->setTranslator($this->getTranslator());
  208. }
  209. }
  210. // Accessors:
  211. /**
  212. * Sets the default proxy to use in {@link render()}
  213. *
  214. * @param string $proxy default proxy
  215. * @return Zend_View_Helper_Navigation fluent interface, returns self
  216. */
  217. public function setDefaultProxy($proxy)
  218. {
  219. $this->_defaultProxy = (string) $proxy;
  220. return $this;
  221. }
  222. /**
  223. * Returns the default proxy to use in {@link render()}
  224. *
  225. * @return string the default proxy to use in {@link render()}
  226. */
  227. public function getDefaultProxy()
  228. {
  229. return $this->_defaultProxy;
  230. }
  231. /**
  232. * Sets whether container should be injected when proxying
  233. *
  234. * @param bool $injectContainer [optional] whether container should
  235. * be injected when proxying. Default
  236. * is true.
  237. * @return Zend_View_Helper_Navigation fluent interface, returns self
  238. */
  239. public function setInjectContainer($injectContainer = true)
  240. {
  241. $this->_injectContainer = (bool) $injectContainer;
  242. return $this;
  243. }
  244. /**
  245. * Returns whether container should be injected when proxying
  246. *
  247. * @return bool whether container should be injected when proxying
  248. */
  249. public function getInjectContainer()
  250. {
  251. return $this->_injectContainer;
  252. }
  253. /**
  254. * Sets whether ACL should be injected when proxying
  255. *
  256. * @param bool $injectAcl [optional] whether ACL should be
  257. * injected when proxying. Default is
  258. * true.
  259. * @return Zend_View_Helper_Navigation fluent interface, returns self
  260. */
  261. public function setInjectAcl($injectAcl = true)
  262. {
  263. $this->_injectAcl = (bool) $injectAcl;
  264. return $this;
  265. }
  266. /**
  267. * Returns whether ACL should be injected when proxying
  268. *
  269. * @return bool whether ACL should be injected when proxying
  270. */
  271. public function getInjectAcl()
  272. {
  273. return $this->_injectAcl;
  274. }
  275. /**
  276. * Sets whether translator should be injected when proxying
  277. *
  278. * @param bool $injectTranslator [optional] whether translator should
  279. * be injected when proxying. Default
  280. * is true.
  281. * @return Zend_View_Helper_Navigation fluent interface, returns self
  282. */
  283. public function setInjectTranslator($injectTranslator = true)
  284. {
  285. $this->_injectTranslator = (bool) $injectTranslator;
  286. return $this;
  287. }
  288. /**
  289. * Returns whether translator should be injected when proxying
  290. *
  291. * @return bool whether translator should be injected when proxying
  292. */
  293. public function getInjectTranslator()
  294. {
  295. return $this->_injectTranslator;
  296. }
  297. // Zend_View_Helper_Navigation_Helper:
  298. /**
  299. * Renders helper
  300. *
  301. * @param Zend_Navigation_Container $container [optional] container to
  302. * render. Default is to
  303. * render the container
  304. * registered in the helper.
  305. * @return string helper output
  306. * @throws Zend_Loader_PluginLoader_Exception if helper cannot be found
  307. * @throws Zend_View_Exception if helper doesn't implement
  308. * the interface specified in
  309. * {@link findHelper()}
  310. */
  311. public function render(Zend_Navigation_Container $container = null)
  312. {
  313. $helper = $this->findHelper($this->getDefaultProxy());
  314. return $helper->render($container);
  315. }
  316. }