PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 1ms

/modules/ZendFramework1Mvc/library/Zend/Navigation/Page/Mvc.php

https://github.com/MontmereLimited/zf2
PHP | 488 lines | 194 code | 52 blank | 242 comment | 29 complexity | 78f9dbdfbfc33951b89143a01db0e887 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_Navigation
  17. * @subpackage Page
  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. namespace Zend\Navigation\Page;
  22. use Zend\Navigation\Exception,
  23. Zend\Controller\Front as FrontController;
  24. /**
  25. * Represents a page that is defined using module, controller, action, route
  26. * name and route params to assemble the href
  27. *
  28. * @category Zend
  29. * @package Zend_Navigation
  30. * @subpackage Page
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Mvc extends AbstractPage
  35. {
  36. /**
  37. * Action name to use when assembling URL
  38. *
  39. * @var string
  40. */
  41. protected $action;
  42. /**
  43. * Controller name to use when assembling URL
  44. *
  45. * @var string
  46. */
  47. protected $controller;
  48. /**
  49. * Module name to use when assembling URL
  50. *
  51. * @var string
  52. */
  53. protected $module;
  54. /**
  55. * Params to use when assembling URL
  56. *
  57. * @see getHref()
  58. * @var array
  59. */
  60. protected $params = array();
  61. /**
  62. * Route name to use when assembling URL
  63. *
  64. * @see getHref()
  65. * @var string
  66. */
  67. protected $route;
  68. /**
  69. * Whether params should be reset when assembling URL
  70. *
  71. * @see getHref()
  72. * @var bool
  73. */
  74. protected $resetParams = true;
  75. /**
  76. * Whether href should be encoded when assembling URL
  77. *
  78. * @see getHref()
  79. * @var bool
  80. */
  81. protected $encodeUrl = true;
  82. /**
  83. * Cached href
  84. *
  85. * The use of this variable minimizes execution time when getHref() is
  86. * called more than once during the lifetime of a request. If a property
  87. * is updated, the cache is invalidated.
  88. *
  89. * @var string
  90. */
  91. protected $hrefCache;
  92. /**
  93. * Action helper for assembling URLs
  94. *
  95. * @see getHref()
  96. * @var \Zend\Controller\Action\Helper\Url
  97. */
  98. protected static $urlHelper = null;
  99. // Accessors:
  100. /**
  101. * Returns whether page should be considered active or not
  102. *
  103. * This method will compare the page properties against the request object
  104. * that is found in the front controller.
  105. *
  106. * @param bool $recursive [optional] whether page should be considered
  107. * active if any child pages are active. Default is
  108. * false.
  109. *
  110. * @return bool whether page should be considered active or not
  111. */
  112. public function isActive($recursive = false)
  113. {
  114. if (!$this->active) {
  115. $front = FrontController::getInstance();
  116. $reqParams = $front->getRequest()->getParams();
  117. if (!array_key_exists('module', $reqParams)) {
  118. $reqParams['module'] = $front->getDefaultModule();
  119. }
  120. $myParams = $this->params;
  121. if (null !== $this->module) {
  122. $myParams['module'] = $this->module;
  123. } else {
  124. $myParams['module'] = $front->getDefaultModule();
  125. }
  126. if (null !== $this->controller) {
  127. $myParams['controller'] = $this->controller;
  128. } else {
  129. $myParams['controller'] = $front->getDefaultControllerName();
  130. }
  131. if (null !== $this->action) {
  132. $myParams['action'] = $this->action;
  133. } else {
  134. $myParams['action'] = $front->getDefaultAction();
  135. }
  136. if (count(array_intersect_assoc($reqParams, $myParams)) ==
  137. count($myParams)
  138. ) {
  139. $this->active = true;
  140. return true;
  141. }
  142. }
  143. return parent::isActive($recursive);
  144. }
  145. /**
  146. * Returns href for this page
  147. *
  148. * This method uses {@link \Zend\Controller\Action\Helper\Url} to assemble
  149. * the href based on the page's properties.
  150. *
  151. * @return string page href
  152. */
  153. public function getHref()
  154. {
  155. if ($this->hrefCache) {
  156. return $this->hrefCache;
  157. }
  158. if (null === self::$urlHelper) {
  159. $front = FrontController::getInstance();
  160. $broker = $front->getHelperBroker();
  161. self::$urlHelper = $broker->load('url');
  162. }
  163. $params = $this->getParams();
  164. if (($param = $this->getModule()) != null) {
  165. $params['module'] = $param;
  166. }
  167. if (($param = $this->getController()) != null) {
  168. $params['controller'] = $param;
  169. }
  170. if (($param = $this->getAction()) != null) {
  171. $params['action'] = $param;
  172. }
  173. $url = self::$urlHelper->__invoke(
  174. $params,
  175. $this->getRoute(),
  176. $this->getResetParams(),
  177. $this->getEncodeUrl()
  178. );
  179. // Add the fragment identifier if it is set
  180. $fragment = $this->getFragment();
  181. if (null !== $fragment) {
  182. $url .= '#' . $fragment;
  183. }
  184. return $this->hrefCache = $url;
  185. }
  186. /**
  187. * Sets action name to use when assembling URL
  188. *
  189. * @see getHref()
  190. *
  191. * @param string $action action name
  192. *
  193. * @return Page\Mvc fluent interface, returns self
  194. * @throws Exception\InvalidArgumentException if invalid $action is given
  195. */
  196. public function setAction($action)
  197. {
  198. if (null !== $action && !is_string($action)) {
  199. throw new Exception\InvalidArgumentException(
  200. 'Invalid argument: $action must be a string or null'
  201. );
  202. }
  203. $this->action = $action;
  204. $this->hrefCache = null;
  205. return $this;
  206. }
  207. /**
  208. * Returns action name to use when assembling URL
  209. *
  210. * @see getHref()
  211. *
  212. * @return string|null action name
  213. */
  214. public function getAction()
  215. {
  216. return $this->action;
  217. }
  218. /**
  219. * Sets controller name to use when assembling URL
  220. *
  221. * @see getHref()
  222. *
  223. * @param string|null $controller controller name
  224. *
  225. * @return Page\Mvc fluent interface, returns self
  226. * @throws Exception\InvalidArgumentException if invalid controller name
  227. * is given
  228. */
  229. public function setController($controller)
  230. {
  231. if (null !== $controller && !is_string($controller)) {
  232. throw new Exception\InvalidArgumentException(
  233. 'Invalid argument: $controller must be a string or null'
  234. );
  235. }
  236. $this->controller = $controller;
  237. $this->hrefCache = null;
  238. return $this;
  239. }
  240. /**
  241. * Returns controller name to use when assembling URL
  242. *
  243. * @see getHref()
  244. *
  245. * @return string|null controller name or null
  246. */
  247. public function getController()
  248. {
  249. return $this->controller;
  250. }
  251. /**
  252. * Sets module name to use when assembling URL
  253. *
  254. * @see getHref()
  255. *
  256. * @param string|null $module module name
  257. *
  258. * @return Page\Mvc fluent interface, returns self
  259. * @throws Exception\InvalidArgumentException if invalid module name
  260. * is given
  261. */
  262. public function setModule($module)
  263. {
  264. if (null !== $module && !is_string($module)) {
  265. throw new Exception\InvalidArgumentException(
  266. 'Invalid argument: $module must be a string or null'
  267. );
  268. }
  269. $this->module = $module;
  270. $this->hrefCache = null;
  271. return $this;
  272. }
  273. /**
  274. * Returns module name to use when assembling URL
  275. *
  276. * @see getHref()
  277. *
  278. * @return string|null module name or null
  279. */
  280. public function getModule()
  281. {
  282. return $this->module;
  283. }
  284. /**
  285. * Sets params to use when assembling URL
  286. *
  287. * @see getHref()
  288. *
  289. * @param array|null $params [optional] page params. Default is null
  290. * which sets no params.
  291. *
  292. * @return Page\Mvc fluent interface, returns self
  293. */
  294. public function setParams(array $params = null)
  295. {
  296. if (null === $params) {
  297. $this->params = array();
  298. } else {
  299. // TODO: do this more intelligently?
  300. $this->params = $params;
  301. }
  302. $this->hrefCache = null;
  303. return $this;
  304. }
  305. /**
  306. * Returns params to use when assembling URL
  307. *
  308. * @see getHref()
  309. *
  310. * @return array page params
  311. */
  312. public function getParams()
  313. {
  314. return $this->params;
  315. }
  316. /**
  317. * Sets route name to use when assembling URL
  318. *
  319. * @see getHref()
  320. *
  321. * @param string $route route name to use when assembling URL
  322. *
  323. * @return Page\Mvc fluent interface, returns self
  324. * @throws Exception\InvalidArgumentException if invalid $route is given
  325. */
  326. public function setRoute($route)
  327. {
  328. if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
  329. throw new Exception\InvalidArgumentException(
  330. 'Invalid argument: $route must be a non-empty string or null'
  331. );
  332. }
  333. $this->route = $route;
  334. $this->hrefCache = null;
  335. return $this;
  336. }
  337. /**
  338. * Returns route name to use when assembling URL
  339. *
  340. * @see getHref()
  341. *
  342. * @return string route name
  343. */
  344. public function getRoute()
  345. {
  346. return $this->route;
  347. }
  348. /**
  349. * Sets whether params should be reset when assembling URL
  350. *
  351. * @see getHref()
  352. *
  353. * @param bool $resetParams whether params should be reset when
  354. * assembling URL
  355. *
  356. * @return Page\Mvc fluent interface, returns self
  357. */
  358. public function setResetParams($resetParams)
  359. {
  360. $this->resetParams = (bool)$resetParams;
  361. $this->hrefCache = null;
  362. return $this;
  363. }
  364. /**
  365. * Returns whether params should be reset when assembling URL
  366. *
  367. * @see getHref()
  368. *
  369. * @return bool whether params should be reset when assembling URL
  370. */
  371. public function getResetParams()
  372. {
  373. return $this->resetParams;
  374. }
  375. /**
  376. * Sets whether href should be encoded when assembling URL
  377. *
  378. * @see getHref()
  379. *
  380. * @param bool $resetParams whether href should be encoded when
  381. * assembling URL
  382. *
  383. * @return Page\Mvc fluent interface, returns self
  384. */
  385. public function setEncodeUrl($encodeUrl)
  386. {
  387. $this->encodeUrl = (bool) $encodeUrl;
  388. $this->hrefCache = null;
  389. return $this;
  390. }
  391. /**
  392. * Returns whether herf should be encoded when assembling URL
  393. *
  394. * @see getHref()
  395. *
  396. * @return bool whether herf should be encoded when assembling URL
  397. */
  398. public function getEncodeUrl()
  399. {
  400. return $this->encodeUrl;
  401. }
  402. /**
  403. * Sets action helper for assembling URLs
  404. *
  405. * @see getHref()
  406. *
  407. * @param \Zend\Controller\Action\Helper\Url $uh URL helper
  408. *
  409. * @return void
  410. */
  411. public static function setUrlHelper(\Zend\Controller\Action\Helper\Url $uh)
  412. {
  413. self::$urlHelper = $uh;
  414. }
  415. // Public methods:
  416. /**
  417. * Returns an array representation of the page
  418. *
  419. * @return array associative array containing all page properties
  420. */
  421. public function toArray()
  422. {
  423. return array_merge(
  424. parent::toArray(),
  425. array(
  426. 'action' => $this->getAction(),
  427. 'controller' => $this->getController(),
  428. 'module' => $this->getModule(),
  429. 'params' => $this->getParams(),
  430. 'route' => $this->getRoute(),
  431. 'reset_params' => $this->getResetParams(),
  432. 'encodeUrl' => $this->getEncodeUrl(),
  433. )
  434. );
  435. }
  436. }