From 5d264ce9b6c292d624fcfea48e275357551a4cd8 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Thu, 27 Dec 2012 04:11:08 +0100 Subject: [PATCH] [Routing] made RouteCompilerInterface::compile static --- src/Symfony/Component/Routing/CHANGELOG.md | 2 ++ src/Symfony/Component/Routing/Route.php | 11 ++++------- src/Symfony/Component/Routing/RouteCompiler.php | 16 ++++++++-------- .../Component/Routing/RouteCompilerInterface.php | 5 ++++- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index af5f753306..14b9065fef 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -79,6 +79,8 @@ CHANGELOG pass the requirements (otherwise it would break your link anyway). * There is no restriction on the route name anymore. So non-alphanumeric characters are now also allowed. + * [BC BREAK] `RouteCompilerInterface::compile(Route $route)` was made static + (only relevant if you implemented your own RouteCompiler). 2.1.0 ----- diff --git a/src/Symfony/Component/Routing/Route.php b/src/Symfony/Component/Routing/Route.php index 241c8ef36e..701cf529e4 100644 --- a/src/Symfony/Component/Routing/Route.php +++ b/src/Symfony/Component/Routing/Route.php @@ -50,8 +50,6 @@ class Route implements \Serializable */ private $compiled; - private static $compilers = array(); - /** * Constructor. * @@ -421,6 +419,9 @@ class Route implements \Serializable * * @return CompiledRoute A CompiledRoute instance * + * @throws \LogicException If the Route cannot be compiled because the + * path or hostname pattern is invalid + * * @see RouteCompiler which is responsible for the compilation process */ public function compile() @@ -431,11 +432,7 @@ class Route implements \Serializable $class = $this->getOption('compiler_class'); - if (!isset(self::$compilers[$class])) { - self::$compilers[$class] = new $class; - } - - return $this->compiled = self::$compilers[$class]->compile($this); + return $this->compiled = $class::compile($this); } private function sanitizeRequirement($key, $regex) diff --git a/src/Symfony/Component/Routing/RouteCompiler.php b/src/Symfony/Component/Routing/RouteCompiler.php index 991ce0985b..70ebe77894 100644 --- a/src/Symfony/Component/Routing/RouteCompiler.php +++ b/src/Symfony/Component/Routing/RouteCompiler.php @@ -35,7 +35,7 @@ class RouteCompiler implements RouteCompilerInterface * @throws \DomainException If a variable name is numeric because PHP raises an error for such * subpatterns in PCRE and thus would break matching, e.g. "(?P<123>.+)". */ - public function compile(Route $route) + public static function compile(Route $route) { $staticPrefix = null; $hostnameVariables = array(); @@ -47,7 +47,7 @@ class RouteCompiler implements RouteCompilerInterface $hostnameTokens = array(); if ('' !== $hostnamePattern = $route->getHostnamePattern()) { - $result = $this->compilePattern($route, $hostnamePattern, true); + $result = self::compilePattern($route, $hostnamePattern, true); $hostnameVariables = $result['variables']; $variables = array_merge($variables, $hostnameVariables); @@ -58,7 +58,7 @@ class RouteCompiler implements RouteCompilerInterface $pattern = $route->getPattern(); - $result = $this->compilePattern($route, $pattern, false); + $result = self::compilePattern($route, $pattern, false); $staticPrefix = $result['staticPrefix']; @@ -80,7 +80,7 @@ class RouteCompiler implements RouteCompilerInterface ); } - private function compilePattern(Route $route, $pattern, $isHostname) + private static function compilePattern(Route $route, $pattern, $isHostname) { $tokens = array(); $variables = array(); @@ -122,7 +122,7 @@ class RouteCompiler implements RouteCompilerInterface // If {page} would also match the separating dot, {_format} would never match as {page} will eagerly consume everything. // Also even if {_format} was not optional the requirement prevents that {page} matches something that was originally // part of {_format} when generating the URL, e.g. _format = 'mobile.html'. - $nextSeparator = $this->findNextSeparator($followingPattern); + $nextSeparator = self::findNextSeparator($followingPattern); $regexp = sprintf( '[^%s%s]+', preg_quote($defaultSeparator, self::REGEX_DELIMITER), @@ -162,7 +162,7 @@ class RouteCompiler implements RouteCompilerInterface // compute the matching regexp $regexp = ''; for ($i = 0, $nbToken = count($tokens); $i < $nbToken; $i++) { - $regexp .= $this->computeRegexp($tokens, $i, $firstOptional); + $regexp .= self::computeRegexp($tokens, $i, $firstOptional); } return array( @@ -180,7 +180,7 @@ class RouteCompiler implements RouteCompilerInterface * * @return string The next static character that functions as separator (or empty string when none available) */ - private function findNextSeparator($pattern) + private static function findNextSeparator($pattern) { if ('' == $pattern) { // return empty string if pattern is empty or false (false which can be returned by substr) @@ -201,7 +201,7 @@ class RouteCompiler implements RouteCompilerInterface * * @return string The regexp pattern for a single token */ - private function computeRegexp(array $tokens, $index, $firstOptional) + private static function computeRegexp(array $tokens, $index, $firstOptional) { $token = $tokens[$index]; if ('text' === $token[0]) { diff --git a/src/Symfony/Component/Routing/RouteCompilerInterface.php b/src/Symfony/Component/Routing/RouteCompilerInterface.php index f72e9e5ef4..36e131c2ff 100644 --- a/src/Symfony/Component/Routing/RouteCompilerInterface.php +++ b/src/Symfony/Component/Routing/RouteCompilerInterface.php @@ -24,6 +24,9 @@ interface RouteCompilerInterface * @param Route $route A Route instance * * @return CompiledRoute A CompiledRoute instance + * + * @throws \LogicException If the Route cannot be compiled because the + * path or hostname pattern is invalid */ - public function compile(Route $route); + public static function compile(Route $route); }