-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathContainer.php
109 lines (87 loc) · 3.08 KB
/
Container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace PHPWatch\SimpleContainer;
use ArrayAccess;
use PHPWatch\SimpleContainer\Exception\BadMethodCallException;
use PHPWatch\SimpleContainer\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use function array_key_exists;
use function gettype;
use function is_callable;
use function sprintf;
class Container implements ArrayAccess, ContainerInterface {
private array $definitions;
private array $generated = [];
private array $protected = [];
private array $factories = [];
public function __construct(array $definitions = []) {
$this->definitions = $definitions;
}
private function getService(string $id): mixed {
if (!$this->has($id)) {
throw new NotFoundException(sprintf('Container key "%s" is not defined', $id));
}
if (array_key_exists($id, $this->generated)) {
return $this->generated[$id];
}
if (!is_callable($this->definitions[$id]) || isset($this->protected[$id])) {
return $this->definitions[$id];
}
if (isset($this->factories[$id])) {
return $this->definitions[$id]($this);
}
return $this->generated[$id] = $this->definitions[$id]($this);
}
public function set(string $id, mixed $value): void {
if (array_key_exists($id, $this->definitions)) {
unset($this->generated[$id], $this->factories[$id], $this->protected[$id]);
}
$this->definitions[$id] = $value;
}
public function setProtected(string $id, ?callable $value = null): void {
if ($value === null) {
$value = $this->getDefaultDefinition($id, sprintf('Attempt to set container ID "%s" as protected, but it is not already set nor provided in the function call.', $id));
}
$this->set($id, $value);
$this->protected[$id] = true;
}
public function setFactory(string $id, ?callable $value = null): void {
if ($value === null) {
$value = $this->getDefaultDefinition($id, sprintf('Attempt to set container ID "%s" as factory, but it is not already set nor provided in the function call', $id));
}
$this->set($id, $value);
$this->factories[$id] = true;
}
private function getDefaultDefinition(string $id, string $exception_message): callable {
if (!$this->has($id)) {
throw new BadMethodCallException($exception_message);
}
if (!is_callable($this->definitions[$id])) {
throw new BadMethodCallException(sprintf('Definition for "%s" expected to be a callable, "%s" found', $id, gettype($this->definitions[$id])));
}
return $this->definitions[$id];
}
public function offsetSet(mixed $offset, mixed $value): void {
$this->set($offset, $value);
}
public function offsetUnset(mixed $offset): void {
unset($this->definitions[$offset], $this->generated[$offset], $this->factories[$offset], $this->protected[$offset]);
}
public function offsetExists(mixed $offset): bool {
return array_key_exists($offset, $this->definitions);
}
public function offsetGet(mixed $offset): mixed {
return $this->getService($offset);
}
/**
* @inheritDoc
*/
public function get(string $id): mixed {
return $this->getService($id);
}
/**
* @inheritDoc
*/
public function has(string $id): bool {
return array_key_exists($id, $this->definitions);
}
}