PageRenderTime 35ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/View/Helper/Navigation.php

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