-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Routing] Add support for aliasing routes #38389
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,35 @@ | ||
<?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; | ||
|
||
final class Alias | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* Returns the name of this alias. | ||
* | ||
* @return string The alias name | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return $this->name; | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
namespace Symfony\Component\Routing; | ||
|
||
use Symfony\Component\Config\Resource\ResourceInterface; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
|
||
/** | ||
* A RouteCollection represents a set of Route instances. | ||
|
@@ -30,6 +32,11 @@ class RouteCollection implements \IteratorAggregate, \Countable | |
*/ | ||
private $routes = []; | ||
|
||
/** | ||
* @var Alias[] | ||
*/ | ||
private $aliases = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
|
@@ -45,6 +52,10 @@ public function __clone() | |
foreach ($this->routes as $name => $route) { | ||
$this->routes[$name] = clone $route; | ||
} | ||
|
||
foreach ($this->aliases as $alias => $route) { | ||
$this->aliases[$alias] = clone $route; | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -80,7 +91,7 @@ public function add(string $name, Route $route/*, int $priority = 0*/) | |
trigger_deprecation('symfony/routing', '5.1', 'The "%s()" method will have a new "int $priority = 0" argument in version 6.0, not defining it is deprecated.', __METHOD__); | ||
} | ||
|
||
unset($this->routes[$name], $this->priorities[$name]); | ||
unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]); | ||
|
||
$this->routes[$name] = $route; | ||
|
||
|
@@ -114,6 +125,10 @@ public function all() | |
*/ | ||
public function get(string $name) | ||
{ | ||
if (isset($this->aliases[$name])) { | ||
return $this->get((string) $this->aliases[$name]); | ||
} | ||
|
||
return isset($this->routes[$name]) ? $this->routes[$name] : null; | ||
} | ||
|
||
|
@@ -125,7 +140,7 @@ public function get(string $name) | |
public function remove($name) | ||
{ | ||
foreach ((array) $name as $n) { | ||
unset($this->routes[$n], $this->priorities[$n]); | ||
unset($this->routes[$n], $this->priorities[$n], $this->aliases[$n]); | ||
} | ||
} | ||
|
||
|
@@ -138,14 +153,18 @@ public function addCollection(self $collection) | |
// we need to remove all routes with the same names first because just replacing them | ||
// would not place the new route at the end of the merged array | ||
foreach ($collection->all() as $name => $route) { | ||
unset($this->routes[$name], $this->priorities[$name]); | ||
unset($this->routes[$name], $this->priorities[$name], $this->aliases[$name]); | ||
$this->routes[$name] = $route; | ||
|
||
if (isset($collection->priorities[$name])) { | ||
$this->priorities[$name] = $collection->priorities[$name]; | ||
} | ||
} | ||
|
||
foreach ($collection->getAliases() as $alias => $route) { | ||
$this->setAlias($alias, $route); | ||
} | ||
|
||
foreach ($collection->getResources() as $resource) { | ||
$this->addResource($resource); | ||
} | ||
|
@@ -303,4 +322,65 @@ public function addResource(ResourceInterface $resource) | |
$this->resources[$key] = $resource; | ||
} | ||
} | ||
|
||
/** | ||
* Sets an alias for an existing route. | ||
* | ||
* @param string $alias The alias to create | ||
* @param string|Alias $route The route to alias | ||
* | ||
* @throws InvalidArgumentException if the id is not a string or an Alias | ||
* @throws InvalidArgumentException if the alias is for itself | ||
*/ | ||
public function setAlias(string $alias, $route): Alias | ||
{ | ||
if (\is_string($route)) { | ||
$route = new Alias($route); | ||
} elseif (!$route instanceof Alias) { | ||
throw new InvalidArgumentException('$route must be a string, or an Alias object.'); | ||
} | ||
|
||
if ($alias === (string) $route) { | ||
throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias)); | ||
} | ||
|
||
unset($this->routes[$alias], $this->priorities[$alias]); | ||
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. Add a comment to explain why you unset these keys in these array? |
||
|
||
return $this->aliases[$alias] = $route; | ||
} | ||
|
||
/** | ||
* @return array<string, Alias> | ||
*/ | ||
public function getAliases(): array | ||
{ | ||
return $this->aliases; | ||
} | ||
|
||
/** | ||
* @return string[] An array of resolved route names | ||
*/ | ||
public function resolveAliases(): array | ||
{ | ||
$resolvedAliases = []; | ||
foreach ($this->aliases as $alias => $target) { | ||
$resolved = $this->resolveAlias((string) $target); | ||
if (null === $resolved) { | ||
throw new RouteNotFoundException(sprintf('Target route "%s" for alias "%s" is not defined.', $target, $alias)); | ||
} | ||
|
||
$resolvedAliases[$alias] = $resolved; | ||
} | ||
|
||
return $resolvedAliases; | ||
} | ||
|
||
private function resolveAlias(string $alias): ?string | ||
stof marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (isset($this->aliases[$alias])) { | ||
return $this->resolveAlias((string) $this->aliases[$alias]); | ||
} | ||
|
||
return isset($this->routes[$alias]) ? $alias : null; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Symfony/Component/Routing/Tests/Fixtures/alias/alias.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,12 @@ | ||
<?php | ||
|
||
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; | ||
|
||
return static function (RoutingConfigurator $routes): void { | ||
$routes->add('route', '/hello'); | ||
$routes->add('overrided', '/'); | ||
$routes->alias('alias', 'route'); | ||
$routes->alias('alias2', 'route'); | ||
$routes->alias('deep', 'alias'); | ||
$routes->alias('overrided', 'route'); | ||
}; |
13 changes: 13 additions & 0 deletions
13
src/Symfony/Component/Routing/Tests/Fixtures/alias/alias.xml
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,13 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<routes xmlns="http://symfony.com/schema/routing" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/routing | ||
https://symfony.com/schema/routing/routing-1.0.xsd"> | ||
|
||
<route id="route" path="/hello"/> | ||
<route id="overrided" path="/"/> | ||
<route id="alias" alias="route"/> | ||
<route id="alias2" alias="route"/> | ||
<route id="deep" alias="alias"/> | ||
<route id="overrided" alias="route"/> | ||
</routes> |
12 changes: 12 additions & 0 deletions
12
src/Symfony/Component/Routing/Tests/Fixtures/alias/alias.yaml
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,12 @@ | ||
route: | ||
path: /hello | ||
overrided: | ||
path: / | ||
alias: | ||
alias: route | ||
alias2: | ||
alias: route | ||
deep: | ||
alias: alias | ||
_import: | ||
resource: override.yaml |
22 changes: 22 additions & 0 deletions
22
src/Symfony/Component/Routing/Tests/Fixtures/alias/expected.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,22 @@ | ||
<?php | ||
|
||
use Symfony\Component\Config\Resource\FileResource; | ||
use Symfony\Component\Routing\Route; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
return static function (string $format) { | ||
$expectedRoutes = new RouteCollection(); | ||
|
||
$expectedRoutes->add('route', new Route('/hello')); | ||
$expectedRoutes->setAlias('alias', 'route'); | ||
$expectedRoutes->setAlias('alias2', 'route'); | ||
$expectedRoutes->setAlias('deep', 'alias'); | ||
$expectedRoutes->setAlias('overrided', 'route'); | ||
|
||
$expectedRoutes->addResource(new FileResource(__DIR__."/alias.$format")); | ||
if ($format === 'yaml') { | ||
$expectedRoutes->addResource(new FileResource(__DIR__."/override.$format")); | ||
} | ||
|
||
return $expectedRoutes; | ||
}; |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/Routing/Tests/Fixtures/alias/override.yaml
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,2 @@ | ||
overrided: | ||
alias: route |
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.