Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.77% |
30 / 31 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
DiRegisterContainer | |
96.77% |
30 / 31 |
|
80.00% |
4 / 5 |
12 | |
0.00% |
0 / 1 |
add | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
remove | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
registerClass | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
registerMapping | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
registerValue | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\DI; |
6 | |
7 | use PeServer\Core\DI\DiContainer; |
8 | use PeServer\Core\DI\DiItem; |
9 | use PeServer\Core\Text; |
10 | use PeServer\Core\Throws\ArgumentException; |
11 | use PeServer\Core\Throws\ArgumentNullException; |
12 | use PeServer\Core\Throws\DiContainerRegisteredException; |
13 | use PeServer\Core\TypeUtility; |
14 | |
15 | /** |
16 | * 登録可能DIコンテナ実装。 |
17 | */ |
18 | class DiRegisterContainer extends DiContainer implements IDiRegisterContainer |
19 | { |
20 | #region IDiRegisterContainer |
21 | |
22 | public function add(string $id, DiItem $item): void |
23 | { |
24 | $this->throwIfDisposed(); |
25 | |
26 | if ($this->has($id)) { |
27 | throw new DiContainerRegisteredException($id); |
28 | } |
29 | |
30 | if (Text::isNullOrWhiteSpace($id)) { //@phpstan-ignore-line [DOCTYPE] |
31 | throw new ArgumentException('$id'); |
32 | } |
33 | |
34 | $this->mapping[$id] = $item; |
35 | } |
36 | |
37 | public function remove(string $id): ?DiItem |
38 | { |
39 | $this->throwIfDisposed(); |
40 | |
41 | if (!$this->has($id)) { |
42 | return null; |
43 | } |
44 | |
45 | $item = $this->mapping[$id]; |
46 | unset($this->mapping[$id]); |
47 | |
48 | return $item; |
49 | } |
50 | |
51 | public function registerClass(string $className, int $lifecycle = DiItem::LIFECYCLE_TRANSIENT): void |
52 | { |
53 | $this->throwIfDisposed(); |
54 | |
55 | $item = $this->remove($className); |
56 | if ($item !== null) { |
57 | $item->dispose(); |
58 | } |
59 | |
60 | $this->add($className, DiItem::class($className, $lifecycle)); |
61 | } |
62 | |
63 | public function registerMapping(string $id, string $className, int $lifecycle = DiItem::LIFECYCLE_TRANSIENT): void |
64 | { |
65 | $this->throwIfDisposed(); |
66 | |
67 | $item = $this->remove($id); |
68 | if ($item !== null) { |
69 | $item->dispose(); |
70 | } |
71 | |
72 | $this->add($id, DiItem::class($className, $lifecycle)); |
73 | } |
74 | |
75 | public function registerValue(?object $value, string $id = Text::EMPTY): void |
76 | { |
77 | $this->throwIfDisposed(); |
78 | |
79 | $registerId = $id; |
80 | if (Text::isNullOrWhiteSpace($registerId)) { |
81 | ArgumentNullException::throwIfNull($value, '$value'); |
82 | |
83 | $registerId = TypeUtility::getType($value); |
84 | } |
85 | |
86 | $item = $this->remove($registerId); |
87 | if ($item !== null) { |
88 | $item->dispose(); |
89 | } |
90 | |
91 | $this->add($registerId, DiItem::value($value)); |
92 | } |
93 | |
94 | #endregion |
95 | } |