-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Routing] Handling of space characters in the dumpers #3858
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
- The route compiler does not add extra s 8000 pace or line-feed, - The generated regex does not use the 'x' modified any more, - The PHP and apache matchers do not need to strip any chars (vs space and line feed before), - The space characters are escaped according to the apache format
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,8 @@ class ApacheMatcherDumper extends MatcherDumper | |
| * @param array $options An array of options | ||
| * | ||
| * @return string A string to be used as Apache rewrite rules | ||
| * | ||
| * @throws \LogicException When the route regex is invalid | ||
| */ | ||
| public function dump(array $options = array()) | ||
| { | ||
|
|
@@ -39,15 +41,23 @@ public function dump(array $options = array()) | |
| 'base_uri' => '', | ||
| ), $options); | ||
|
|
||
| $options['script_name'] = self::escape($options['script_name'], ' ', '\\'); | ||
|
|
||
| $rules = array("# skip \"real\" requests\nRewriteCond %{REQUEST_FILENAME} -f\nRewriteRule .* - [QSA,L]"); | ||
| $methodVars = array(); | ||
|
|
||
| foreach ($this->getRoutes()->all() as $name => $route) { | ||
| $compiledRoute = $route->compile(); | ||
|
|
||
| // prepare the apache regex | ||
| $regex = preg_replace('/\?P<.+?>/', '', substr(str_replace(array("\n", ' '), '', $compiledRoute->getRegex()), 1, -3)); | ||
| $regex = '^'.preg_quote($options['base_uri']).substr($regex, 1); | ||
| $regex = $compiledRoute->getRegex(); | ||
| $delimiter = $regex[0]; | ||
| $regexPatternEnd = strrpos($regex, $delimiter); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the last delimiter-character is escaped? That would mean the regex is still invalid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't really validate the regex anyway. It just makes sure there the delimiters are there. |
||
| if (strlen($regex) < 2 || 0 === $regexPatternEnd) { | ||
| throw new \LogicException('The "%s" route regex "%s" is invalid', $name, $regex); | ||
| } | ||
| $regex = preg_replace('/\?P<.+?>/', '', substr($regex, 1, $regexPatternEnd - 1)); | ||
| $regex = '^'.self::escape(preg_quote($options['base_uri']).substr($regex, 1), ' ', '\\'); | ||
|
|
||
| $hasTrailingSlash = '/$' == substr($regex, -2) && '^/$' != $regex; | ||
|
|
||
|
|
@@ -56,7 +66,6 @@ public function dump(array $options = array()) | |
| $variables[] = 'E=_ROUTING_'.$variable.':%'.($i + 1); | ||
| } | ||
| foreach ($route->getDefaults() as $key => $value) { | ||
| // todo: a more legit way to escape the value? | ||
| $variables[] = 'E=_ROUTING_'.$key.':'.strtr($value, array( | ||
| ':' => '\\:', | ||
| '=' => '\\=', | ||
|
|
@@ -112,4 +121,36 @@ public function dump(array $options = array()) | |
|
|
||
| return implode("\n\n", $rules)."\n"; | ||
| } | ||
|
|
||
| /** | ||
| * Escapes a string. | ||
| * | ||
| * @param string $string The string to be escaped | ||
| * @param string $char The character to be escaped | ||
| * @param string $with The character to be used for escaping | ||
| * | ||
| * @return string The escaped string | ||
| */ | ||
| static private function escape($string, $char, $with) | ||
| { | ||
| $escaped = false; | ||
| $output = ''; | ||
| foreach(str_split($string) as $symbol) { | ||
| if ($escaped) { | ||
| $output .= $symbol; | ||
| $escaped = false; | ||
| continue; | ||
| } | ||
| if ($symbol === $char) { | ||
| $output .= $with.$char; | ||
| continue; | ||
| } | ||
| if ($symbol === $with) { | ||
| $escaped = true; | ||
| } | ||
| $output .= $symbol; | ||
| } | ||
|
|
||
| return $output; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a BC break in 2.0 and doesn't comply with the
MatcherDumperInterfaceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tobion it is not really a BC as it would not have work without a valid regexp even before