Skip to content

Commit 69df91f

Browse files
committed
Fixing autowiring by parameter name
1 parent 9d35d06 commit 69df91f

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public function process(ContainerBuilder $container)
7575
$controllersNamespaces = $container->getParameter('graphqlite.namespace.controllers');
7676
$typesNamespaces = $container->getParameter('graphqlite.namespace.types');
7777

78+
$autowireByParameterName = $container->getParameter('graphqlite.autowire.by_parameter_name');
79+
$autowireByClassName = $container->getParameter('graphqlite.autowire.by_class_name');
80+
7881
// 2 seconds of TTL in environment mode. Otherwise, let's cache forever!
7982
$env = $container->getParameter('kernel.environment');
8083
$globTtl = null;
@@ -120,15 +123,17 @@ public function process(ContainerBuilder $container)
120123
}
121124
}
122125

123-
foreach ($controllersNamespaces as $controllersNamespace) {
124-
foreach ($this->getClassList($controllersNamespace) as $className => $refClass) {
125-
$this->makePublicInjectedServices($refClass, $reader, $container);
126+
if ($autowireByParameterName || $autowireByClassName) {
127+
foreach ($controllersNamespaces as $controllersNamespace) {
128+
foreach ($this->getClassList($controllersNamespace) as $className => $refClass) {
129+
$this->makePublicInjectedServices($refClass, $reader, $container, $autowireByClassName, $autowireByParameterName);
130+
}
126131
}
127-
}
128132

129-
foreach ($typesNamespaces as $typeNamespace) {
130-
foreach ($this->getClassList($typeNamespace) as $className => $refClass) {
131-
$this->makePublicInjectedServices($refClass, $reader, $container);
133+
foreach ($typesNamespaces as $typeNamespace) {
134+
foreach ($this->getClassList($typeNamespace) as $className => $refClass) {
135+
$this->makePublicInjectedServices($refClass, $reader, $container, $autowireByClassName, $autowireByParameterName);
136+
}
132137
}
133138
}
134139

@@ -215,14 +220,14 @@ public function process(ContainerBuilder $container)
215220
}
216221
}
217222

218-
private function makePublicInjectedServices(ReflectionClass $refClass, AnnotationReader $reader, ContainerBuilder $container): void
223+
private function makePublicInjectedServices(ReflectionClass $refClass, AnnotationReader $reader, ContainerBuilder $container, bool $autowireByClassName, bool $autowireByParameterName): void
219224
{
220-
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container) {
225+
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container, $autowireByClassName, $autowireByParameterName) {
221226
$services = [];
222227
foreach ($refClass->getMethods() as $method) {
223228
$field = $reader->getRequestAnnotation($method, AbstractRequest::class);
224229
if ($field !== null) {
225-
$services += $this->getListOfInjectedServices($method, $container);
230+
$services += $this->getListOfInjectedServices($method, $container, $autowireByClassName, $autowireByParameterName);
226231
}
227232
}
228233
return $services;
@@ -239,15 +244,23 @@ private function makePublicInjectedServices(ReflectionClass $refClass, Annotatio
239244
* @param ContainerBuilder $container
240245
* @return array<string, string> key = value = service name
241246
*/
242-
private function getListOfInjectedServices(ReflectionMethod $method, ContainerBuilder $container): array
247+
private function getListOfInjectedServices(ReflectionMethod $method, ContainerBuilder $container, bool $autowireByClassName, bool $autowireByParameterName): array
243248
{
244249
$services = [];
245250
foreach ($method->getParameters() as $parameter) {
246-
$type = $parameter->getType();
247-
if ($type !== null) {
248-
$fqcn = $type->getName();
249-
if ($container->has($fqcn)) {
250-
$services[$fqcn] = $fqcn;
251+
if ($autowireByParameterName) {
252+
$parameterName = $parameter->getName();
253+
if ($container->has($parameterName)) {
254+
$services[$parameterName] = $parameterName;
255+
}
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;
263+
}
251264
}
252265
}
253266
}

Tests/Fixtures/Entities/Contact.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public function getName(): string
3838
* @Field()
3939
* @return string
4040
*/
41-
public function injectService(TestGraphqlController $testService = null): string
41+
public function injectService(TestGraphqlController $testService = null, stdClass $someService = null): string
4242
{
43-
if (!$testService instanceof TestGraphqlController) {
43+
if (!$testService instanceof TestGraphqlController || $someService === null) {
4444
return 'KO';
4545
}
4646
return 'OK';

Tests/Fixtures/config/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ services:
1717
resource: '../*'
1818
exclude: '../{Entities}'
1919

20+
someService:
21+
class: stdClass
2022
# controllers are imported separately to make sure services can be injected
2123
# as action arguments even if you don't extend any base controller class
2224
#App\Controller\:

composer.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require" : {
1919
"php" : ">=7.2",
20-
"thecodingmachine/graphqlite" : "dev-autowiring as 4.0.0",
20+
"thecodingmachine/graphqlite" : "^4",
2121
"symfony/framework-bundle": "^4.1.9",
2222
"doctrine/annotations": "^1.6",
2323
"doctrine/cache": "^1.8",
@@ -34,12 +34,6 @@
3434
"phpstan/phpstan": "^0.10.6",
3535
"beberlei/porpaginas": "^1.2"
3636
},
37-
"repositories": [
38-
{
39-
"type": "vcs",
40-
"url": "https://github.com/moufmouf/graphqlite"
41-
}
42-
],
4337
"scripts": {
4438
"phpstan": "phpstan analyse GraphqliteBundle.php DependencyInjection/ Controller/ Resources/ Security/ -c phpstan.neon --level=7 --no-progress"
4539
},

0 commit comments

Comments
 (0)