|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Di; |
|
6
|
|
|
|
|
7
|
|
|
use Ray\Aop\ReflectionClass; |
|
8
|
|
|
use ReflectionMethod; |
|
9
|
|
|
|
|
10
|
|
|
use function assert; |
|
11
|
|
|
use function class_exists; |
|
12
|
|
|
|
|
13
|
|
|
final class DependencyFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create dependency object |
|
17
|
|
|
* |
|
18
|
|
|
* @param ReflectionClass<object> $class |
|
19
|
|
|
*/ |
|
20
|
|
|
public function newAnnotatedDependency(ReflectionClass $class): Dependency |
|
21
|
|
|
{ |
|
22
|
|
|
$annotateClass = new AnnotatedClass(); |
|
23
|
|
|
$newInstance = $annotateClass->getNewInstance($class); |
|
24
|
|
|
$postConstruct = $annotateClass->getPostConstruct($class); |
|
25
|
|
|
|
|
26
|
|
|
return new Dependency($newInstance, $postConstruct); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create Provider binding |
|
31
|
|
|
* |
|
32
|
|
|
* @param ReflectionClass<object> $provider |
|
33
|
|
|
*/ |
|
34
|
|
|
public function newProvider(ReflectionClass $provider, string $context): DependencyProvider |
|
35
|
|
|
{ |
|
36
|
|
|
$dependency = $this->newAnnotatedDependency($provider); |
|
37
|
|
|
|
|
38
|
|
|
return new DependencyProvider($dependency, $context); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create ToConstructor binding |
|
43
|
|
|
* |
|
44
|
|
|
* @param ReflectionClass<object> $class |
|
45
|
|
|
*/ |
|
46
|
|
|
public function newToConstructor( |
|
47
|
|
|
ReflectionClass $class, |
|
48
|
|
|
string $name, |
|
49
|
|
|
?InjectionPoints $injectionPoints = null, |
|
50
|
|
|
?ReflectionMethod $postConstruct = null |
|
51
|
|
|
): Dependency { |
|
52
|
|
|
assert(class_exists($class->name)); |
|
53
|
|
|
$setterMethods = $injectionPoints ? $injectionPoints($class->name) : new SetterMethods([]); |
|
54
|
|
|
$newInstance = new NewInstance($class, $setterMethods, new Name($name)); |
|
55
|
|
|
|
|
56
|
|
|
return new Dependency($newInstance, $postConstruct); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|