Repository navigation
Expand file tree
/
Copy pathDefinitionSource.php
More file actions
145 lines (122 loc) · 4.12 KB
/
Copy pathDefinitionSource.php
File metadata and controls
145 lines (122 loc) · 4.12 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di\Definition;
use Hyperf\Di\ReflectionManager;
use ReflectionFunctionAbstract;
use ReflectionNamedType;
use function class_exists;
use function interface_exists;
use function is_callable;
use function is_string;
use function method_exists;
class DefinitionSource implements DefinitionSourceInterface
{
protected array $source;
public function __construct(array $source)
{
$this->source = $this->normalizeSource($source);
}
/**
* Returns the DI definition for the entry name.
*/
public function getDefinition(string $name): ?DefinitionInterface
{
return $this->source[$name] ??= $this->autowire($name);
}
/**
* @return array definitions indexed by their name
*/
public function getDefinitions(): array
{
return $this->source;
}
/**
* @param array|callable|string $definition
*/
public function addDefinition(string $name, $definition): static
{
$this->source[$name] = $this->normalizeDefinition($name, $definition);
return $this;
}
public function clearDefinitions(): void
{
$this->source = [];
}
/**
* Read the type-hinting from the parameters of the function.
*/
protected function getParametersDefinition(ReflectionFunctionAbstract $constructor): array
{
$parameters = [];
foreach ($constructor->getParameters() as $index => $parameter) {
// Skip optional parameters.
if ($parameter->isOptional()) {
continue;
}
$parameterType = $parameter->getType();
if ($parameterType instanceof ReflectionNamedType && ! $parameterType->isBuiltin()) {
$parameters[$index] = new Reference($parameterType->getName());
}
}
return $parameters;
}
/**
* Normalize the user definition source to a standard definition source.
*/
protected function normalizeSource(array $source): array
{
$definitions = [];
foreach ($source as $identifier => $definition) {
$normalizedDefinition = $this->normalizeDefinition($identifier, $definition);
if (! is_null($normalizedDefinition)) {
$definitions[$identifier] = $normalizedDefinition;
}
}
return $definitions;
}
/**
* @param array|callable|string $definition
*/
protected function normalizeDefinition(string $identifier, $definition): ?DefinitionInterface
{
if ($definition instanceof PriorityDefinition) {
$definition = $definition->getDefinition();
}
if (is_string($definition) && class_exists($definition)) {
if (method_exists($definition, '__invoke')) {
return new FactoryDefinition($identifier, $definition, []);
}
return $this->autowire($identifier, new ObjectDefinition($identifier, $definition));
}
if (is_callable($definition)) {
return new FactoryDefinition($identifier, $definition, []);
}
return null;
}
protected function autowire(string $name, ?ObjectDefinition $definition = null): ?ObjectDefinition
{
$className = $definition ? $definition->getClassName() : $name;
if (! class_exists($className) && ! interface_exists($className)) {
return $definition;
}
$definition = $definition ?: new ObjectDefinition($name);
/**
* Constructor.
*/
$class = ReflectionManager::reflectClass($className);
$constructor = $class->getConstructor();
if ($constructor && $constructor->isPublic()) {
$constructorInjection = new MethodInjection('__construct', $this->getParametersDefinition($constructor));
$definition->completeConstructorInjection($constructorInjection);
}
return $definition;
}
}