|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Conia\Chuck\Di; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Conia\Chuck\Exception\ContainerException; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @psalm-type Args array<string|mixed>|Closure(): array<string|mixed> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Entry |
|
14
|
|
|
{ |
|
15
|
|
|
/** @psalm-var null|Args */ |
|
16
|
|
|
protected array|Closure|null $args = null; |
|
17
|
|
|
protected bool $asIs = false; |
|
18
|
|
|
protected bool $reify; |
|
19
|
|
|
protected mixed $instance = null; |
|
20
|
|
|
|
|
21
|
|
|
/** @psalm-var list<Call> */ |
|
22
|
|
|
protected array $calls = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @psalm-param non-empty-string $id |
|
26
|
|
|
* */ |
|
27
|
164 |
|
public function __construct( |
|
28
|
|
|
readonly protected string $id, |
|
29
|
|
|
protected mixed $definition |
|
30
|
|
|
) { |
|
31
|
164 |
|
$this->reify = $this->negotiateReify($definition); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
93 |
|
public function shouldReify(): bool |
|
35
|
|
|
{ |
|
36
|
93 |
|
return $this->reify; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
101 |
|
public function shouldReturnAsIs(): bool |
|
40
|
|
|
{ |
|
41
|
101 |
|
return $this->asIs; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
94 |
|
public function getArgs(): array|Closure|null |
|
45
|
|
|
{ |
|
46
|
94 |
|
return $this->args; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function reify(bool $reify = true): static |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->reify = $reify; |
|
52
|
|
|
|
|
53
|
1 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function asIs(bool $asIs = true): static |
|
57
|
|
|
{ |
|
58
|
|
|
// An update call is unecessary |
|
59
|
2 |
|
if ($asIs) { |
|
60
|
2 |
|
$this->reify = false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
$this->asIs = $asIs; |
|
64
|
|
|
|
|
65
|
2 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
133 |
|
public function args(mixed ...$args): static |
|
69
|
|
|
{ |
|
70
|
133 |
|
$numArgs = count($args); |
|
71
|
|
|
|
|
72
|
133 |
|
if ($numArgs === 1) { |
|
73
|
7 |
|
if (is_string(array_key_first($args))) { |
|
74
|
|
|
/** @psalm-var Args */ |
|
75
|
1 |
|
$this->args = $args; |
|
76
|
6 |
|
} elseif (is_array($args[0]) || $args[0] instanceof Closure) { |
|
77
|
|
|
/** @psalm-var Args */ |
|
78
|
5 |
|
$this->args = $args[0]; |
|
79
|
|
|
} else { |
|
80
|
7 |
|
throw new ContainerException( |
|
81
|
7 |
|
'Registry entry arguments can be passed as a single associative array, ' . |
|
82
|
7 |
|
'as named arguments, or as a Closure' |
|
83
|
7 |
|
); |
|
84
|
|
|
} |
|
85
|
128 |
|
} elseif ($numArgs > 1) { |
|
86
|
128 |
|
if (!is_string(array_key_first($args))) { |
|
87
|
1 |
|
throw new ContainerException( |
|
88
|
1 |
|
'Registry entry arguments can be passed as a single associative array, ' . |
|
89
|
1 |
|
'as named arguments, or as a Closure' |
|
90
|
1 |
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
127 |
|
$this->args = $args; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
131 |
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
public function call(string $method, mixed ...$args): static |
|
100
|
|
|
{ |
|
101
|
2 |
|
$this->calls[] = new Call($method, ...$args); |
|
102
|
|
|
|
|
103
|
2 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
10 |
|
public function definition(): mixed |
|
107
|
|
|
{ |
|
108
|
10 |
|
return $this->definition; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
public function instance(): mixed |
|
112
|
|
|
{ |
|
113
|
3 |
|
return $this->instance; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
101 |
|
public function get(): mixed |
|
117
|
|
|
{ |
|
118
|
101 |
|
return $this->instance ?? $this->definition; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
93 |
|
public function set(mixed $instance): void |
|
122
|
|
|
{ |
|
123
|
93 |
|
$this->instance = $instance; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** @psalm-return list<Call> */ |
|
127
|
94 |
|
public function getCalls(): array |
|
128
|
|
|
{ |
|
129
|
94 |
|
return $this->calls; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
164 |
|
protected function negotiateReify(mixed $definition): bool |
|
133
|
|
|
{ |
|
134
|
164 |
|
if (is_string($definition)) { |
|
135
|
144 |
|
if (!class_exists($definition)) { |
|
136
|
144 |
|
return false; |
|
137
|
|
|
} |
|
138
|
162 |
|
} elseif ($definition instanceof Closure) { |
|
139
|
131 |
|
return true; |
|
140
|
|
|
} else { |
|
141
|
162 |
|
if (is_scalar($definition) || is_array($definition) || is_object($definition)) { |
|
142
|
162 |
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
143 |
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|