Skip to content

Commit 8081390

Browse files
committed
Adding support for new @autowire annotation
This PR adds support for the brand new @autowire annotation added in thecodingmachine/graphqlite#86
1 parent 7c73f6a commit 8081390

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use function error_log;
1313
use Mouf\Composer\ClassNameMapper;
1414
use Psr\SimpleCache\CacheInterface;
15+
use ReflectionParameter;
1516
use Symfony\Component\Cache\Simple\ApcuCache as SymfonyApcuCache;
1617
use Symfony\Component\Cache\Simple\PhpFilesCache as SymfonyPhpFilesCache;
1718
use function function_exists;
@@ -34,12 +35,15 @@
3435
use TheCodingMachine\ClassExplorer\Glob\GlobClassExplorer;
3536
use TheCodingMachine\GraphQLite\AnnotationReader;
3637
use TheCodingMachine\GraphQLite\Annotations\AbstractRequest;
38+
use TheCodingMachine\GraphQLite\Annotations\Autowire;
3739
use TheCodingMachine\GraphQLite\Annotations\Field;
3840
use TheCodingMachine\GraphQLite\Annotations\Mutation;
41+
use TheCodingMachine\GraphQLite\Annotations\Parameter;
3942
use TheCodingMachine\GraphQLite\Annotations\Query;
4043
use TheCodingMachine\Graphqlite\Bundle\QueryProviders\ControllerQueryProvider;
4144
use TheCodingMachine\GraphQLite\FieldsBuilder;
4245
use TheCodingMachine\GraphQLite\FieldsBuilderFactory;
46+
use TheCodingMachine\GraphQLite\GraphQLException;
4347
use TheCodingMachine\GraphQLite\InputTypeGenerator;
4448
use TheCodingMachine\GraphQLite\InputTypeUtils;
4549
use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper;
@@ -247,26 +251,60 @@ private function makePublicInjectedServices(ReflectionClass $refClass, Annotatio
247251
private function getListOfInjectedServices(ReflectionMethod $method, ContainerBuilder $container, bool $autowireByClassName, bool $autowireByParameterName): array
248252
{
249253
$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);
255270
}
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+
}
263287
}
264288
}
265289
}
266290
}
291+
267292
return $services;
268293
}
269294

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+
270308
/**
271309
* @param object $controller
272310
*/

Tests/Fixtures/Entities/Contact.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use Psr\Http\Message\UploadedFileInterface;
99
use stdClass;
1010
use TheCodingMachine\GraphQLite\Annotations\Field;
11+
use TheCodingMachine\GraphQLite\Annotations\Parameter;
1112
use TheCodingMachine\GraphQLite\Annotations\Type;
1213
use TheCodingMachine\Graphqlite\Bundle\Tests\Fixtures\Controller\TestGraphqlController;
14+
use TheCodingMachine\GraphQLite\Annotations\Autowire;
1315

1416
/**
1517
* @Type()
@@ -36,6 +38,8 @@ public function getName(): string
3638

3739
/**
3840
* @Field()
41+
* @Parameter(for="$testService", annotations={@Autowire})
42+
* @Parameter(for="$someService", annotations={@Autowire(identifier="someService")})
3943
* @return string
4044
*/
4145
public function injectService(TestGraphqlController $testService = null, stdClass $someService = null): string

0 commit comments

Comments
 (0)