UrlGenerator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Generator;
  11. use Symfony\Component\Routing\Route;
  12. use Symfony\Component\Routing\RouteCollection;
  13. use Symfony\Component\Routing\RequestContext;
  14. /**
  15. * UrlGenerator generates URL based on a set of routes.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class UrlGenerator implements UrlGeneratorInterface
  20. {
  21. protected $context;
  22. private $routes;
  23. private $cache;
  24. /**
  25. * Constructor.
  26. *
  27. * @param RouteCollection $routes A RouteCollection instance
  28. * @param RequestContext $context The context
  29. */
  30. public function __construct(RouteCollection $routes, RequestContext $context)
  31. {
  32. $this->routes = $routes;
  33. $this->context = $context;
  34. $this->cache = array();
  35. }
  36. /**
  37. * Sets the request context.
  38. *
  39. * @param RequestContext $context The context
  40. */
  41. public function setContext(RequestContext $context)
  42. {
  43. $this->context = $context;
  44. }
  45. /**
  46. * Generates a URL from the given parameters.
  47. *
  48. * @param string $name The name of the route
  49. * @param array $parameters An array of parameters
  50. * @param Boolean $absolute Whether to generate an absolute URL
  51. *
  52. * @return string The generated URL
  53. *
  54. * @throws \InvalidArgumentException When route doesn't exist
  55. */
  56. public function generate($name, array $parameters = array(), $absolute = false)
  57. {
  58. if (null === $route = $this->routes->get($name)) {
  59. throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name));
  60. }
  61. if (!isset($this->cache[$name])) {
  62. $this->cache[$name] = $route->compile();
  63. }
  64. return $this->doGenerate($this->cache[$name]->getVariables(), $route->getDefaults(), $route->getRequirements(), $this->cache[$name]->getTokens(), $parameters, $name, $absolute);
  65. }
  66. /**
  67. * @throws \InvalidArgumentException When route has some missing mandatory parameters
  68. */
  69. protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $absolute)
  70. {
  71. $parameters = array_replace($this->context->getParameters(), $parameters);
  72. $tparams = array_replace($defaults, $parameters);
  73. // all params must be given
  74. if ($diff = array_diff_key($variables, $tparams)) {
  75. throw new \InvalidArgumentException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $name, implode(', ', $diff)));
  76. }
  77. $url = '';
  78. $optional = true;
  79. foreach ($tokens as $token) {
  80. if ('variable' === $token[0]) {
  81. if (false === $optional || !isset($defaults[$token[3]]) || (isset($parameters[$token[3]]) && $parameters[$token[3]] != $defaults[$token[3]])) {
  82. // check requirement
  83. if (isset($requirements[$token[3]]) && !preg_match('#^'.$requirements[$token[3]].'$#', $tparams[$token[3]])) {
  84. throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $requirements[$token[3]], $tparams[$token[3]]));
  85. }
  86. if (isset($tparams[$token[3]])) {
  87. // %2F is not valid in a URL, so we don't encode it (which is fine as the requirements explicitly allowed it)
  88. $url = $token[1].str_replace('%2F', '/', urlencode($tparams[$token[3]])).$url;
  89. }
  90. $optional = false;
  91. }
  92. } elseif ('text' === $token[0]) {
  93. $url = $token[1].$token[2].$url;
  94. $optional = false;
  95. } else {
  96. // handle custom tokens
  97. if ($segment = call_user_func_array(array($this, 'generateFor'.ucfirst(array_shift($token))), array_merge(array($optional, $tparams), $token))) {
  98. $url = $segment.$url;
  99. $optional = false;
  100. }
  101. }
  102. }
  103. if (!$url) {
  104. $url = '/';
  105. }
  106. // add a query string if needed
  107. if ($extra = array_diff_key($parameters, $variables, $defaults)) {
  108. $url .= '?'.http_build_query($extra);
  109. }
  110. $url = $this->context->getBaseUrl().$url;
  111. if ($this->context->getHost()) {
  112. $scheme = $this->context->getScheme();
  113. if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
  114. $absolute = true;
  115. $scheme = $req;
  116. }
  117. if ($absolute) {
  118. $port = '';
  119. if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
  120. $port = ':'.$this->context->getHttpPort();
  121. } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
  122. $port = ':'.$this->context->getHttpsPort();
  123. }
  124. $url = $scheme.'://'.$this->context->getHost().$port.$url;
  125. }
  126. }
  127. return $url;
  128. }
  129. }