-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathAutowireLocatorServiceMapFactory.php
110 lines (86 loc) · 2.87 KB
/
AutowireLocatorServiceMapFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php declare(strict_types = 1);
namespace PHPStan\Symfony;
use InvalidArgumentException;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
use Symfony\Component\DependencyInjection\TypedReference;
use function class_exists;
use function count;
use function sprintf;
final class AutowireLocatorServiceMapFactory implements ServiceMapFactory
{
/** @var Node */
private $node;
/** @var Scope */
private $scope;
public function __construct(
Node $node,
Scope $scope
)
{
$this->node = $node;
$this->scope = $scope;
}
public function create(): ServiceMap
{
if (!class_exists('Symfony\\Component\\DependencyInjection\\Attribute\\AutowireLocator')) {
return new FakeServiceMap();
}
if (!$this->node instanceof MethodCall) {
return new FakeServiceMap();
}
$nodeParentProperty = $this->node->var;
if (!$nodeParentProperty instanceof Node\Expr\PropertyFetch) {
return new FakeServiceMap();
}
$nodeParentPropertyName = $nodeParentProperty->name;
if (!$nodeParentPropertyName instanceof Node\Identifier) {
return new FakeServiceMap();
}
$containerInterfacePropertyName = $nodeParentPropertyName->name;
$scopeClassReflection = $this->scope->getClassReflection();
if (!$scopeClassReflection instanceof ClassReflection) {
return new FakeServiceMap();
}
$containerInterfacePropertyReflection = $scopeClassReflection
->getNativeProperty($containerInterfacePropertyName);
$classPropertyReflection = $containerInterfacePropertyReflection->getNativeReflection();
$autowireLocatorAttributesReflection = $classPropertyReflection->getAttributes(AutowireLocator::class);
if (count($autowireLocatorAttributesReflection) === 0) {
return new FakeServiceMap();
}
if (count($autowireLocatorAttributesReflection) > 1) {
throw new InvalidArgumentException(sprintf(
'Only one AutowireLocator attribute is allowed on "%s::%s".',
$scopeClassReflection->getName(),
$containerInterfacePropertyName
));
}
$autowireLocatorAttributeReflection = $autowireLocatorAttributesReflection[0];
/** @var AutowireLocator $autowireLocator */
$autowireLocator = $autowireLocatorAttributeReflection->newInstance();
$serviceLocatorArgument = $autowireLocator->value;
if (!$serviceLocatorArgument instanceof ServiceLocatorArgument) {
return new FakeServiceMap();
}
/** @var Service[] $services */
$services = [];
/** @var TypedReference $service */
foreach ($serviceLocatorArgument->getValues() as $id => $service) {
$class = $service->getType();
$alias = $service->getName();
$services[$id] = new Service(
$id,
$class,
true,
false,
$alias
);
}
return new DefaultServiceMap($services);
}
}