[Routing] made RouteCompilerInterface::compile static
This commit is contained in:
parent
1f281db261
commit
5d264ce9b6
@ -79,6 +79,8 @@ CHANGELOG
|
|||||||
pass the requirements (otherwise it would break your link anyway).
|
pass the requirements (otherwise it would break your link anyway).
|
||||||
* There is no restriction on the route name anymore. So non-alphanumeric characters
|
* There is no restriction on the route name anymore. So non-alphanumeric characters
|
||||||
are now also allowed.
|
are now also allowed.
|
||||||
|
* [BC BREAK] `RouteCompilerInterface::compile(Route $route)` was made static
|
||||||
|
(only relevant if you implemented your own RouteCompiler).
|
||||||
|
|
||||||
2.1.0
|
2.1.0
|
||||||
-----
|
-----
|
||||||
|
@ -50,8 +50,6 @@ class Route implements \Serializable
|
|||||||
*/
|
*/
|
||||||
private $compiled;
|
private $compiled;
|
||||||
|
|
||||||
private static $compilers = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
@ -421,6 +419,9 @@ class Route implements \Serializable
|
|||||||
*
|
*
|
||||||
* @return CompiledRoute A CompiledRoute instance
|
* @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
|
* @see RouteCompiler which is responsible for the compilation process
|
||||||
*/
|
*/
|
||||||
public function compile()
|
public function compile()
|
||||||
@ -431,11 +432,7 @@ class Route implements \Serializable
|
|||||||
|
|
||||||
$class = $this->getOption('compiler_class');
|
$class = $this->getOption('compiler_class');
|
||||||
|
|
||||||
if (!isset(self::$compilers[$class])) {
|
return $this->compiled = $class::compile($this);
|
||||||
self::$compilers[$class] = new $class;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->compiled = self::$compilers[$class]->compile($this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sanitizeRequirement($key, $regex)
|
private function sanitizeRequirement($key, $regex)
|
||||||
|
@ -35,7 +35,7 @@ class RouteCompiler implements RouteCompilerInterface
|
|||||||
* @throws \DomainException If a variable name is numeric because PHP raises an error for such
|
* @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>.+)".
|
* 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;
|
$staticPrefix = null;
|
||||||
$hostnameVariables = array();
|
$hostnameVariables = array();
|
||||||
@ -47,7 +47,7 @@ class RouteCompiler implements RouteCompilerInterface
|
|||||||
$hostnameTokens = array();
|
$hostnameTokens = array();
|
||||||
|
|
||||||
if ('' !== $hostnamePattern = $route->getHostnamePattern()) {
|
if ('' !== $hostnamePattern = $route->getHostnamePattern()) {
|
||||||
$result = $this->compilePattern($route, $hostnamePattern, true);
|
$result = self::compilePattern($route, $hostnamePattern, true);
|
||||||
|
|
||||||
$hostnameVariables = $result['variables'];
|
$hostnameVariables = $result['variables'];
|
||||||
$variables = array_merge($variables, $hostnameVariables);
|
$variables = array_merge($variables, $hostnameVariables);
|
||||||
@ -58,7 +58,7 @@ class RouteCompiler implements RouteCompilerInterface
|
|||||||
|
|
||||||
$pattern = $route->getPattern();
|
$pattern = $route->getPattern();
|
||||||
|
|
||||||
$result = $this->compilePattern($route, $pattern, false);
|
$result = self::compilePattern($route, $pattern, false);
|
||||||
|
|
||||||
$staticPrefix = $result['staticPrefix'];
|
$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();
|
$tokens = array();
|
||||||
$variables = 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.
|
// 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
|
// 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'.
|
// part of {_format} when generating the URL, e.g. _format = 'mobile.html'.
|
||||||
$nextSeparator = $this->findNextSeparator($followingPattern);
|
$nextSeparator = self::findNextSeparator($followingPattern);
|
||||||
$regexp = sprintf(
|
$regexp = sprintf(
|
||||||
'[^%s%s]+',
|
'[^%s%s]+',
|
||||||
preg_quote($defaultSeparator, self::REGEX_DELIMITER),
|
preg_quote($defaultSeparator, self::REGEX_DELIMITER),
|
||||||
@ -162,7 +162,7 @@ class RouteCompiler implements RouteCompilerInterface
|
|||||||
// compute the matching regexp
|
// compute the matching regexp
|
||||||
$regexp = '';
|
$regexp = '';
|
||||||
for ($i = 0, $nbToken = count($tokens); $i < $nbToken; $i++) {
|
for ($i = 0, $nbToken = count($tokens); $i < $nbToken; $i++) {
|
||||||
$regexp .= $this->computeRegexp($tokens, $i, $firstOptional);
|
$regexp .= self::computeRegexp($tokens, $i, $firstOptional);
|
||||||
}
|
}
|
||||||
|
|
||||||
return array(
|
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)
|
* @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) {
|
if ('' == $pattern) {
|
||||||
// return empty string if pattern is empty or false (false which can be returned by substr)
|
// 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
|
* @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];
|
$token = $tokens[$index];
|
||||||
if ('text' === $token[0]) {
|
if ('text' === $token[0]) {
|
||||||
|
@ -24,6 +24,9 @@ interface RouteCompilerInterface
|
|||||||
* @param Route $route A Route instance
|
* @param Route $route A Route instance
|
||||||
*
|
*
|
||||||
* @return CompiledRoute A CompiledRoute 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);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user