Skip to content

Commit 35670c0

Browse files
committed
🐛 Fix support for autowiring attribute in compiler pass - it is now targeted to the method
1 parent 2420914 commit 35670c0

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/DependencyInjection/GraphQLiteCompilerPass.php

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -377,33 +377,29 @@ private function makePublicInjectedServices(ReflectionClass $refClass, Annotatio
377377
*/
378378
private function getListOfInjectedServices(ReflectionMethod $method, ContainerBuilder $container): array
379379
{
380+
/** @var array<string, string> $services */
380381
$services = [];
381382

382-
/**
383-
* @var Autowire[] $autowireAnnotations
384-
*/
385-
$autowireAnnotations = $this->getAnnotationReader()->getMethodAnnotations($method, Autowire::class);
386-
387383
$parametersByName = null;
388384

389-
foreach ($autowireAnnotations as $autowire) {
390-
$target = $autowire->getTarget();
391-
392-
if ($parametersByName === null) {
393-
$parametersByName = self::getParametersByName($method);
394-
}
385+
$annotations = $this->getAnnotationReader()->getParameterAnnotationsPerParameter($method->getParameters());
386+
foreach ($annotations as $parameterName => $parameterAnnotations) {
387+
$parameterAutowireAnnotation = $parameterAnnotations->getAnnotationsByType(Autowire::class);
388+
foreach ($parameterAutowireAnnotation as $autowire) {
389+
$id = $autowire->getIdentifier();
390+
if ($id !== null) {
391+
$services[$id] = $id;
392+
continue;
393+
}
395394

396-
if (!isset($parametersByName[$target])) {
397-
throw new GraphQLException('In method '.$method->getDeclaringClass()->getName().'::'.$method->getName().', the @Autowire annotation refers to a non existing parameter named "'.$target.'"');
398-
}
395+
$parametersByName ??= self::getParametersByName($method);
396+
if (!isset($parametersByName[$parameterName])) {
397+
throw new \LogicException('Should not happen');
398+
}
399399

400-
$id = $autowire->getIdentifier();
401-
if ($id !== null) {
402-
$services[$id] = $id;
403-
} else {
404-
$parameter = $parametersByName[$target];
400+
$parameter = $parametersByName[$parameterName];
405401
$type = $parameter->getType();
406-
if ($type !== null && $type instanceof ReflectionNamedType) {
402+
if ($type instanceof ReflectionNamedType) {
407403
$fqcn = $type->getName();
408404
if ($container->has($fqcn)) {
409405
$services[$fqcn] = $fqcn;

tests/Fixtures/Entities/Contact.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function injectService(
3939
if (!$testService instanceof TestGraphqlController || $someService === null || $someAlias === null) {
4040
return 'KO';
4141
}
42+
4243
return 'OK';
4344
}
4445

0 commit comments

Comments
 (0)