1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\RegistryBundle\Model; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\RegistryBundle\Exception\LazyServiceAlreadyExistsException; |
15
|
|
|
use Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException; |
16
|
|
|
use Lug\Component\Registry\Model\RegistryInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author GeLo <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class LazyRegistry implements LazyRegistryInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RegistryInterface |
31
|
|
|
*/ |
32
|
|
|
private $registry; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private $services = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ContainerInterface $container |
41
|
|
|
* @param RegistryInterface $registry |
42
|
|
|
* @param string[] $services |
43
|
|
|
*/ |
44
|
|
|
public function __construct(ContainerInterface $container, RegistryInterface $registry, array $services = []) |
45
|
|
|
{ |
46
|
|
|
$this->container = $container; |
47
|
|
|
$this->registry = $registry; |
48
|
|
|
|
49
|
|
|
foreach ($services as $type => $service) { |
50
|
|
|
$this->setLazy($type, $service); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function hasLazy($type) |
58
|
|
|
{ |
59
|
|
|
return array_key_exists($type, $this->services) && $this->container->has($this->services[$type]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getLazy($type) |
66
|
|
|
{ |
67
|
|
|
if (!$this->hasLazy($type)) { |
68
|
|
|
throw new LazyServiceNotFoundException(sprintf('The lazy service "%s" could not be found.', $type)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->services[$type]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function setLazy($type, $service) |
78
|
|
|
{ |
79
|
|
|
if ($this->hasLazy($type)) { |
80
|
|
|
throw new LazyServiceAlreadyExistsException(sprintf('The lazy service "%s" already exists.', $type)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->services[$type] = $service; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function removeLazy($type) |
90
|
|
|
{ |
91
|
|
|
if (!$this->hasLazy($type)) { |
92
|
|
|
throw new LazyServiceNotFoundException(sprintf('The lazy service "%s" could not be found.', $type)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
unset($this->services[$type]); |
96
|
|
|
|
97
|
|
|
if (isset($this->registry[$type])) { |
98
|
|
|
unset($this->registry[$type]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function offsetExists($offset) |
106
|
|
|
{ |
107
|
|
|
return $this->hasLazy($offset) || isset($this->registry[$offset]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function offsetGet($offset) |
114
|
|
|
{ |
115
|
|
|
$this->lazyLoad($offset); |
116
|
|
|
|
117
|
|
|
return $this->registry[$offset]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function offsetSet($offset, $value) |
124
|
|
|
{ |
125
|
|
|
if ($this->hasLazy($offset)) { |
126
|
|
|
$this->lazyLoad($offset); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->registry[$offset] = $value; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function offsetUnset($offset) |
136
|
|
|
{ |
137
|
|
|
if ($this->hasLazy($offset)) { |
138
|
|
|
$this->lazyLoad($offset); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
unset($this->registry[$offset]); |
142
|
|
|
|
143
|
|
|
if ($this->hasLazy($offset)) { |
144
|
|
|
$this->removeLazy($offset); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function count() |
152
|
|
|
{ |
153
|
|
|
$this->load(); |
154
|
|
|
|
155
|
|
|
return count($this->registry); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function getIterator() |
162
|
|
|
{ |
163
|
|
|
$this->load(); |
164
|
|
|
|
165
|
|
|
return $this->registry->getIterator(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
private function load() |
169
|
|
|
{ |
170
|
|
|
foreach (array_keys($this->services) as $type) { |
171
|
|
|
$this->lazyLoad($type); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $type |
177
|
|
|
*/ |
178
|
|
|
private function lazyLoad($type) |
179
|
|
|
{ |
180
|
|
|
if (!isset($this->registry[$type])) { |
181
|
|
|
$this->registry[$type] = $this->container->get($this->getLazy($type)); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|