123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace Symfony\Component\Routing\Generator;
- use Symfony\Component\Routing\RouteCollection;
- use Symfony\Component\Routing\RequestContext;
- use Symfony\Component\Routing\Exception\InvalidParameterException;
- use Symfony\Component\Routing\Exception\RouteNotFoundException;
- use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
- use Psr\Log\LoggerInterface;
- class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInterface
- {
-
- protected $routes;
-
- protected $context;
-
- protected $strictRequirements = true;
-
- protected $logger;
-
- protected $decodedChars = array(
-
-
-
- '%2F' => '/',
-
-
- '%40' => '@',
- '%3A' => ':',
-
-
- '%3B' => ';',
- '%2C' => ',',
- '%3D' => '=',
- '%2B' => '+',
- '%21' => '!',
- '%2A' => '*',
- '%7C' => '|',
- );
-
- public function __construct(RouteCollection $routes, RequestContext $context, LoggerInterface $logger = null)
- {
- $this->routes = $routes;
- $this->context = $context;
- $this->logger = $logger;
- }
-
- public function setContext(RequestContext $context)
- {
- $this->context = $context;
- }
-
- public function getContext()
- {
- return $this->context;
- }
-
- public function setStrictRequirements($enabled)
- {
- $this->strictRequirements = null === $enabled ? null : (bool) $enabled;
- }
-
- public function isStrictRequirements()
- {
- return $this->strictRequirements;
- }
-
- public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
- {
- if (null === $route = $this->routes->get($name)) {
- throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
- }
-
- $compiledRoute = $route->compile();
- return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $referenceType, $compiledRoute->getHostTokens(), $route->getSchemes());
- }
-
- protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
- {
- $variables = array_flip($variables);
- $mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters);
-
- if ($diff = array_diff_key($variables, $mergedParams)) {
- throw new MissingMandatoryParametersException(sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', array_keys($diff)), $name));
- }
- $url = '';
- $optional = true;
- foreach ($tokens as $token) {
- if ('variable' === $token[0]) {
- if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
-
- if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
- $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]);
- if ($this->strictRequirements) {
- throw new InvalidParameterException($message);
- }
- if ($this->logger) {
- $this->logger->error($message);
- }
- return;
- }
- $url = $token[1].$mergedParams[$token[3]].$url;
- $optional = false;
- }
- } else {
-
- $url = $token[1].$url;
- $optional = false;
- }
- }
- if ('' === $url) {
- $url = '/';
- }
-
- $url = strtr(rawurlencode($url), $this->decodedChars);
-
-
-
- $url = strtr($url, array('/../' => '/%2E%2E/', '/./' => '/%2E/'));
- if ('/..' === substr($url, -3)) {
- $url = substr($url, 0, -2).'%2E%2E';
- } elseif ('/.' === substr($url, -2)) {
- $url = substr($url, 0, -1).'%2E';
- }
- $schemeAuthority = '';
- if ($host = $this->context->getHost()) {
- $scheme = $this->context->getScheme();
- if ($requiredSchemes) {
- if (!in_array($scheme, $requiredSchemes, true)) {
- $referenceType = self::ABSOLUTE_URL;
- $scheme = current($requiredSchemes);
- }
- }
- if ($hostTokens) {
- $routeHost = '';
- foreach ($hostTokens as $token) {
- if ('variable' === $token[0]) {
- if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) {
- $message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]);
- if ($this->strictRequirements) {
- throw new InvalidParameterException($message);
- }
- if ($this->logger) {
- $this->logger->error($message);
- }
- return;
- }
- $routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
- } else {
- $routeHost = $token[1].$routeHost;
- }
- }
- if ($routeHost !== $host) {
- $host = $routeHost;
- if (self::ABSOLUTE_URL !== $referenceType) {
- $referenceType = self::NETWORK_PATH;
- }
- }
- }
- if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
- $port = '';
- if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
- $port = ':'.$this->context->getHttpPort();
- } elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
- $port = ':'.$this->context->getHttpsPort();
- }
- $schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
- $schemeAuthority .= $host.$port;
- }
- }
- if (self::RELATIVE_PATH === $referenceType) {
- $url = self::getRelativePath($this->context->getPathInfo(), $url);
- } else {
- $url = $schemeAuthority.$this->context->getBaseUrl().$url;
- }
-
- $extra = array_udiff_assoc(array_diff_key($parameters, $variables), $defaults, function ($a, $b) {
- return $a == $b ? 0 : 1;
- });
- if ($extra && $query = http_build_query($extra, '', '&')) {
-
-
- $url .= '?'.strtr($query, array('%2F' => '/'));
- }
- return $url;
- }
-
- public static function getRelativePath($basePath, $targetPath)
- {
- if ($basePath === $targetPath) {
- return '';
- }
- $sourceDirs = explode('/', isset($basePath[0]) && '/' === $basePath[0] ? substr($basePath, 1) : $basePath);
- $targetDirs = explode('/', isset($targetPath[0]) && '/' === $targetPath[0] ? substr($targetPath, 1) : $targetPath);
- array_pop($sourceDirs);
- $targetFile = array_pop($targetDirs);
- foreach ($sourceDirs as $i => $dir) {
- if (isset($targetDirs[$i]) && $dir === $targetDirs[$i]) {
- unset($sourceDirs[$i], $targetDirs[$i]);
- } else {
- break;
- }
- }
- $targetDirs[] = $targetFile;
- $path = str_repeat('../', count($sourceDirs)).implode('/', $targetDirs);
-
-
-
-
- return '' === $path || '/' === $path[0]
- || false !== ($colonPos = strpos($path, ':')) && ($colonPos < ($slashPos = strpos($path, '/')) || false === $slashPos)
- ? "./$path" : $path;
- }
- }
|