Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitefeefc6

Browse files
[FrameworkBundle] Auto-register#[Route] attributes found on services tagged withrouting.controller
1 parented38673 commitefeefc6

File tree

7 files changed

+79
-3
lines changed

7 files changed

+79
-3
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.4
55
---
66

7+
* Auto-register`#[Route]` attributes found on services tagged with`routing.controller`
78
* Add`ControllerHelper`; the helpers from AbstractController as a standalone service
89
* Allow using their name without added suffix when using`#[Target]` for custom services
910
* Deprecate`Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of`Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
useSymfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
useSymfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
16+
useSymfony\Component\DependencyInjection\ContainerBuilder;
17+
18+
/**
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
class RoutingControllerPassimplements CompilerPassInterface
22+
{
23+
use PriorityTaggedServiceTrait;
24+
25+
publicfunctionprocess(ContainerBuilder$container):void
26+
{
27+
if (!$container->hasDefinition('routing.loader.attribute')) {
28+
return;
29+
}
30+
31+
$resolve =$container->getParameterBag()->resolveValue(...);
32+
$taggedClasses = [];
33+
foreach ($this->findAndSortTaggedServices('routing.controller',$container)as$id) {
34+
$taggedClasses[$resolve($container->getDefinition($id)->getClass())] =true;
35+
}
36+
37+
$container->getDefinition('routing.loader.attribute')
38+
->replaceArgument(1,array_keys($taggedClasses));
39+
}
40+
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class UnusedTagsPass implements CompilerPassInterface
7878
'proxy',
7979
'remote_event.consumer',
8080
'routing.condition_service',
81+
'routing.controller',
8182
'routing.expression_language_function',
8283
'routing.expression_language_provider',
8384
'routing.loader',

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ public function load(array $configs, ContainerBuilder $container): void
692692
$container->registerForAutoconfiguration(ValueResolverInterface::class)
693693
->addTag('controller.argument_value_resolver');
694694
$container->registerForAutoconfiguration(AbstractController::class)
695-
->addTag('controller.service_arguments');
695+
->addTag('controller.service_arguments')->addTag('routing.controller');
696696
$container->registerForAutoconfiguration(DataCollectorInterface::class)
697697
->addTag('data_collector');
698698
$container->registerForAutoconfiguration(FormTypeInterface::class)
@@ -765,10 +765,10 @@ public function load(array $configs, ContainerBuilder $container): void
765765
$definition->addTag('kernel.event_listener',$tagAttributes);
766766
});
767767
$container->registerAttributeForAutoconfiguration(AsController::class,staticfunction (ChildDefinition$definition,AsController$attribute):void {
768-
$definition->addTag('controller.service_arguments');
768+
$definition->addTag('controller.service_arguments')->addTag('routing.controller');
769769
});
770770
$container->registerAttributeForAutoconfiguration(Route::class,staticfunction (ChildDefinition$definition,Route$attribute,\ReflectionClass|\ReflectionMethod$reflection):void {
771-
$definition->addTag('controller.service_arguments');
771+
$definition->addTag('controller.service_arguments')->addTag('routing.controller');
772772
});
773773
$container->registerAttributeForAutoconfiguration(AsRemoteEventConsumer::class,staticfunction (ChildDefinition$definition,AsRemoteEventConsumer$attribute):void {
774774
$definition->addTag('remote_event.consumer', ['consumer' =>$attribute->name]);

‎src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ErrorLoggerCompilerPass;
1919
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
2020
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RemoveUnusedSessionMarshallingHandlerPass;
21+
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingControllerPass;
2122
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerRealRefPass;
2223
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass;
2324
useSymfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationLintCommandPass;
@@ -146,6 +147,7 @@ public function build(ContainerBuilder $container): void
146147
$container->addCompilerPass(newRegisterControllerArgumentLocatorsPass());
147148
$container->addCompilerPass(newRemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);
148149
$container->addCompilerPass(newRoutingResolverPass());
150+
$container->addCompilerPass(newRoutingControllerPass());
149151
$this->addCompilerPassIfExists($container, DataCollectorTranslatorPass::class);
150152
$container->addCompilerPass(newProfilerPass());
151153
// must be registered before removing private services as some might be listeners/subscribers

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
->set('routing.loader.attribute', AttributeRouteControllerLoader::class)
9696
->args([
9797
'%kernel.environment%',
98+
abstract_arg('classes tagged with "routing.controller"'),
9899
])
99100
->tag('routing.loader', ['priority' => -10])
100101

‎src/Symfony/Bundle/FrameworkBundle/Routing/AttributeRouteControllerLoader.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
useSymfony\Component\Routing\Loader\AttributeClassLoader;
1515
useSymfony\Component\Routing\Route;
16+
useSymfony\Component\Routing\RouteCollection;
1617

1718
/**
1819
* AttributeRouteControllerLoader is an implementation of AttributeClassLoader
@@ -23,6 +24,36 @@
2324
*/
2425
class AttributeRouteControllerLoaderextends AttributeClassLoader
2526
{
27+
/**
28+
* @param class-string[] $taggedClasses
29+
*/
30+
publicfunction__construct(
31+
?string$env =null,
32+
privatearray$taggedClasses = [],
33+
) {
34+
parent::__construct($env);
35+
}
36+
37+
publicfunctionload(mixed$class, ?string$type =null):RouteCollection
38+
{
39+
if (['tag' =>'routing.controller'] !==$class) {
40+
returnparent::load($class,$type);
41+
}
42+
43+
$collection =newRouteCollection();
44+
45+
foreach ($this->taggedClassesas$class) {
46+
$collection->addCollection(parent::load($class,$type));
47+
}
48+
49+
return$collection;
50+
}
51+
52+
publicfunctionsupports(mixed$resource, ?string$type =null):bool
53+
{
54+
returnparent::supports($resource,$type) ||'attribute' ===$type && ['tag' =>'routing.controller'] ===$resource;
55+
}
56+
2657
/**
2758
* Configures the _controller default parameter of a given Route instance.
2859
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp