4 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 14
14
namespace ApiPlatform \Symfony \Bundle ;
15
15
16
16
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \AttributeFilterPass ;
17
+ use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \AttributeResourcePass ;
17
18
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \AuthenticatorManagerPass ;
18
19
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \DataProviderPass ;
19
20
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \ElasticsearchClientPass ;
@@ -47,6 +48,7 @@ public function build(ContainerBuilder $container): void
47
48
$ container ->addCompilerPass (new DataProviderPass ());
48
49
// Run the compiler pass before the {@see ResolveInstanceofConditionalsPass} to allow autoconfiguration of generated filter definitions.
49
50
$ container ->addCompilerPass (new AttributeFilterPass (), PassConfig::TYPE_BEFORE_OPTIMIZATION , 101 );
51
+ $ container ->addCompilerPass (new AttributeResourcePass ());
50
52
$ container ->addCompilerPass (new FilterPass ());
51
53
$ container ->addCompilerPass (new ElasticsearchClientPass ());
52
54
$ container ->addCompilerPass (new GraphQlTypePass ());
Original file line number Diff line number Diff line change 48
48
use Ramsey \Uuid \Uuid ;
49
49
use Symfony \Component \Config \FileLocator ;
50
50
use Symfony \Component \Config \Resource \DirectoryResource ;
51
+ use Symfony \Component \DependencyInjection \ChildDefinition ;
51
52
use Symfony \Component \DependencyInjection \ContainerBuilder ;
52
53
use Symfony \Component \DependencyInjection \Definition ;
53
54
use Symfony \Component \DependencyInjection \Exception \RuntimeException ;
@@ -168,6 +169,10 @@ public function load(array $configs, ContainerBuilder $container): void
168
169
->addTag ('api_platform.uri_variables.transformer ' );
169
170
$ container ->registerForAutoconfiguration (ParameterProviderInterface::class)
170
171
->addTag ('api_platform.parameter_provider ' );
172
+ $ container ->registerAttributeForAutoconfiguration (ApiResource::class, static function (ChildDefinition $ definition ): void {
173
+ $ definition ->addTag ('api_platform.resource ' );
174
+ $ definition ->addTag ('container.excluded ' , ['source ' => __FILE__ ]);
175
+ });
171
176
172
177
if (!$ container ->has ('api_platform.state.item_provider ' )) {
173
178
$ container ->setAlias ('api_platform.state.item_provider ' , 'api_platform.state_provider.object ' );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /*
4
+ * This file is part of the API Platform project.
5
+ *
6
+ * (c) Kévin Dunglas <dunglas@gmail.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
+ declare (strict_types=1 );
13
+
14
+ namespace ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler ;
15
+
16
+ use ApiPlatform \Metadata \ApiResource ;
17
+ use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
18
+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
19
+
20
+ /**
21
+ * Registers resource classes from {@see ApiResource} attribute.
22
+ *
23
+ * @internal
24
+ *
25
+ * @author Jérôme Tamarelle <jerome@tamarelle.net>
26
+ */
27
+ final class AttributeResourcePass implements CompilerPassInterface
28
+ {
29
+ /**
30
+ * {@inheritdoc}
31
+ */
32
+ public function process (ContainerBuilder $ container ): void
33
+ {
34
+ $ resourceClassDirectories = $ container ->getParameter ('api_platform.resource_class_directories ' );
35
+
36
+ // findTaggedServiceIds cannot be used, as the services are excluded
37
+ foreach ($ container ->getDefinitions () as $ id => $ definition ) {
38
+ if ($ definition ->hasTag ('api_platform.resource ' )) {
39
+ $ r = new \ReflectionClass ($ definition ->getClass ());
40
+ if ($ r ->getFileName ()) {
41
+ $ resourceClassDirectories [] = dirname ($ r ->getFileName ());
42
+ }
43
+ }
44
+ }
45
+ $ resourceClassDirectories = array_unique ($ resourceClassDirectories );
46
+ $ container ->setParameter ('api_platform.resource_class_directories ' , $ resourceClassDirectories );
47
+ }
48
+ }
Original file line number Diff line number Diff line change 15
15
16
16
use ApiPlatform \Symfony \Bundle \ApiPlatformBundle ;
17
17
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \AttributeFilterPass ;
18
+ use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \AttributeResourcePass ;
18
19
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \AuthenticatorManagerPass ;
19
20
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \DataProviderPass ;
20
21
use ApiPlatform \Symfony \Bundle \DependencyInjection \Compiler \ElasticsearchClientPass ;
@@ -45,6 +46,7 @@ public function testBuild(): void
45
46
$ containerProphecy = $ this ->prophesize (ContainerBuilder::class);
46
47
$ containerProphecy ->addCompilerPass (Argument::type (DataProviderPass::class))->willReturn ($ containerProphecy ->reveal ())->shouldBeCalled ();
47
48
$ containerProphecy ->addCompilerPass (Argument::type (AttributeFilterPass::class), PassConfig::TYPE_BEFORE_OPTIMIZATION , 101 )->willReturn ($ containerProphecy ->reveal ())->shouldBeCalled ();
49
+ $ containerProphecy ->addCompilerPass (Argument::type (AttributeResourcePass::class))->shouldBeCalled ()->willReturn ($ containerProphecy ->reveal ())->shouldBeCalled ();
48
50
$ containerProphecy ->addCompilerPass (Argument::type (FilterPass::class))->willReturn ($ containerProphecy ->reveal ())->shouldBeCalled ();
49
51
$ containerProphecy ->addCompilerPass (Argument::type (ElasticsearchClientPass::class))->willReturn ($ containerProphecy ->reveal ())->shouldBeCalled ();
50
52
$ containerProphecy ->addCompilerPass (Argument::type (GraphQlTypePass::class))->willReturn ($ containerProphecy ->reveal ())->shouldBeCalled ();
0 commit comments