1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yuca\SingletonContainer; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
|
9
|
|
|
class SingletonContainer implements ContainerInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Decorated container. |
13
|
|
|
* |
14
|
|
|
* @var ContainerInterface |
15
|
|
|
*/ |
16
|
|
|
private $container; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Cached instances. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $instances = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param ContainerInterface $container Decorated container. |
29
|
|
|
*/ |
30
|
39 |
|
public function __construct(ContainerInterface $container) |
31
|
|
|
{ |
32
|
39 |
|
$this->container = $container; |
33
|
39 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Finds an entry by its identifier and returns it. |
37
|
|
|
* Searches first amongst cached entries and them in the decorated container. |
38
|
|
|
* |
39
|
|
|
* @param string $id Identifier of the entry to look for. |
40
|
|
|
* |
41
|
|
|
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
42
|
|
|
* @throws ContainerExceptionInterface Error while retrieving the entry. |
43
|
|
|
* |
44
|
|
|
* @return mixed Entry. |
45
|
|
|
*/ |
46
|
33 |
|
public function get($id) |
47
|
|
|
{ |
48
|
33 |
|
if (!array_key_exists($id, $this->instances)) { |
49
|
33 |
|
$this->instances[$id] = $this->container->get($id); |
50
|
|
|
} |
51
|
27 |
|
return $this->instances[$id]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns true if the decorated container can return an entry for the given identifier. |
56
|
|
|
* Returns false otherwise. |
57
|
|
|
* |
58
|
|
|
* `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
59
|
|
|
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
60
|
|
|
* |
61
|
|
|
* @param string $id Identifier of the entry to look for. |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
6 |
|
public function has($id) |
66
|
|
|
{ |
67
|
6 |
|
return $this->container->has($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns true if the entry for the given identifier is already cached. |
72
|
|
|
* Returns false otherwise. |
73
|
|
|
* |
74
|
|
|
* @param string $id Identifier of the entry to look for. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
9 |
|
public function isCached($id) |
79
|
|
|
{ |
80
|
9 |
|
return array_key_exists($id, $this->instances); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Releases the cached instance for the given identifier if cached. |
85
|
|
|
* |
86
|
|
|
* @param string $id Identifier of the entry to look for. |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
9 |
|
public function clear($id) |
91
|
|
|
{ |
92
|
9 |
|
unset($this->instances[$id]); |
93
|
9 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Releases all cached instances. Useful for memory reduction. |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
6 |
|
public function clearAll() |
101
|
|
|
{ |
102
|
6 |
|
$this->instances = array(); |
103
|
6 |
|
} |
104
|
|
|
} |
105
|
|
|
|