123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
- namespace Symfony\Component\Routing;
- class RouteCompiler implements RouteCompilerInterface
- {
-
- public const REGEX_DELIMITER = '#';
-
- public const SEPARATORS = '/,;.:-_~+*=@|';
-
- public const VARIABLE_MAXIMUM_LENGTH = 32;
-
- public static function compile(Route $route)
- {
- $hostVariables = [];
- $variables = [];
- $hostRegex = null;
- $hostTokens = [];
- if ('' !== $host = $route->getHost()) {
- $result = self::compilePattern($route, $host, true);
- $hostVariables = $result['variables'];
- $variables = $hostVariables;
- $hostTokens = $result['tokens'];
- $hostRegex = $result['regex'];
- }
- $locale = $route->getDefault('_locale');
- if (null !== $locale && null !== $route->getDefault('_canonical_route') && preg_quote($locale) === $route->getRequirement('_locale')) {
- $requirements = $route->getRequirements();
- unset($requirements['_locale']);
- $route->setRequirements($requirements);
- $route->setPath(str_replace('{_locale}', $locale, $route->getPath()));
- }
- $path = $route->getPath();
- $result = self::compilePattern($route, $path, false);
- $staticPrefix = $result['staticPrefix'];
- $pathVariables = $result['variables'];
- foreach ($pathVariables as $pathParam) {
- if ('_fragment' === $pathParam) {
- throw new \InvalidArgumentException(sprintf('Route pattern "%s" cannot contain "_fragment" as a path parameter.', $route->getPath()));
- }
- }
- $variables = array_merge($variables, $pathVariables);
- $tokens = $result['tokens'];
- $regex = $result['regex'];
- return new CompiledRoute(
- $staticPrefix,
- $regex,
- $tokens,
- $pathVariables,
- $hostRegex,
- $hostTokens,
- $hostVariables,
- array_unique($variables)
- );
- }
- private static function compilePattern(Route $route, string $pattern, bool $isHost): array
- {
- $tokens = [];
- $variables = [];
- $matches = [];
- $pos = 0;
- $defaultSeparator = $isHost ? '.' : '/';
- $useUtf8 = preg_match('//u', $pattern);
- $needsUtf8 = $route->getOption('utf8');
- if (!$needsUtf8 && $useUtf8 && preg_match('/[\x80-\xFF]/', $pattern)) {
- throw new \LogicException(sprintf('Cannot use UTF-8 route patterns without setting the "utf8" option for route "%s".', $route->getPath()));
- }
- if (!$useUtf8 && $needsUtf8) {
- throw new \LogicException(sprintf('Cannot mix UTF-8 requirements with non-UTF-8 pattern "%s".', $pattern));
- }
-
-
- preg_match_all('#\{(!)?(\w+)\}#', $pattern, $matches, \PREG_OFFSET_CAPTURE | \PREG_SET_ORDER);
- foreach ($matches as $match) {
- $important = $match[1][1] >= 0;
- $varName = $match[2][0];
-
- $precedingText = substr($pattern, $pos, $match[0][1] - $pos);
- $pos = $match[0][1] + \strlen($match[0][0]);
- if (!\strlen($precedingText)) {
- $precedingChar = '';
- } elseif ($useUtf8) {
- preg_match('/.$/u', $precedingText, $precedingChar);
- $precedingChar = $precedingChar[0];
- } else {
- $precedingChar = substr($precedingText, -1);
- }
- $isSeparator = '' !== $precedingChar && str_contains(static::SEPARATORS, $precedingChar);
-
-
- if (preg_match('/^\d/', $varName)) {
- throw new \DomainException(sprintf('Variable name "%s" cannot start with a digit in route pattern "%s". Please use a different name.', $varName, $pattern));
- }
- if (\in_array($varName, $variables)) {
- throw new \LogicException(sprintf('Route pattern "%s" cannot reference variable name "%s" more than once.', $pattern, $varName));
- }
- if (\strlen($varName) > self::VARIABLE_MAXIMUM_LENGTH) {
- throw new \DomainException(sprintf('Variable name "%s" cannot be longer than %d characters in route pattern "%s". Please use a shorter name.', $varName, self::VARIABLE_MAXIMUM_LENGTH, $pattern));
- }
- if ($isSeparator && $precedingText !== $precedingChar) {
- $tokens[] = ['text', substr($precedingText, 0, -\strlen($precedingChar))];
- } elseif (!$isSeparator && '' !== $precedingText) {
- $tokens[] = ['text', $precedingText];
- }
- $regexp = $route->getRequirement($varName);
- if (null === $regexp) {
- $followingPattern = (string) substr($pattern, $pos);
-
-
-
-
-
-
-
- $nextSeparator = self::findNextSeparator($followingPattern, $useUtf8);
- $regexp = sprintf(
- '[^%s%s]+',
- preg_quote($defaultSeparator),
- $defaultSeparator !== $nextSeparator && '' !== $nextSeparator ? preg_quote($nextSeparator) : ''
- );
- if (('' !== $nextSeparator && !preg_match('#^\{\w+\}#', $followingPattern)) || '' === $followingPattern) {
-
-
-
-
-
- $regexp .= '+';
- }
- } else {
- if (!preg_match('//u', $regexp)) {
- $useUtf8 = false;
- } elseif (!$needsUtf8 && preg_match('/[\x80-\xFF]|(?<!\\\\)\\\\(?:\\\\\\\\)*+(?-i:X|[pP][\{CLMNPSZ]|x\{[A-Fa-f0-9]{3})/', $regexp)) {
- throw new \LogicException(sprintf('Cannot use UTF-8 route requirements without setting the "utf8" option for variable "%s" in pattern "%s".', $varName, $pattern));
- }
- if (!$useUtf8 && $needsUtf8) {
- throw new \LogicException(sprintf('Cannot mix UTF-8 requirement with non-UTF-8 charset for variable "%s" in pattern "%s".', $varName, $pattern));
- }
- $regexp = self::transformCapturingGroupsToNonCapturings($regexp);
- }
- if ($important) {
- $token = ['variable', $isSeparator ? $precedingChar : '', $regexp, $varName, false, true];
- } else {
- $token = ['variable', $isSeparator ? $precedingChar : '', $regexp, $varName];
- }
- $tokens[] = $token;
- $variables[] = $varName;
- }
- if ($pos < \strlen($pattern)) {
- $tokens[] = ['text', substr($pattern, $pos)];
- }
-
- $firstOptional = \PHP_INT_MAX;
- if (!$isHost) {
- for ($i = \count($tokens) - 1; $i >= 0; --$i) {
- $token = $tokens[$i];
-
- if ('variable' === $token[0] && !($token[5] ?? false) && $route->hasDefault($token[3])) {
- $firstOptional = $i;
- } else {
- break;
- }
- }
- }
-
- $regexp = '';
- for ($i = 0, $nbToken = \count($tokens); $i < $nbToken; ++$i) {
- $regexp .= self::computeRegexp($tokens, $i, $firstOptional);
- }
- $regexp = '{^'.$regexp.'$}sD'.($isHost ? 'i' : '');
-
- if ($needsUtf8) {
- $regexp .= 'u';
- for ($i = 0, $nbToken = \count($tokens); $i < $nbToken; ++$i) {
- if ('variable' === $tokens[$i][0]) {
- $tokens[$i][4] = true;
- }
- }
- }
- return [
- 'staticPrefix' => self::determineStaticPrefix($route, $tokens),
- 'regex' => $regexp,
- 'tokens' => array_reverse($tokens),
- 'variables' => $variables,
- ];
- }
-
- private static function determineStaticPrefix(Route $route, array $tokens): string
- {
- if ('text' !== $tokens[0][0]) {
- return ($route->hasDefault($tokens[0][3]) || '/' === $tokens[0][1]) ? '' : $tokens[0][1];
- }
- $prefix = $tokens[0][1];
- if (isset($tokens[1][1]) && '/' !== $tokens[1][1] && false === $route->hasDefault($tokens[1][3])) {
- $prefix .= $tokens[1][1];
- }
- return $prefix;
- }
-
- private static function findNextSeparator(string $pattern, bool $useUtf8): string
- {
- if ('' == $pattern) {
-
- return '';
- }
-
- if ('' === $pattern = preg_replace('#\{\w+\}#', '', $pattern)) {
- return '';
- }
- if ($useUtf8) {
- preg_match('/^./u', $pattern, $pattern);
- }
- return str_contains(static::SEPARATORS, $pattern[0]) ? $pattern[0] : '';
- }
-
- private static function computeRegexp(array $tokens, int $index, int $firstOptional): string
- {
- $token = $tokens[$index];
- if ('text' === $token[0]) {
-
- return preg_quote($token[1]);
- } else {
-
- if (0 === $index && 0 === $firstOptional) {
-
- return sprintf('%s(?P<%s>%s)?', preg_quote($token[1]), $token[3], $token[2]);
- } else {
- $regexp = sprintf('%s(?P<%s>%s)', preg_quote($token[1]), $token[3], $token[2]);
- if ($index >= $firstOptional) {
-
-
-
- $regexp = "(?:$regexp";
- $nbTokens = \count($tokens);
- if ($nbTokens - 1 == $index) {
-
- $regexp .= str_repeat(')?', $nbTokens - $firstOptional - (0 === $firstOptional ? 1 : 0));
- }
- }
- return $regexp;
- }
- }
- }
- private static function transformCapturingGroupsToNonCapturings(string $regexp): string
- {
- for ($i = 0; $i < \strlen($regexp); ++$i) {
- if ('\\' === $regexp[$i]) {
- ++$i;
- continue;
- }
- if ('(' !== $regexp[$i] || !isset($regexp[$i + 2])) {
- continue;
- }
- if ('*' === $regexp[++$i] || '?' === $regexp[$i]) {
- ++$i;
- continue;
- }
- $regexp = substr_replace($regexp, '?:', $i, 0);
- ++$i;
- }
- return $regexp;
- }
- }
|