-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConstructorResolver.php
151 lines (115 loc) · 5.12 KB
/
ConstructorResolver.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace Matthias\SymfonyServiceDefinitionValidator;
use Matthias\SymfonyServiceDefinitionValidator\Exception\ClassNotFoundException;
use Matthias\SymfonyServiceDefinitionValidator\Exception\FunctionNotFoundException;
use Matthias\SymfonyServiceDefinitionValidator\Exception\MethodNotFoundException;
use Matthias\SymfonyServiceDefinitionValidator\Exception\NonPublicConstructorException;
use Matthias\SymfonyServiceDefinitionValidator\Exception\NonStaticFactoryMethodException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
class ConstructorResolver implements ConstructorResolverInterface
{
private $containerBuilder;
private $resultingClassResolver;
public function __construct(
ContainerBuilder $containerBuilder,
ResultingClassResolverInterface $resultingClassResolver
) {
$this->containerBuilder = $containerBuilder;
$this->resultingClassResolver = $resultingClassResolver;
}
public function resolve(Definition $definition)
{
$factory = $this->resolveFactory($definition);
if (is_string($factory)) {
return $this->resolveFactoryFunction($factory);
}
if (is_array($factory) && $factory[0] instanceof Reference) {
return $this->resolveFactoryServiceWithMethod((string) $factory[0], $factory[1]);
}
if (is_array($factory) && $factory[0] instanceof Definition) {
return $this->resolveFactoryServiceDefinitionWithMethod($factory[0], $factory[1]);
}
if (is_array($factory)) {
return $this->resolveFactoryClassWithMethod($factory[0], $factory[1]);
}
if ($definition->getClass()) {
return $this->resolveClassWithConstructor($definition->getClass());
}
return null;
}
private function resolveFactoryClassWithMethod($factoryClass, $factoryMethod)
{
$factoryClass = $this->resolvePlaceholders($factoryClass);
if (!class_exists($factoryClass)) {
throw new ClassNotFoundException($factoryClass);
}
if (!method_exists($factoryClass, $factoryMethod)) {
throw new MethodNotFoundException($factoryClass, $factoryMethod);
}
$reflectionMethod = new \ReflectionMethod($factoryClass, $factoryMethod);
if (!$reflectionMethod->isStatic()) {
throw new NonStaticFactoryMethodException($factoryClass, $factoryMethod);
}
return $reflectionMethod;
}
private function resolveFactoryServiceWithMethod($factoryServiceId, $factoryMethod)
{
$factoryDefinition = $this->containerBuilder->findDefinition($factoryServiceId);
$factoryClass = $this->resultingClassResolver->resolve($factoryDefinition);
if (!method_exists($factoryClass, $factoryMethod)) {
throw new MethodNotFoundException($factoryClass, $factoryMethod);
}
return new \ReflectionMethod($factoryClass, $factoryMethod);
}
private function resolveClassWithConstructor($class)
{
$class = $this->resolvePlaceholders($class);
$reflectionClass = new \ReflectionClass($class);
if ($reflectionClass->hasMethod('__construct')) {
$constructMethod = $reflectionClass->getMethod('__construct');
if (!$constructMethod->isPublic()) {
throw new NonPublicConstructorException($class);
}
return $constructMethod;
}
return null;
}
private function resolvePlaceholders($value)
{
return $this->containerBuilder->getParameterBag()->resolveValue($value);
}
private function resolveFactory(Definition $definition)
{
if (method_exists($definition, 'getFactory') && $definition->getFactory() !== null) {
return $definition->getFactory();
}
if (method_exists($definition, 'getFactoryClass')
&& $definition->getFactoryClass(false)
&& $definition->getFactoryMethod(false)) {
return array($definition->getFactoryClass(false), $definition->getFactoryMethod(false));
}
if (method_exists($definition, 'getFactoryService')
&& $definition->getFactoryService(false)
&& $definition->getFactoryMethod(false)) {
return array(new Reference($definition->getFactoryService(false)), $definition->getFactoryMethod(false));
}
return null;
}
private function resolveFactoryFunction($factory)
{
if (!function_exists($factory)) {
throw new FunctionNotFoundException($factory);
}
return new \ReflectionFunction($factory);
}
private function resolveFactoryServiceDefinitionWithMethod(Definition $factoryDefinition, $factoryMethod)
{
$factoryClass = $this->resultingClassResolver->resolve($factoryDefinition);
if (!method_exists($factoryClass, $factoryMethod)) {
throw new MethodNotFoundException($factoryClass, $factoryMethod);
}
return new \ReflectionMethod($factoryClass, $factoryMethod);
}
}