PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Navigation/Page/Mvc.php

https://bitbucket.org/zbahij/eprojets_app
PHP | 528 lines | 235 code | 62 blank | 231 comment | 38 complexity | c03ebed6fb84958410210a4b86f4e0e0 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\Navigation\Page;
  10. use Zend\Mvc\ModuleRouteListener;
  11. use Zend\Mvc\Router\RouteMatch;
  12. use Zend\Mvc\Router\RouteStackInterface;
  13. use Zend\Navigation\Exception;
  14. /**
  15. * Represents a page that is defined using controller, action, route
  16. * name and route params to assemble the href
  17. */
  18. class Mvc extends AbstractPage
  19. {
  20. /**
  21. * Action name to use when assembling URL
  22. *
  23. * @var string
  24. */
  25. protected $action;
  26. /**
  27. * Controller name to use when assembling URL
  28. *
  29. * @var string
  30. */
  31. protected $controller;
  32. /**
  33. * URL query part to use when assembling URL
  34. *
  35. * @var array|string
  36. */
  37. protected $query;
  38. /**
  39. * Params to use when assembling URL
  40. *
  41. * @see getHref()
  42. * @var array
  43. */
  44. protected $params = array();
  45. /**
  46. * RouteInterface name to use when assembling URL
  47. *
  48. * @see getHref()
  49. * @var string
  50. */
  51. protected $route;
  52. /**
  53. * Cached href
  54. *
  55. * The use of this variable minimizes execution time when getHref() is
  56. * called more than once during the lifetime of a request. If a property
  57. * is updated, the cache is invalidated.
  58. *
  59. * @var string
  60. */
  61. protected $hrefCache;
  62. /**
  63. * RouteInterface matches; used for routing parameters and testing validity
  64. *
  65. * @var RouteMatch
  66. */
  67. protected $routeMatch;
  68. /**
  69. * If true and set routeMatch than getHref will use routeMatch params
  70. * to assemble uri
  71. * @var bool
  72. */
  73. protected $useRouteMatch = false;
  74. /**
  75. * Router for assembling URLs
  76. *
  77. * @see getHref()
  78. * @var RouteStackInterface
  79. */
  80. protected $router = null;
  81. /**
  82. * Default router to be used if router is not given.
  83. *
  84. * @see getHref()
  85. *
  86. * @var RouteStackInterface
  87. */
  88. protected static $defaultRouter= null;
  89. // Accessors:
  90. /**
  91. * Returns whether page should be considered active or not
  92. *
  93. * This method will compare the page properties against the route matches
  94. * composed in the object.
  95. *
  96. * @param bool $recursive [optional] whether page should be considered
  97. * active if any child pages are active. Default is
  98. * false.
  99. * @return bool whether page should be considered active or not
  100. */
  101. public function isActive($recursive = false)
  102. {
  103. if (!$this->active) {
  104. $reqParams = array();
  105. if ($this->routeMatch instanceof RouteMatch) {
  106. $reqParams = $this->routeMatch->getParams();
  107. if (isset($reqParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
  108. $reqParams['controller'] = $reqParams[ModuleRouteListener::ORIGINAL_CONTROLLER];
  109. }
  110. $myParams = $this->params;
  111. if (null !== $this->controller) {
  112. $myParams['controller'] = $this->controller;
  113. }
  114. if (null !== $this->action) {
  115. $myParams['action'] = $this->action;
  116. }
  117. if (null !== $this->getRoute()
  118. && $this->routeMatch->getMatchedRouteName() === $this->getRoute()
  119. && (count(array_intersect_assoc($reqParams, $myParams)) == count($myParams))
  120. ) {
  121. $this->active = true;
  122. return true;
  123. }
  124. }
  125. $myParams = $this->params;
  126. if (null !== $this->controller) {
  127. $myParams['controller'] = $this->controller;
  128. } else {
  129. /**
  130. * @todo In ZF1, this was configurable and pulled from the front controller
  131. */
  132. $myParams['controller'] = 'index';
  133. }
  134. if (null !== $this->action) {
  135. $myParams['action'] = $this->action;
  136. } else {
  137. /**
  138. * @todo In ZF1, this was configurable and pulled from the front controller
  139. */
  140. $myParams['action'] = 'index';
  141. }
  142. if (count(array_intersect_assoc($reqParams, $myParams)) == count($myParams)) {
  143. $this->active = true;
  144. return true;
  145. }
  146. }
  147. return parent::isActive($recursive);
  148. }
  149. /**
  150. * Returns href for this page
  151. *
  152. * This method uses {@link RouteStackInterface} to assemble
  153. * the href based on the page's properties.
  154. *
  155. * @see RouteStackInterface
  156. * @return string page href
  157. * @throws Exception\DomainException if no router is set
  158. */
  159. public function getHref()
  160. {
  161. if ($this->hrefCache) {
  162. return $this->hrefCache;
  163. }
  164. $router = $this->router;
  165. if (null === $router) {
  166. $router = static::$defaultRouter;
  167. }
  168. if (!$router instanceof RouteStackInterface) {
  169. throw new Exception\DomainException(
  170. __METHOD__
  171. . ' cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed'
  172. );
  173. }
  174. if ($this->useRouteMatch()) {
  175. $rmParams = $this->getRouteMatch()->getParams();
  176. if (isset($rmParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
  177. $rmParams['controller'] = $rmParams[ModuleRouteListener::ORIGINAL_CONTROLLER];
  178. unset($rmParams[ModuleRouteListener::ORIGINAL_CONTROLLER]);
  179. }
  180. if (isset($rmParams[ModuleRouteListener::MODULE_NAMESPACE])) {
  181. unset($rmParams[ModuleRouteListener::MODULE_NAMESPACE]);
  182. }
  183. $params = array_merge($rmParams, $this->getParams());
  184. } else {
  185. $params = $this->getParams();
  186. }
  187. if (($param = $this->getController()) != null) {
  188. $params['controller'] = $param;
  189. }
  190. if (($param = $this->getAction()) != null) {
  191. $params['action'] = $param;
  192. }
  193. switch (true) {
  194. case ($this->getRoute() !== null):
  195. $name = $this->getRoute();
  196. break;
  197. case ($this->getRouteMatch() !== null):
  198. $name = $this->getRouteMatch()->getMatchedRouteName();
  199. break;
  200. default:
  201. throw new Exception\DomainException('No route name could be found');
  202. }
  203. $options = array('name' => $name);
  204. // Add the fragment identifier if it is set
  205. $fragment = $this->getFragment();
  206. if (null !== $fragment) {
  207. $options['fragment'] = $fragment;
  208. }
  209. if (null !== ($query = $this->getQuery())) {
  210. $options['query'] = $query;
  211. }
  212. $url = $router->assemble($params, $options);
  213. return $this->hrefCache = $url;
  214. }
  215. /**
  216. * Sets action name to use when assembling URL
  217. *
  218. * @see getHref()
  219. *
  220. * @param string $action action name
  221. * @return Mvc fluent interface, returns self
  222. * @throws Exception\InvalidArgumentException if invalid $action is given
  223. */
  224. public function setAction($action)
  225. {
  226. if (null !== $action && !is_string($action)) {
  227. throw new Exception\InvalidArgumentException(
  228. 'Invalid argument: $action must be a string or null'
  229. );
  230. }
  231. $this->action = $action;
  232. $this->hrefCache = null;
  233. return $this;
  234. }
  235. /**
  236. * Returns action name to use when assembling URL
  237. *
  238. * @see getHref()
  239. *
  240. * @return string|null action name
  241. */
  242. public function getAction()
  243. {
  244. return $this->action;
  245. }
  246. /**
  247. * Sets controller name to use when assembling URL
  248. *
  249. * @see getHref()
  250. *
  251. * @param string|null $controller controller name
  252. * @return Mvc fluent interface, returns self
  253. * @throws Exception\InvalidArgumentException if invalid controller name is given
  254. */
  255. public function setController($controller)
  256. {
  257. if (null !== $controller && !is_string($controller)) {
  258. throw new Exception\InvalidArgumentException(
  259. 'Invalid argument: $controller must be a string or null'
  260. );
  261. }
  262. $this->controller = $controller;
  263. $this->hrefCache = null;
  264. return $this;
  265. }
  266. /**
  267. * Returns controller name to use when assembling URL
  268. *
  269. * @see getHref()
  270. *
  271. * @return string|null controller name or null
  272. */
  273. public function getController()
  274. {
  275. return $this->controller;
  276. }
  277. /**
  278. * Sets URL query part to use when assembling URL
  279. *
  280. * @see getHref()
  281. * @param array|string|null $query URL query part
  282. * @return self fluent interface, returns self
  283. */
  284. public function setQuery($query)
  285. {
  286. $this->query = $query;
  287. $this->hrefCache = null;
  288. return $this;
  289. }
  290. /**
  291. * Returns URL query part to use when assembling URL
  292. *
  293. * @see getHref()
  294. *
  295. * @return array|string|null URL query part (as an array or string) or null
  296. */
  297. public function getQuery()
  298. {
  299. return $this->query;
  300. }
  301. /**
  302. * Sets params to use when assembling URL
  303. *
  304. * @see getHref()
  305. * @param array|null $params [optional] page params. Default is null
  306. * which sets no params.
  307. * @return Mvc fluent interface, returns self
  308. */
  309. public function setParams(array $params = null)
  310. {
  311. if (null === $params) {
  312. $this->params = array();
  313. } else {
  314. // TODO: do this more intelligently?
  315. $this->params = $params;
  316. }
  317. $this->hrefCache = null;
  318. return $this;
  319. }
  320. /**
  321. * Returns params to use when assembling URL
  322. *
  323. * @see getHref()
  324. *
  325. * @return array page params
  326. */
  327. public function getParams()
  328. {
  329. return $this->params;
  330. }
  331. /**
  332. * Sets route name to use when assembling URL
  333. *
  334. * @see getHref()
  335. *
  336. * @param string $route route name to use when assembling URL
  337. * @return Mvc fluent interface, returns self
  338. * @throws Exception\InvalidArgumentException if invalid $route is given
  339. */
  340. public function setRoute($route)
  341. {
  342. if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
  343. throw new Exception\InvalidArgumentException(
  344. 'Invalid argument: $route must be a non-empty string or null'
  345. );
  346. }
  347. $this->route = $route;
  348. $this->hrefCache = null;
  349. return $this;
  350. }
  351. /**
  352. * Returns route name to use when assembling URL
  353. *
  354. * @see getHref()
  355. *
  356. * @return string route name
  357. */
  358. public function getRoute()
  359. {
  360. return $this->route;
  361. }
  362. /**
  363. * Get the route match.
  364. *
  365. * @return \Zend\Mvc\Router\RouteMatch
  366. */
  367. public function getRouteMatch()
  368. {
  369. return $this->routeMatch;
  370. }
  371. /**
  372. * Set route match object from which parameters will be retrieved
  373. *
  374. * @param RouteMatch $matches
  375. * @return Mvc fluent interface, returns self
  376. */
  377. public function setRouteMatch(RouteMatch $matches)
  378. {
  379. $this->routeMatch = $matches;
  380. return $this;
  381. }
  382. /**
  383. * Get the useRouteMatch flag
  384. *
  385. * @return bool
  386. */
  387. public function useRouteMatch()
  388. {
  389. return $this->useRouteMatch;
  390. }
  391. /**
  392. * Set whether the page should use route match params for assembling link uri
  393. *
  394. * @see getHref()
  395. * @param bool $useRouteMatch [optional]
  396. * @return Mvc
  397. */
  398. public function setUseRouteMatch($useRouteMatch = true)
  399. {
  400. $this->useRouteMatch = (bool) $useRouteMatch;
  401. $this->hrefCache = null;
  402. return $this;
  403. }
  404. /**
  405. * Get the router.
  406. *
  407. * @return null|RouteStackInterface
  408. */
  409. public function getRouter()
  410. {
  411. return $this->router;
  412. }
  413. /**
  414. * Sets router for assembling URLs
  415. *
  416. * @see getHref()
  417. *
  418. * @param RouteStackInterface $router Router
  419. * @return Mvc fluent interface, returns self
  420. */
  421. public function setRouter(RouteStackInterface $router)
  422. {
  423. $this->router = $router;
  424. return $this;
  425. }
  426. /**
  427. * Sets the default router for assembling URLs.
  428. *
  429. * @see getHref()
  430. * @param RouteStackInterface $router Router
  431. * @return void
  432. */
  433. public static function setDefaultRouter($router)
  434. {
  435. static::$defaultRouter = $router;
  436. }
  437. /**
  438. * Gets the default router for assembling URLs.
  439. *
  440. * @return RouteStackInterface
  441. */
  442. public static function getDefaultRouter()
  443. {
  444. return static::$defaultRouter;
  445. }
  446. // Public methods:
  447. /**
  448. * Returns an array representation of the page
  449. *
  450. * @return array associative array containing all page properties
  451. */
  452. public function toArray()
  453. {
  454. return array_merge(
  455. parent::toArray(),
  456. array(
  457. 'action' => $this->getAction(),
  458. 'controller' => $this->getController(),
  459. 'params' => $this->getParams(),
  460. 'route' => $this->getRoute(),
  461. 'router' => $this->getRouter(),
  462. 'route_match' => $this->getRouteMatch(),
  463. )
  464. );
  465. }
  466. }