-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Add support for aliasing routes #38464
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing; | ||
|
||
use Symfony\Component\Routing\Exception\InvalidArgumentException; | ||
|
||
class Alias | ||
{ | ||
private $id; | ||
private $deprecation = []; | ||
|
||
public function __construct(string $id) | ||
{ | ||
$this->id = $id; | ||
} | ||
|
||
/** | ||
* @return static | ||
*/ | ||
public function withId(string $id): self | ||
{ | ||
$new = clone $this; | ||
|
||
$new->id = $id; | ||
|
||
return $new; | ||
} | ||
|
||
/** | ||
* Returns the target name of this alias. | ||
* | ||
* @return string The target name | ||
*/ | ||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* Whether this alias is deprecated, that means it should not be referenced anymore. | ||
* | ||
* @param string $package The name of the composer package that is triggering the deprecation | ||
* @param string $version The version of the package that introduced the deprecation | ||
* @param string $message The deprecation message to use | ||
* | ||
* @return $this | ||
* | ||
* @throws InvalidArgumentException when the message template is invalid | ||
*/ | ||
public function setDeprecated(string $package, string $version, string $message): self | ||
{ | ||
if ('' !== $message) { | ||
if (preg_match('#[\r\n]|\*/#', $message)) { | ||
throw new InvalidArgumentException('Invalid characters found in deprecation template.'); | ||
} | ||
|
||
if (!str_contains($message, '%alias_id%')) { | ||
throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.'); | ||
} | ||
} | ||
|
||
$this->deprecation = [ | ||
'package' => $package, | ||
'version' => $version, | ||
'message' => $message ?: 'The "%alias_id%" route alias is deprecated. You should stop using it, as it will be removed in the future.', | ||
]; | ||
|
||
return $this; | ||
} | ||
|
||
public function isDeprecated(): bool | ||
{ | ||
return (bool) $this->deprecation; | ||
} | ||
|
||
/** | ||
* @param string $name Route name relying on this alias | ||
*/ | ||
public function getDeprecation(string $name): array | ||
{ | ||
return [ | ||
'package' => $this->deprecation['package'], | ||
'version' => $this->deprecation['version'], | ||
'message' => str_replace('%alias_id%', $name, $this->deprecation['message']), | ||
]; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Symfony/Component/Routing/Exception/InvalidArgumentException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Exception; | ||
|
||
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface | ||
{ | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Symfony/Component/Routing/Exception/RouteCircularReferenceException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Exception; | ||
|
||
class RouteCircularReferenceException extends RuntimeException | ||
{ | ||
public function __construct(string $routeId, array $path) | ||
{ | ||
parent::__construct(sprintf('Circular reference detected for route "%s", path: "%s".', $routeId, implode(' -> ', $path))); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Symfony/Component/Routing/Exception/RuntimeException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Exception; | ||
|
||
class RuntimeException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
namespace Symfony\Component\Routing\Generator\Dumper; | ||
|
||
use Symfony\Component\Routing\Exception\RouteCircularReferenceException; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
use Symfony\Component\Routing\Matcher\Dumper\CompiledUrlMatcherDumper; | ||
|
||
/** | ||
|
@@ -35,12 +37,57 @@ public function getCompiledRoutes(): array | |
$compiledRoute->getTokens(), | ||
$compiledRoute->getHostTokens(), | ||
$route->getSchemes(), | ||
[], | ||
]; | ||
} | ||
|
||
return $compiledRoutes; | ||
} | ||
|
||
public function getCompiledAliases(): array | ||
{ | ||
$routes = $this->getRoutes(); | ||
$compiledAliases = []; | ||
foreach ($routes->getAliases() as $name => $alias) { | ||
$deprecations = $alias->isDeprecated() ? [$alias->getDeprecation($name)] : []; | ||
$currentId = $alias->getId(); | ||
$visited = []; | ||
while (null !== $alias = $routes->getAlias($currentId) ?? null) { | ||
if (false !== $searchKey = array_search($currentId, $visited)) { | ||
$visited[] = $currentId; | ||
|
||
throw new RouteCircularReferenceException($currentId, \array_slice($visited, $searchKey)); | ||
} | ||
|
||
if ($alias->isDeprecated()) { | ||
$deprecations[] = $deprecation = $alias->getDeprecation($currentId); | ||
trigger_deprecation($deprecation['package'], $deprecation['version'], $deprecation['message']); | ||
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. targeting a deprecated alias should raise a deprecation, so this one is fine compared to the previous one |
||
} | ||
|
||
$visited[] = $currentId; | ||
$currentId = $alias->getId(); | ||
} | ||
|
||
if (null === $target = $routes->get($currentId)) { | ||
throw new RouteNotFoundException(sprintf('Target route "%s" for alias "%s" does not exist.', $currentId, $name)); | ||
} | ||
derrabus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$compiledTarget = $target->compile(); | ||
|
||
$compiledAliases[$name] = [ | ||
$compiledTarget->getVariables(), | ||
$target->getDefaults(), | ||
$target->getRequirements(), | ||
$compiledTarget->getTokens(), | ||
$compiledTarget->getHostTokens(), | ||
$target->getSchemes(), | ||
$deprecations, | ||
]; | ||
} | ||
|
||
return $compiledAliases; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -68,6 +115,10 @@ private function generateDeclaredRoutes(): string | |
$routes .= sprintf("\n '%s' => %s,", $name, CompiledUrlMatcherDumper::export($properties)); | ||
} | ||
|
||
foreach ($this->getCompiledAliases() as $alias => $properties) { | ||
$routes .= sprintf("\n '%s' => %s,", $alias, CompiledUrlMatcherDumper::export($properties)); | ||
} | ||
|
||
return $routes; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/Symfony/Component/Routing/Loader/Configurator/AliasConfigurator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader\Configurator; | ||
|
||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\Routing\Alias; | ||
|
||
class AliasConfigurator | ||
{ | ||
private $alias; | ||
|
||
public function __construct(Alias $alias) | ||
{ | ||
$this->alias = $alias; | ||
} | ||
|
||
/** | ||
* Whether this alias is deprecated, that means it should not be called anymore. | ||
* | ||
* @param string $package The name of the composer package that is triggering the deprecation | ||
* @param string $version The version of the package that introduced the deprecation | ||
* @param string $message The deprecation message to use | ||
* | ||
* @return $this | ||
* | ||
* @throws InvalidArgumentException when the message template is invalid | ||
*/ | ||
public function deprecate(string $package, string $version, string $message): self | ||
{ | ||
$this->alias->setDeprecated($package, $version, $message); | ||
|
||
return $this; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.