1
|
|
|
<?php |
2
|
|
|
namespace UltraLite\Container; |
3
|
|
|
|
4
|
|
|
use Psr\Container\ContainerInterface; |
5
|
|
|
use UltraLite\Container\Exception\DiServiceNotFound; |
6
|
|
|
use Interop\Container\Exception\NotFoundException; |
7
|
|
|
|
8
|
|
|
class Container implements ContainerInterface |
9
|
|
|
{ |
10
|
|
|
/** @var \Closure[] */ |
11
|
|
|
private $serviceFactories = []; |
12
|
|
|
|
13
|
|
|
/** @var array */ |
14
|
|
|
private $services = []; |
15
|
|
|
|
16
|
|
|
/** @var ContainerInterface */ |
17
|
|
|
private $delegateContainer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param \Closure[] $serviceFactories |
21
|
|
|
*/ |
22
|
|
|
public function __construct(array $serviceFactories = []) |
23
|
|
|
{ |
24
|
|
|
foreach ($serviceFactories as $serviceId => $serviceFactory) { |
25
|
|
|
$this->set($serviceId, $serviceFactory); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $serviceId |
31
|
|
|
* @param \Closure $serviceFactory |
32
|
|
|
*/ |
33
|
|
|
public function set($serviceId, \Closure $serviceFactory) |
34
|
|
|
{ |
35
|
|
|
$this->serviceFactories[$serviceId] = $serviceFactory; |
36
|
|
|
unset($this->services[$serviceId]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $path |
41
|
|
|
*/ |
42
|
|
|
public function configureFromFile(string $path) |
43
|
|
|
{ |
44
|
|
|
foreach (require $path as $serviceId => $serviceFactory) { |
45
|
|
|
$this->set($serviceId, $serviceFactory); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws NotFoundException |
51
|
|
|
* |
52
|
|
|
* @param string $serviceId |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
public function get($serviceId) |
56
|
|
|
{ |
57
|
|
|
if (!$this->has($serviceId)) { |
58
|
|
|
throw DiServiceNotFound::createFromServiceId($serviceId); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!isset($this->services[$serviceId])) { |
62
|
|
|
$this->services[$serviceId] = $this->getServiceFromFactory($serviceId); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->services[$serviceId]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string[] |
70
|
|
|
*/ |
71
|
|
|
public function listServiceIds() : array |
72
|
|
|
{ |
73
|
|
|
return array_keys($this->serviceFactories); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
private function getServiceFromFactory(string $serviceId) |
80
|
|
|
{ |
81
|
|
|
$serviceFactory = $this->serviceFactories[$serviceId]; |
82
|
|
|
$containerToUseForDependencies = $this->delegateContainer ?: $this; |
83
|
|
|
return $serviceFactory($containerToUseForDependencies); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $serviceId |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function has($serviceId) |
91
|
|
|
{ |
92
|
|
|
return isset($this->serviceFactories[$serviceId]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setDelegateContainer(ContainerInterface $delegateContainer) |
96
|
|
|
{ |
97
|
|
|
$this->delegateContainer = $delegateContainer; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|