8000 [Routing] small refactoring + language fixes · kimhemsoe/symfony@cb47b03 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb47b03

Browse files
committed
[Routing] small refactoring + language fixes
1 parent b47cb35 commit cb47b03

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function dump(array $options = array())
6868
}
6969
}
7070

71-
$hasTrailingSlash = (!$methods || in_array('HEAD', $methods)) && '/$' == substr($regex, -2) && '^/$' != $regex;
71+
$hasTrailingSlash = (!$methods || in_array('HEAD', $methods)) && '/$' === substr($regex, -2) && '^/$' !== $regex;
7272

7373
$variables = array('E=_ROUTING__route:'.$name);
7474
foreach ($compiledRoute->getVariables() as $i => $variable) {

src/Symfony/Component/Routing/RouteCompiler.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
class RouteCompiler implements RouteCompilerInterface
2020
{
21+
const REGEX_DELIMITER = '#';
22+
2123
/**
2224
* Compiles the current route instance.
2325
*
@@ -47,7 +49,7 @@ public function compile(Route $route)
4749
if ($pos !== $len) {
4850
$seps[] = $pattern[$pos];
4951
}
50-
$regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), '#'));
52+
$regexp = sprintf('[^%s]+?', preg_quote(implode('', array_unique($seps)), self::REGEX_DELIMITER));
5153
}
5254

5355
$tokens[] = array('variable', $match[0][0][0], $regexp, $var);
@@ -83,38 +85,40 @@ public function compile(Route $route)
8385
return new CompiledRoute(
8486
$route,
8587
'text' === $tokens[0][0] ? $tokens[0][1] : '',
86-
sprintf("#^%s$#s", $regexp),
88+
self::REGEX_DELIMITER.'^'.$regexp.'$'.self::REGEX_DELIMITER.'s',
8789
array_reverse($tokens),
8890
$variables
8991
);
9092
}
9193

9294
/**
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.
9496
*
9597
* @param array $tokens The route tokens
9698
* @param integer $index The index of the current token
9799
* @param integer $firstOptional The index of the first optional token
98100
*
99-
* @return string The regexp
101+
* @return string The regexp pattern for a single token
100102
*/
101103
private function computeRegexp(array $tokens, $index, $firstOptional)
102104
{
103105
$token = $tokens[$index];
104106
if('text' === $token[0]) {
105107
// Text tokens
106-
return preg_quote($token[1], '#');
108+
return preg_quote($token[1], self::REGEX_DELIMITER);
107109
} else {
108110
// Variable tokens
109111
if (0 === $index && 0 === $firstOptional && 1 == count($tokens)) {
110112
// 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]);
112114
} 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]);
115116
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.
117120
$regexp = "(?:$regexp";
121+
$nbTokens = count($tokens);
118122
if ($nbTokens - 1 == $index) {
119123
// Close the optional subpatterns
120124
$regexp .= str_repeat(")?", $nbTokens - $firstOptional);

src/Symfony/Component/Routing/RouteCompilerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Routing;
1313

1414
/**
15-
* RouteCompilerInterface is the interface that all RouteCompiler classes must implements.
15+
* RouteCompilerInterface is the interface that all RouteCompiler classes must implement.
1616
*
1717
* @author Fabien Potencier <fabien@symfony.com>
1818
*/

0 commit comments

Comments
 (0)
0