1 | <?php |
||
16 | final class TurnOnAutowireCompilerPass implements CompilerPassInterface |
||
17 | { |
||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | 3 | public function process(ContainerBuilder $containerBuilder) |
|
29 | |||
30 | 3 | private function shouldDefinitionBeAutowired(Definition $definition) : bool |
|
31 | { |
||
32 | 3 | if (!$this->isDefinitionValid($definition)) { |
|
33 | return false; |
||
34 | } |
||
35 | |||
36 | 3 | $classReflection = new ReflectionClass($definition->getClass()); |
|
37 | 3 | if (!$classReflection->hasMethod('__construct')) { |
|
38 | 2 | return false; |
|
39 | } |
||
40 | |||
41 | 3 | if (!$this->hasConstructorArguments($classReflection)) { |
|
42 | return false; |
||
43 | } |
||
44 | |||
45 | 3 | if ($this->areAllConstructorArgumentsRequired($definition, $classReflection)) { |
|
46 | return false; |
||
47 | } |
||
48 | |||
49 | 3 | $constructorReflection = $classReflection->getConstructor(); |
|
50 | 3 | if (!$this->haveMissingArgumentsTypehints($definition, $constructorReflection)) { |
|
51 | return false; |
||
52 | } |
||
53 | |||
54 | 3 | return true; |
|
55 | } |
||
56 | |||
57 | 3 | private function isDefinitionValid(Definition $definition) : bool |
|
58 | { |
||
59 | 3 | if (null === $definition->getClass()) { |
|
60 | return false; |
||
61 | } |
||
62 | |||
63 | 3 | if (!$definition->isPublic()) { |
|
64 | return false; |
||
65 | } |
||
66 | |||
67 | 3 | if ($definition->isAbstract()) { |
|
68 | return false; |
||
69 | } |
||
70 | |||
71 | 3 | if (!class_exists($definition->getClass())) { |
|
72 | return false; |
||
73 | } |
||
74 | |||
75 | 3 | return true; |
|
76 | } |
||
77 | |||
78 | 3 | private function areAllConstructorArgumentsRequired(Definition $definition, ReflectionClass $classReflection) : bool |
|
79 | { |
||
80 | 3 | $constructorMethodReflection = $classReflection->getConstructor(); |
|
81 | |||
82 | 3 | $constructorArgumentsCount = count($definition->getArguments()); |
|
83 | 3 | $constructorRequiredArgumentsCount = $constructorMethodReflection->getNumberOfRequiredParameters(); |
|
84 | |||
85 | 3 | if ($constructorArgumentsCount === $constructorRequiredArgumentsCount) { |
|
86 | return true; |
||
87 | } |
||
88 | |||
89 | 3 | return false; |
|
90 | } |
||
91 | |||
92 | 3 | private function hasConstructorArguments(ReflectionClass $classReflection) : bool |
|
101 | |||
102 | 3 | private function haveMissingArgumentsTypehints( |
|
103 | Definition $definition, |
||
104 | ReflectionMethod $constructorReflection |
||
121 | } |
||
122 |