Skip to content

Commit b8a1896

Browse files
authored
Merge pull request #22 from moufmouf/support_autowiring_annotation
Changing code to adopt new @autowire syntax.
2 parents e9ccb23 + 7a9918a commit b8a1896

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -248,37 +248,33 @@ private function getListOfInjectedServices(ReflectionMethod $method, ContainerBu
248248
$services = [];
249249

250250
/**
251-
* @var Parameter[] $parameterAnnotations
251+
* @var Autowire[] $autowireAnnotations
252252
*/
253-
$parameterAnnotations = $this->getAnnotationReader()->getMethodAnnotations($method, Parameter::class);
253+
$autowireAnnotations = $this->getAnnotationReader()->getMethodAnnotations($method, Autowire::class);
254254

255255
$parametersByName = null;
256256

257-
foreach ($parameterAnnotations as $parameterAnnotation) {
258-
/** @var Autowire|null $autowire */
259-
$autowire = $parameterAnnotation->getAnnotationByType(Autowire::class);
260-
if ($autowire) {
261-
$target = $parameterAnnotation->getFor();
257+
foreach ($autowireAnnotations as $autowire) {
258+
$target = $autowire->getTarget();
262259

263-
if ($parametersByName === null) {
264-
$parametersByName = self::getParametersByName($method);
265-
}
260+
if ($parametersByName === null) {
261+
$parametersByName = self::getParametersByName($method);
262+
}
266263

267-
if (!isset($parametersByName[$target])) {
268-
throw new GraphQLException('In method '.$method->getDeclaringClass()->getName().'::'.$method->getName().', the @Autowire annotation refers to a non existing parameter named "'.$target.'"');
269-
}
264+
if (!isset($parametersByName[$target])) {
265+
throw new GraphQLException('In method '.$method->getDeclaringClass()->getName().'::'.$method->getName().', the @Autowire annotation refers to a non existing parameter named "'.$target.'"');
266+
}
270267

271-
$id = $autowire->getIdentifier();
272-
if ($id !== null) {
273-
$services[$id] = $id;
274-
} else {
275-
$parameter = $parametersByName[$target];
276-
$type = $parameter->getType();
277-
if ($type !== null) {
278-
$fqcn = $type->getName();
279-
if ($container->has($fqcn)) {
280-
$services[$fqcn] = $fqcn;
281-
}
268+
$id = $autowire->getIdentifier();
269+
if ($id !== null) {
270+
$services[$id] = $id;
271+
} else {
272+
$parameter = $parametersByName[$target];
273+
$type = $parameter->getType();
274+
if ($type !== null) {
275+
$fqcn = $type->getName();
276+
if ($container->has($fqcn)) {
277+
$services[$fqcn] = $fqcn;
282278
}
283279
}
284280
}

Tests/Fixtures/Entities/Contact.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
namespace TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Entities;
55

66

7-
use DateTimeInterface;
8-
use Psr\Http\Message\UploadedFileInterface;
97
use stdClass;
108
use TheCodingMachine\GraphQLite\Annotations\Field;
11-
use TheCodingMachine\GraphQLite\Annotations\Parameter;
129
use TheCodingMachine\GraphQLite\Annotations\Type;
1310
use TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Controller\TestGraphqlController;
1411
use TheCodingMachine\GraphQLite\Annotations\Autowire;
@@ -38,8 +35,8 @@ public function getName(): string
3835

3936
/**
4037
* @Field()
41-
* @Parameter(for="$testService", annotations={@Autowire})
42-
* @Parameter(for="$someService", annotations={@Autowire(identifier="someService")})
38+
* @Autowire(for="$testService")
39+
* @Autowire(for="$someService", identifier="someService")
4340
* @return string
4441
*/
4542
public function injectService(TestGraphqlController $testService = null, stdClass $someService = null): string

0 commit comments

Comments
 (0)