13
13
14
14
namespace ApiPlatform \Core \Bridge \Symfony \Bundle \DependencyInjection \Compiler ;
15
15
16
+ use ApiPlatform \Core \Annotation \ApiFilter ;
16
17
use ApiPlatform \Core \Util \AnnotationFilterExtractorTrait ;
17
18
use ApiPlatform \Core \Util \ReflectionClassRecursiveIterator ;
18
19
use Doctrine \Common \Annotations \Reader ;
23
24
use Symfony \Component \DependencyInjection \Exception \InvalidArgumentException ;
24
25
25
26
/**
26
- * Injects filters .
27
+ * Registers filter services from {@see ApiFilter} annotations .
27
28
*
28
29
* @internal
29
30
*
@@ -43,7 +44,7 @@ final class AnnotationFilterPass implements CompilerPassInterface
43
44
/**
44
45
* {@inheritdoc}
45
46
*/
46
- public function process (ContainerBuilder $ container )
47
+ public function process (ContainerBuilder $ container ): void
47
48
{
48
49
$ resourceClassDirectories = $ container ->getParameter ('api_platform.resource_class_directories ' );
49
50
@@ -55,28 +56,41 @@ public function process(ContainerBuilder $container)
55
56
/**
56
57
* @throws InvalidArgumentException
57
58
*/
58
- private function createFilterDefinitions (\ReflectionClass $ reflectionClass , ContainerBuilder $ container ): void
59
+ private function createFilterDefinitions (\ReflectionClass $ resourceReflectionClass , ContainerBuilder $ container ): void
59
60
{
60
- $ reader = $ this ->reader ?? $ this ->reader = $ container ->get ('annotation_reader ' );
61
+ $ this ->reader ?? $ this ->reader = $ container ->get ('annotation_reader ' );
61
62
62
- foreach ($ this ->readFilterAnnotations ($ reflectionClass , $ reader ) as $ id => [$ arguments , $ filterClass ]) {
63
+ foreach ($ this ->readFilterAnnotations ($ resourceReflectionClass , $ this -> reader ) as $ id => [$ arguments , $ filterClass ]) {
63
64
if ($ container ->has ($ id )) {
64
65
continue ;
65
66
}
66
67
67
- if ($ container ->has ($ filterClass ) && ($ definition = $ container ->findDefinition ($ filterClass ))->isAbstract ()) {
68
- $ definition = new ChildDefinition ($ definition ->getClass ());
69
- } elseif ($ reflectionClass = $ container ->getReflectionClass ($ filterClass , false )) {
70
- $ definition = new Definition ($ reflectionClass ->getName ());
71
- $ definition ->setAutoconfigured (true );
72
- } else {
68
+ if (null === $ filterReflectionClass = $ container ->getReflectionClass ($ filterClass , false )) {
73
69
throw new InvalidArgumentException (sprintf ('Class "%s" used for service "%s" cannot be found. ' , $ filterClass , $ id ));
74
70
}
75
71
72
+ if ($ container ->has ($ filterClass ) && ($ parentDefinition = $ container ->findDefinition ($ filterClass ))->isAbstract ()) {
73
+ $ definition = new ChildDefinition ($ parentDefinition ->getClass ());
74
+ } else {
75
+ $ definition = new Definition ($ filterReflectionClass ->getName ());
76
+ $ definition ->setAutoconfigured (true );
77
+ }
78
+
76
79
$ definition ->addTag (self ::TAG_FILTER_NAME );
77
80
$ definition ->setAutowired (true );
78
81
82
+ $ parameterNames = [];
83
+ if (null !== $ constructorReflectionMethod = $ filterReflectionClass ->getConstructor ()) {
84
+ foreach ($ constructorReflectionMethod ->getParameters () as $ reflectionParameter ) {
85
+ $ parameterNames [$ reflectionParameter ->name ] = true ;
86
+ }
87
+ }
88
+
79
89
foreach ($ arguments as $ key => $ value ) {
90
+ if (!isset ($ parameterNames [$ key ])) {
91
+ throw new InvalidArgumentException (sprintf ('Class "%s" does not have argument "$%s". ' , $ filterClass , $ key ));
92
+ }
93
+
80
94
$ definition ->setArgument ("$ $ key " , $ value );
81
95
}
82
96
0 commit comments