|
7 | 7 | use ReflectionParameter;
|
8 | 8 | use Symfony\Component\Config\Resource\DirectoryResource;
|
9 | 9 | use Symfony\Component\DependencyInjection\Annotation\Inject\InjectableInterface;
|
10 |
| -use Symfony\Component\DependencyInjection\Annotation\Inject\MethodAnnotationInterface; |
| 10 | +use Symfony\Component\DependencyInjection\Annotation\Modifier\ModifyContainerInterface; |
| 11 | +use Symfony\Component\DependencyInjection\Annotation\Modifier\ModifyServiceAnnotationInterface; |
11 | 12 | use Symfony\Component\DependencyInjection\Annotation\Service;
|
12 | 13 | use Symfony\Component\DependencyInjection\Annotation\Tag\TagInterface;
|
13 | 14 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
@@ -67,23 +68,26 @@ public function process(ContainerBuilder $container)
|
67 | 68 |
|
68 | 69 | $services = $this->serviceFinder->findServiceAnnotations($this->srcDirs);
|
69 | 70 |
|
| 71 | + // register all services first so they are known on the further steps |
70 | 72 | foreach ($services as $id => $service) {
|
71 |
| - $service = $this->modifyServiceByMethodAnnotations($service, $container); |
| 73 | + $service = $this->modifyServiceByMethodAnnotations($id, $service, $container); |
72 | 74 |
|
73 | 75 | $container->setDefinition($id, $this->createServiceDefinition($service));
|
74 | 76 | }
|
75 | 77 |
|
76 |
| - // resolve arguments after all services are added, so we know all available services here |
77 | 78 | foreach ($services as $id => $service) {
|
78 | 79 | $definition = $container->getDefinition($id);
|
79 | 80 |
|
| 81 | + // resolve arguments and method calls |
80 | 82 | if (!$service->factoryClass) {
|
81 | 83 | $definition->setArguments($this->getConstructorArguments($service, $container));
|
82 | 84 |
|
83 | 85 | foreach ($service->methodCalls as $methodCall) {
|
84 | 86 | $this->addMethodCall($definition, $methodCall, $service, $container);
|
85 | 87 | }
|
86 | 88 | }
|
| 89 | + |
| 90 | + $this->modifyContainerByMethodAnnotations($id, $service, $definition, $container); |
87 | 91 | }
|
88 | 92 | }
|
89 | 93 |
|
@@ -120,23 +124,46 @@ protected function createServiceDefinition(Service $service)
|
120 | 124 | }
|
121 | 125 |
|
122 | 126 | /**
|
| 127 | + * @param string $serviceId |
123 | 128 | * @param Service $service
|
124 | 129 | * @param ContainerBuilder $container
|
125 | 130 | *
|
126 | 131 | * @return Service
|
127 | 132 | */
|
128 |
| - private function modifyServiceByMethodAnnotations(Service $service, ContainerBuilder $container) |
| 133 | + private function modifyServiceByMethodAnnotations($serviceId, Service $service, ContainerBuilder $container) |
129 | 134 | {
|
130 | 135 | foreach ($service->getAllMethodAnnotations() as $method => $methodAnnotations) {
|
131 |
| - /** @var MethodAnnotationInterface[] $methodAnnotations */ |
132 | 136 | foreach ($methodAnnotations as $methodAnnotation) {
|
133 |
| - $service = $methodAnnotation->modifyService($service, $method, $container); |
| 137 | + if ($methodAnnotation instanceof ModifyServiceAnnotationInterface) { |
| 138 | + $service = $methodAnnotation->modifyService($serviceId, $service, $method, $container); |
| 139 | + } |
134 | 140 | }
|
135 | 141 | }
|
136 | 142 |
|
137 | 143 | return $service;
|
138 | 144 | }
|
139 | 145 |
|
| 146 | + /** |
| 147 | + * @param string $serviceId |
| 148 | + * @param Service $service |
| 149 | + * @param Definition $definition |
| 150 | + * @param ContainerBuilder $container |
| 151 | + */ |
| 152 | + private function modifyContainerByMethodAnnotations( |
| 153 | + $serviceId, |
| 154 | + Service $service, |
| 155 | + Definition $definition, |
| 156 | + ContainerBuilder $container |
| 157 | + ) { |
| 158 | + foreach ($service->getAllMethodAnnotations() as $method => $methodAnnotations) { |
| 159 | + foreach ($methodAnnotations as $methodAnnotation) { |
| 160 | + if ($methodAnnotation instanceof ModifyContainerInterface) { |
| 161 | + $methodAnnotation->modifyContainer($serviceId, $service, $definition, $method, $container); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
140 | 167 | /**
|
141 | 168 | * @param Definition $definition
|
142 | 169 | * @param Service $service
|
|
0 commit comments