|
18 | 18 | */
|
19 | 19 | class RouteCompiler implements RouteCompilerInterface
|
20 | 20 | {
|
| 21 | + const REGEX_DELIMITER = '#'; |
| 22 | + |
21 | 23 | /**
|
22 | 24 | * Compiles the current route instance.
|
23 | 25 | *
|
@@ -47,7 +49,7 @@ public function compile(Route $route)
|
47 | 49 | if ($pos !== $len) {
|
48 | 50 | $seps[] = $pattern[$pos];
|
49 | 51 | }
|
50 |
| - $regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), '#')); |
| 52 | + $regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), self::REGEX_DELIMITER)); |
51 | 53 | }
|
52 | 54 |
|
53 | 55 | $tokens[] = array('variable', $match[0][0][0], $regexp, $var);
|
@@ -83,38 +85,40 @@ public function compile(Route $route)
|
83 | 85 | return new CompiledRoute(
|
84 | 86 | $route,
|
85 | 87 | 'text' === $tokens[0][0] ? $tokens[0][1] : '',
|
86 |
| - sprintf("#^%s$#s", $regexp), |
| 88 | + self::REGEX_DELIMITER.'^'.$regexp.'$'.self::REGEX_DELIMITER.'s', |
87 | 89 | array_reverse($tokens),
|
88 | 90 | $variables
|
89 | 91 | );
|
90 | 92 | }
|
91 | 93 |
|
92 | 94 | /**
|
93 |
| - * Computes the regexp used to match the token. |
| 95 | + * Computes the regexp used to match a specific token. It can be static text or a subpattern. |
94 | 96 | *
|
95 | 97 | * @param array $tokens The route tokens
|
96 | 98 | * @param integer $index The index of the current token
|
97 | 99 | * @param integer $firstOptional The index of the first optional token
|
98 | 100 | *
|
99 |
| - * @return string The regexp |
| 101 | + * @return string The regexp pattern for a single token |
100 | 102 | */
|
101 | 103 | private function computeRegexp(array $tokens, $index, $firstOptional)
|
102 | 104 | {
|
103 | 105 | $token = $tokens[$index];
|
104 | 106 | if('text' === $token[0]) {
|
105 | 107 | // Text tokens
|
106 |
| - return preg_quote($token[1], '#'); |
| 108 | + return preg_quote($token[1], self::REGEX_DELIMITER); |
107 | 109 | } else {
|
108 | 110 | // Variable tokens
|
109 | 111 | if (0 === $index && 0 === $firstOptional && 1 == count($tokens)) {
|
110 | 112 | // When the only token is an optional variable token, the separator is required
|
111 |
| - return sprintf('%s(?<%s>%s)?', preg_quote($token[1], '#'), $token[3], $token[2]); |
| 113 | + return sprintf('%s(?<%s>%s)?', preg_quote($token[1], self::REGEX_DELIMITER), $token[3], $token[2]); |
112 | 114 | } else {
|
113 |
| - $nbTokens = count($tokens); |
114 |
| - $regexp = sprintf('%s(?<%s>%s)', preg_quote($token[1], '#'), $token[3], $token[2]); |
| 115 | + $regexp = sprintf('%s(?<%s>%s)', preg_quote($token[1], self::REGEX_DELIMITER), $token[3], $token[2]); |
115 | 116 | if ($index >= $firstOptional) {
|
116 |
| - // Enclose each optional tokens in a subpattern to make it optional |
| 117 | + // Enclose each optional token in a subpattern to make it optional. |
| 118 | + // "?:" means it is non-capturing, i.e. the portion of the subject string that |
| 119 | + // matched the optional subpattern is not passed back. |
117 | 120 | $regexp = "(?:$regexp";
|
| 121 | + $nbTokens = count($tokens); |
118 | 122 | if ($nbTokens - 1 == $index) {
|
119 | 123 | // Close the optional subpatterns
|
120 | 124 | $regexp .= str_repeat(")?", $nbTokens - $firstOptional);
|
|
0 commit comments