-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Routing] Allow using services in the route condition #44405
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
Tobion
merged 1 commit into
symfony:6.1
from
renanbr:inject-variable-route-expression-condition
Apr 14, 2022
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
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
47 changes: 47 additions & 0 deletions
47
src/Symfony/Bundle/FrameworkBundle/Routing/Attribute/AsRoutingConditionService.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,47 @@ | ||
| <?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\Bundle\FrameworkBundle\Routing\Attribute; | ||
|
|
||
| /** | ||
| * Service tag to autoconfigure routing condition services. | ||
| * | ||
| * You can tag a service: | ||
| * | ||
| * #[AsRoutingConditionService('foo')] | ||
| * class SomeFooService | ||
| * { | ||
| * public function bar(): bool | ||
| * { | ||
| * // ... | ||
| * } | ||
| * } | ||
| * | ||
| * Then you can use the tagged service in the routing condition: | ||
| * | ||
| * class PageController | ||
| * { | ||
| * #[Route('/page', condition: "service('foo').bar()")] | ||
| * public function page(): Response | ||
| * { | ||
| * // ... | ||
| * } | ||
| * } | ||
| */ | ||
| #[\Attribute(\Attribute::TARGET_CLASS)] | ||
| class AsRoutingConditionService | ||
| { | ||
| public function __construct( | ||
| public ?string $alias = null, | ||
| public int $priority = 0, | ||
| ) { | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
...le/Tests/Functional/Bundle/RoutingConditionServiceBundle/Controller/DefaultController.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,81 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Controller; | ||
|
|
||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\Routing\Annotation\Route; | ||
|
|
||
| class DefaultController | ||
| { | ||
| #[Route( | ||
| path: '/allowed/manually-tagged', | ||
| condition: 'service("manually_tagged").giveMeTrue()', | ||
| )] | ||
| public function allowedByManuallyTagged(): Response | ||
| { | ||
| return new Response(); | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/allowed/auto-configured', | ||
| condition: 'service("auto_configured").flip(false)', | ||
| )] | ||
| public function allowedByAutoConfigured(): Response | ||
| { | ||
| return new Response(); | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/allowed/auto-configured-non-aliased', | ||
| condition: 'service("auto_configured_non_aliased").alwaysTrue()', | ||
| )] | ||
| public function allowedByAutoConfiguredNonAliased(): Response | ||
| { | ||
| return new Response(); | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/denied/manually-tagged', | ||
| condition: 'service("manually_tagged").giveMeFalse()', | ||
| )] | ||
| public function deniedByManuallyTagged(): Response | ||
| { | ||
| return new Response(); | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/denied/auto-configured', | ||
| condition: 'service("auto_configured").flip(true)', | ||
| )] | ||
| public function deniedByAutoConfigured(): Response | ||
| { | ||
| return new Response(); | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/denied/auto-configured-non-aliased', | ||
| condition: 'service("auto_configured_non_aliased").alwaysFalse()', | ||
| )] | ||
| public function deniedByAutoConfiguredNonAliased(): Response | ||
| { | ||
| return new Response(); | ||
| } | ||
|
|
||
| #[Route( | ||
| path: '/denied/overridden', | ||
| condition: 'service("foo").isAllowed()', | ||
| )] | ||
| public function deniedByOverriddenAlias(): Response | ||
| { | ||
| return new Response(); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
...e/Tests/Functional/Bundle/RoutingConditionServiceBundle/RoutingConditionServiceBundle.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,18 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle; | ||
|
|
||
| use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
|
||
| class RoutingConditionServiceBundle extends Bundle | ||
| { | ||
| } |
28 changes: 28 additions & 0 deletions
28
...nctional/Bundle/RoutingConditionServiceBundle/Service/AutoConfiguredNonAliasedService.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,28 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service; | ||
|
|
||
| use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService; | ||
|
|
||
| #[AsRoutingConditionService] | ||
| class AutoConfiguredNonAliasedService | ||
| { | ||
| public function alwaysFalse(): bool | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| public function alwaysTrue(): bool | ||
| { | ||
| return true; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...e/Tests/Functional/Bundle/RoutingConditionServiceBundle/Service/AutoConfiguredService.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,23 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service; | ||
|
|
||
| use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService; | ||
|
|
||
| #[AsRoutingConditionService('auto_configured')] | ||
| class AutoConfiguredService | ||
| { | ||
| public function flip(bool $value): bool | ||
| { | ||
| return !$value; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...ndle/Tests/Functional/Bundle/RoutingConditionServiceBundle/Service/FooOriginalService.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,23 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service; | ||
|
|
||
| use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService; | ||
|
|
||
| #[AsRoutingConditionService('foo')] | ||
| class FooOriginalService | ||
| { | ||
| public function isAllowed(): bool | ||
| { | ||
| return true; | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...e/Tests/Functional/Bundle/RoutingConditionServiceBundle/Service/FooReplacementService.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,23 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service; | ||
|
|
||
| use Symfony\Bundle\FrameworkBundle\Routing\Attribute\AsRoutingConditionService; | ||
|
|
||
| #[AsRoutingConditionService(alias: 'foo', priority: -1)] | ||
| class FooReplacementService | ||
| { | ||
| public function isAllowed(): bool | ||
| { | ||
| return false; | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
...e/Tests/Functional/Bundle/RoutingConditionServiceBundle/Service/ManuallyTaggedService.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,25 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\RoutingConditionServiceBundle\Service; | ||
|
|
||
| class ManuallyTaggedService | ||
| { | ||
| public function giveMeTrue(): bool | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| public function giveMeFalse(): bool | ||
| { | ||
| return false; | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/RoutingConditionServiceTest.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,64 @@ | ||
| <?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\Bundle\FrameworkBundle\Tests\Functional; | ||
|
|
||
| class RoutingConditionServiceTest extends AbstractWebTestCase | ||
| { | ||
| /** | ||
| * @dataProvider provideRoutes | ||
| */ | ||
| public function testCondition(int $code, string $path) | ||
| { | ||
| $client = static::createClient(['test_case' => 'RoutingConditionService']); | ||
|
|
||
| $client->request('GET', $path); | ||
| $this->assertSame($code, $client->getResponse()->getStatusCode()); | ||
| } | ||
|
|
||
| public function provideRoutes(): iterable | ||
| { | ||
| yield 'allowed by an autoconfigured service' => [ | ||
| 200, | ||
| '/allowed/manually-tagged', | ||
| ]; | ||
|
|
||
| yield 'allowed by a manually tagged service' => [ | ||
| 200, | ||
| '/allowed/auto-configured', | ||
| ]; | ||
|
|
||
| yield 'allowed by a manually tagged non aliased service' => [ | ||
| 200, | ||
| '/allowed/auto-configured-non-aliased', | ||
| ]; | ||
|
|
||
| yield 'denied by an autoconfigured service' => [ | ||
| 404, | ||
| '/denied/manually-tagged', | ||
| ]; | ||
|
|
||
| yield 'denied by a manually tagged service' => [ | ||
| 404, | ||
| '/denied/auto-configured', | ||
| ]; | ||
|
|
||
| yield 'denied by a manually tagged non aliased service' => [ | ||
| 404, | ||
| '/denied/auto-configured-non-aliased', | ||
| ]; | ||
|
|
||
| yield 'denied by an overridden service' => [ | ||
| 404, | ||
| '/denied/overridden', | ||
| ]; | ||
| } | ||
| } |
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.