1 | <?php |
||
18 | class Autowiring extends AbstractDefinitionSource |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | public function get($name) |
||
24 | { |
||
25 | if ($this->has($name)) { |
||
26 | return $this->definitions[$name]; |
||
27 | } |
||
28 | |||
29 | if (!class_exists($name) && !interface_exists($name)) { |
||
30 | return; |
||
31 | } |
||
32 | |||
33 | $autowiring = function ($name) { |
||
34 | $class = new \ReflectionClass($name); |
||
35 | |||
36 | $constructor = $class->getConstructor(); |
||
37 | |||
38 | if ($constructor && $constructor->isPublic()) { |
||
39 | $parameters = $this->getParametersDefinition($constructor); |
||
40 | if ($constructor->getNumberOfRequiredParameters() !== count($parameters)) { |
||
41 | return; |
||
42 | } |
||
43 | |||
44 | $object = $class->newInstanceArgs($parameters); |
||
45 | $this->set($name, $object); |
||
46 | |||
47 | return $object; |
||
48 | } |
||
49 | |||
50 | $object = $class->newInstanceWithoutConstructor(); |
||
51 | $this->set($name, $object); |
||
52 | |||
53 | return $object; |
||
54 | }; |
||
55 | |||
56 | return $autowiring($name); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Get constructor parameters definitions. |
||
61 | * |
||
62 | * @param \ReflectionFunctionAbstract $constructor |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | public function getParametersDefinition(\ReflectionFunctionAbstract $constructor) |
||
83 | |||
84 | /** |
||
85 | * Get Class definition for constructor parameter. |
||
86 | * |
||
87 | * @param \ReflectionClass $parameterClass |
||
88 | * |
||
89 | * @return mixed|null|object |
||
90 | */ |
||
91 | private function getClassDefinition(\ReflectionClass $parameterClass) |
||
103 | } |
||
104 |