/library/Zend/View/Helper/Url.php

https://github.com/christeredvartsen/zf2 · PHP · 119 lines · 53 code · 17 blank · 49 comment · 11 complexity · 55fc791bf2d38f7df3a0a053b72530e8 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. * @package Zend_View
  9. */
  10. namespace Zend\View\Helper;
  11. use Zend\Mvc\ModuleRouteListener;
  12. use Zend\Mvc\Router\RouteMatch;
  13. use Zend\Mvc\Router\RouteStackInterface;
  14. use Zend\View\Exception;
  15. /**
  16. * Helper for making easy links and getting urls that depend on the routes and router.
  17. *
  18. * @package Zend_View
  19. * @subpackage Helper
  20. */
  21. class Url extends AbstractHelper
  22. {
  23. /**
  24. * RouteStackInterface instance.
  25. *
  26. * @var RouteStackInterface
  27. */
  28. protected $router;
  29. /**
  30. * RouteInterface match returned by the router.
  31. *
  32. * @var RouteMatch.
  33. */
  34. protected $routeMatch;
  35. /**
  36. * Set the router to use for assembling.
  37. *
  38. * @param RouteStackInterface $router
  39. * @return Url
  40. */
  41. public function setRouter(RouteStackInterface $router)
  42. {
  43. $this->router = $router;
  44. return $this;
  45. }
  46. /**
  47. * Set route match returned by the router.
  48. *
  49. * @param RouteMatch $routeMatch
  50. * @return self
  51. */
  52. public function setRouteMatch(RouteMatch $routeMatch)
  53. {
  54. $this->routeMatch = $routeMatch;
  55. return $this;
  56. }
  57. /**
  58. * Generates an url given the name of a route.
  59. *
  60. * @see Zend\Mvc\Router\RouteInterface::assemble()
  61. * @param string $name Name of the route
  62. * @param array $params Parameters for the link
  63. * @param array $options Options for the route
  64. * @param bool $reuseMatchedParams Whether to reuse matched parameters
  65. * @return string Url For the link href attribute
  66. * @throws Exception\RuntimeException If no RouteStackInterface was provided
  67. * @throws Exception\RuntimeException If no RouteMatch was provided
  68. * @throws Exception\RuntimeException If RouteMatch didn't contain a matched route name
  69. */
  70. public function __invoke($name = null, array $params = array(), $options = array(), $reuseMatchedParams = false)
  71. {
  72. if (null === $this->router) {
  73. throw new Exception\RuntimeException('No RouteStackInterface instance provided');
  74. }
  75. if (3 == func_num_args() && is_bool($options)) {
  76. $reuseMatchedParams = $options;
  77. $options = array();
  78. }
  79. if ($name === null) {
  80. if ($this->routeMatch === null) {
  81. throw new Exception\RuntimeException('No RouteMatch instance provided');
  82. }
  83. $name = $this->routeMatch->getMatchedRouteName();
  84. if ($name === null) {
  85. throw new Exception\RuntimeException('RouteMatch does not contain a matched route name');
  86. }
  87. }
  88. if ($reuseMatchedParams && $this->routeMatch !== null) {
  89. $routeMatchParams = $this->routeMatch->getParams();
  90. if (isset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER])) {
  91. $routeMatchParams['controller'] = $routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER];
  92. unset($routeMatchParams[ModuleRouteListener::ORIGINAL_CONTROLLER]);
  93. }
  94. if (isset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE])) {
  95. unset($routeMatchParams[ModuleRouteListener::MODULE_NAMESPACE]);
  96. }
  97. $params = array_merge($routeMatchParams, $params);
  98. }
  99. $options['name'] = $name;
  100. return $this->router->assemble($params, $options);
  101. }
  102. }