|
12 | 12 | use function error_log; |
13 | 13 | use Mouf\Composer\ClassNameMapper; |
14 | 14 | use Psr\SimpleCache\CacheInterface; |
| 15 | +use ReflectionParameter; |
15 | 16 | use Symfony\Component\Cache\Simple\ApcuCache as SymfonyApcuCache; |
16 | 17 | use Symfony\Component\Cache\Simple\PhpFilesCache as SymfonyPhpFilesCache; |
17 | 18 | use function function_exists; |
|
34 | 35 | use TheCodingMachine\ClassExplorer\Glob\GlobClassExplorer; |
35 | 36 | use TheCodingMachine\GraphQLite\AnnotationReader; |
36 | 37 | use TheCodingMachine\GraphQLite\Annotations\AbstractRequest; |
| 38 | +use TheCodingMachine\GraphQLite\Annotations\Autowire; |
37 | 39 | use TheCodingMachine\GraphQLite\Annotations\Field; |
38 | 40 | use TheCodingMachine\GraphQLite\Annotations\Mutation; |
| 41 | +use TheCodingMachine\GraphQLite\Annotations\Parameter; |
39 | 42 | use TheCodingMachine\GraphQLite\Annotations\Query; |
40 | 43 | use TheCodingMachine\Graphqlite\Bundle\QueryProviders\ControllerQueryProvider; |
41 | 44 | use TheCodingMachine\GraphQLite\FieldsBuilder; |
42 | 45 | use TheCodingMachine\GraphQLite\FieldsBuilderFactory; |
| 46 | +use TheCodingMachine\GraphQLite\GraphQLException; |
43 | 47 | use TheCodingMachine\GraphQLite\InputTypeGenerator; |
44 | 48 | use TheCodingMachine\GraphQLite\InputTypeUtils; |
45 | 49 | use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper; |
@@ -247,26 +251,60 @@ private function makePublicInjectedServices(ReflectionClass $refClass, Annotatio |
247 | 251 | private function getListOfInjectedServices(ReflectionMethod $method, ContainerBuilder $container, bool $autowireByClassName, bool $autowireByParameterName): array |
248 | 252 | { |
249 | 253 | $services = []; |
250 | | - foreach ($method->getParameters() as $parameter) { |
251 | | - if ($autowireByParameterName) { |
252 | | - $parameterName = $parameter->getName(); |
253 | | - if ($container->has($parameterName)) { |
254 | | - $services[$parameterName] = $parameterName; |
| 254 | + |
| 255 | + /** |
| 256 | + * @var Parameter[] $parameterAnnotations |
| 257 | + */ |
| 258 | + $parameterAnnotations = $this->getAnnotationReader()->getMethodAnnotations($method, Parameter::class); |
| 259 | + |
| 260 | + $parametersByName = null; |
| 261 | + |
| 262 | + foreach ($parameterAnnotations as $parameterAnnotation) { |
| 263 | + /** @var Autowire|null $autowire */ |
| 264 | + $autowire = $parameterAnnotation->getAnnotationByType(Autowire::class); |
| 265 | + if ($autowire) { |
| 266 | + $target = $parameterAnnotation->getFor(); |
| 267 | + |
| 268 | + if ($parametersByName === null) { |
| 269 | + $parametersByName = self::getParametersByName($method); |
255 | 270 | } |
256 | | - } |
257 | | - if ($autowireByClassName) { |
258 | | - $type = $parameter->getType(); |
259 | | - if ($type !== null) { |
260 | | - $fqcn = $type->getName(); |
261 | | - if ($container->has($fqcn)) { |
262 | | - $services[$fqcn] = $fqcn; |
| 271 | + |
| 272 | + if (!isset($parametersByName[$target])) { |
| 273 | + throw new GraphQLException('In method '.$method->getDeclaringClass()->getName().'::'.$method->getName().', the @Autowire annotation refers to a non existing parameter named "'.$target.'"'); |
| 274 | + } |
| 275 | + |
| 276 | + $id = $autowire->getIdentifier(); |
| 277 | + if ($id !== null) { |
| 278 | + $services[$id] = $id; |
| 279 | + } else { |
| 280 | + $parameter = $parametersByName[$target]; |
| 281 | + $type = $parameter->getType(); |
| 282 | + if ($type !== null) { |
| 283 | + $fqcn = $type->getName(); |
| 284 | + if ($container->has($fqcn)) { |
| 285 | + $services[$fqcn] = $fqcn; |
| 286 | + } |
263 | 287 | } |
264 | 288 | } |
265 | 289 | } |
266 | 290 | } |
| 291 | + |
267 | 292 | return $services; |
268 | 293 | } |
269 | 294 |
|
| 295 | + /** |
| 296 | + * @param ReflectionMethod $method |
| 297 | + * @return array<string, ReflectionParameter> |
| 298 | + */ |
| 299 | + private static function getParametersByName(ReflectionMethod $method): array |
| 300 | + { |
| 301 | + $parameters = []; |
| 302 | + foreach ($method->getParameters() as $parameter) { |
| 303 | + $parameters[$parameter->getName()] = $parameter; |
| 304 | + } |
| 305 | + return $parameters; |
| 306 | + } |
| 307 | + |
270 | 308 | /** |
271 | 309 | * @param object $controller |
272 | 310 | */ |
|
0 commit comments