1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\Di; |
6
|
|
|
|
7
|
|
|
use Ray\Aop\Bind as AopBind; |
8
|
|
|
|
9
|
|
|
final class NewInstance |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $class; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var SetterMethods |
18
|
|
|
*/ |
19
|
|
|
private $setterMethods; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var null|Arguments |
23
|
|
|
*/ |
24
|
|
|
private $arguments; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var AspectBind |
28
|
|
|
*/ |
29
|
|
|
private $bind; |
30
|
|
|
|
31
|
|
|
public function __construct( |
32
|
|
|
\ReflectionClass $class, |
33
|
|
|
SetterMethods $setterMethods, |
34
|
|
|
Name $constructorName = null |
35
|
|
|
) { |
36
|
|
|
$constructorName = $constructorName ?: new Name(Name::ANY); |
37
|
|
|
$this->class = $class->name; |
38
|
|
|
$constructor = $class->getConstructor(); |
39
|
|
|
if ($constructor) { |
40
|
|
|
$this->arguments = new Arguments($constructor, $constructorName); |
41
|
|
|
} |
42
|
|
|
$this->setterMethods = $setterMethods; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws \ReflectionException |
47
|
|
|
* |
48
|
|
|
* @return object |
49
|
|
|
*/ |
50
|
|
|
public function __invoke(Container $container) |
51
|
|
|
{ |
52
|
|
|
// constructor injection |
53
|
|
|
$instance = $this->arguments instanceof Arguments ? (new \ReflectionClass($this->class))->newInstanceArgs($this->arguments->inject($container)) : new $this->class; |
54
|
|
|
|
55
|
|
|
// setter injection |
56
|
|
|
($this->setterMethods)($instance, $container); |
57
|
|
|
|
58
|
|
|
// bind dependency injected interceptors |
59
|
|
|
if ($this->bind instanceof AspectBind) { |
60
|
|
|
$instance->bindings = $this->bind->inject($container); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $instance; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function __toString() |
70
|
|
|
{ |
71
|
|
|
return $this->class; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws \ReflectionException |
76
|
|
|
* |
77
|
|
|
* @return object |
78
|
|
|
*/ |
79
|
|
|
public function newInstanceArgs(Container $container, array $args) |
80
|
|
|
{ |
81
|
|
|
// constructor injection |
82
|
|
|
$instance = $this->arguments instanceof Arguments ? (new \ReflectionClass($this->class))->newInstanceArgs($args) : new $this->class; |
83
|
|
|
|
84
|
|
|
// setter injection |
85
|
|
|
($this->setterMethods)($instance, $container); |
86
|
|
|
|
87
|
|
|
// bind dependency injected interceptors |
88
|
|
|
if ($this->bind instanceof AspectBind) { |
89
|
|
|
$instance->bindings = $this->bind->inject($container); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $instance; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $class |
97
|
|
|
*/ |
98
|
|
|
public function weaveAspects($class, AopBind $bind) |
99
|
|
|
{ |
100
|
|
|
$this->class = $class; |
101
|
|
|
$this->bind = new AspectBind($bind); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|