|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tleckie\Di; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
6
|
|
|
use Tleckie\Di\Definition\Adapter\AdapterInterface; |
|
7
|
|
|
use Tleckie\Di\Definition\Definition; |
|
8
|
|
|
use Tleckie\Di\Definition\Handler\ArrayHandler; |
|
9
|
|
|
use Tleckie\Di\Definition\Handler\ClosureHandler; |
|
10
|
|
|
use Tleckie\Di\Definition\Handler\HandlerInterface; |
|
11
|
|
|
use Tleckie\Di\Definition\Handler\InstanceHandler; |
|
12
|
|
|
use Tleckie\Di\Definition\Handler\NumericHandler; |
|
13
|
|
|
use Tleckie\Di\Definition\Handler\ObjectHandler; |
|
14
|
|
|
use Tleckie\Di\Definition\Handler\StringHandler; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Di |
|
18
|
|
|
* |
|
19
|
|
|
* @package Tleckie\Di |
|
20
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Di implements DiInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var array */ |
|
25
|
|
|
private array $definitions = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
private array $indexed = []; |
|
29
|
|
|
|
|
30
|
|
|
/** @var HandlerInterface|null */ |
|
31
|
|
|
private HandlerInterface|null $handler = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Di constructor. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$handler = new StringHandler($this); |
|
39
|
|
|
|
|
40
|
|
|
$handler |
|
41
|
|
|
->next(new NumericHandler($this)) |
|
42
|
|
|
->next(new InstanceHandler($this)) |
|
43
|
|
|
->next(new ClosureHandler($this)) |
|
44
|
|
|
->next(new ObjectHandler($this)) |
|
45
|
|
|
->next(new ArrayHandler($this)); |
|
46
|
|
|
|
|
47
|
|
|
$this->changeHandler($handler); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param HandlerInterface $handler |
|
52
|
|
|
* @return DiInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
public function changeHandler(HandlerInterface $handler): DiInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$this->handler = $handler; |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return HandlerInterface|null |
|
63
|
|
|
*/ |
|
64
|
|
|
public function handler(): ?HandlerInterface |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->handler; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param AdapterInterface $adapter |
|
71
|
|
|
* @return DiInterface |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setAdapter(AdapterInterface $adapter): DiInterface |
|
74
|
|
|
{ |
|
75
|
|
|
$this->definitions = $adapter->load(); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $id |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
* @throws NotFoundExceptionInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function get(string $id): mixed |
|
86
|
|
|
{ |
|
87
|
|
|
if (isset($this->indexed[$id])) { |
|
88
|
|
|
return $this->indexed[$id]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if (!$this->has($id)) { |
|
92
|
|
|
return $id; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($this->newInstance($id)) { |
|
96
|
|
|
return $this->handler->handle($this->definitions[$id]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->indexed[$id] = $this->handler->handle($this->definitions[$id]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $id |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
public function has(string $id): bool |
|
107
|
|
|
{ |
|
108
|
|
|
return isset($this->definitions[$id]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $id |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
private function newInstance(string $id): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return is_array($this->definitions[$id]) |
|
118
|
|
|
&& isset($this->definitions[$id]['newInstance']) |
|
119
|
|
|
&& true === $this->definitions[$id]['newInstance']; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|