|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Common; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Common\Exception\DuplicateRegistrationException; |
|
28
|
|
|
use Brickoo\Component\Common\Exception\IdentifierLockedException; |
|
29
|
|
|
use Brickoo\Component\Common\Exception\IdentifierNotRegisteredException; |
|
30
|
|
|
use Brickoo\Component\Common\Exception\ReadonlyModeException; |
|
31
|
|
|
use Brickoo\Component\Common\Assert; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Registry |
|
35
|
|
|
* |
|
36
|
|
|
* Registrations are stored as key value pairs. |
|
37
|
|
|
* Provides getter and setter for accessing the registrations. |
|
38
|
|
|
* Provides lock functionality for each identifier and an read only mode for all identifiers. |
|
39
|
|
|
* @author Celestino Diaz <[email protected]> |
|
40
|
|
|
*/ |
|
41
|
|
|
class Registry extends Locker { |
|
42
|
|
|
|
|
43
|
|
|
/** @var array */ |
|
44
|
|
|
protected $registrations; |
|
45
|
|
|
|
|
46
|
|
|
/** @var boolean */ |
|
47
|
|
|
protected $readOnly; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Class constructor. |
|
51
|
|
|
* @param array $registrations the registrations to add |
|
52
|
|
|
* @param boolean $readOnly initialize mode for the registry |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function __construct(array $registrations = [], $readOnly = false) { |
|
55
|
1 |
|
parent::__construct(); |
|
56
|
1 |
|
$this->registrations = $registrations; |
|
57
|
1 |
|
$this->readOnly = (!empty($registrations)) && (boolean)$readOnly; |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return all assigned registrations as an array. |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function toArray() { |
|
65
|
1 |
|
return $this->registrations; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add a registration to the registry. |
|
70
|
|
|
* @param mixed $registrations the registrations to add |
|
71
|
|
|
* @throws \InvalidArgumentException if passed registrations is empty |
|
72
|
|
|
* @return \Brickoo\Component\Common\Registry |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function add($registrations) { |
|
75
|
3 |
|
if ((!is_array($registrations)) && (!$registrations instanceof \Traversable)) { |
|
76
|
1 |
|
$registrations = [$registrations]; |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
3 |
|
foreach ($registrations as $identifier => $value) { |
|
80
|
3 |
|
$this->register($identifier, $value); |
|
81
|
3 |
|
} |
|
82
|
3 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return the registered value from the given identifier. |
|
87
|
|
|
* @param string $identifier the identifier so retrieve the value from |
|
88
|
|
|
* @throws \InvalidArgumentException if the identifier is not valid |
|
89
|
|
|
* @throws \Brickoo\Component\Common\Exception\IdentifierNotRegisteredException |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function get($identifier) { |
|
93
|
2 |
|
Assert::isStringOrInteger($identifier); |
|
94
|
|
|
|
|
95
|
2 |
|
if (!$this->isRegistered($identifier)) { |
|
96
|
1 |
|
throw new IdentifierNotRegisteredException($identifier); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return $this->registrations[$identifier]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Register an identifier-value pair. |
|
104
|
|
|
* Take care of registering objects who are assigned somewhere else |
|
105
|
|
|
* as an reference the changes applies to the registered objects as well. |
|
106
|
|
|
* @param string $identifier the identifier to register |
|
107
|
|
|
* @param mixed $value the identifier value to register with |
|
108
|
|
|
* @throws \Brickoo\Component\Common\Exception\DuplicateRegistrationException |
|
109
|
|
|
* @throws \Brickoo\Component\Common\Exception\ReadonlyModeException |
|
110
|
|
|
* @return \Brickoo\Component\Common\Registry |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function register($identifier, $value) { |
|
113
|
3 |
|
Assert::isStringOrInteger($identifier); |
|
114
|
|
|
|
|
115
|
3 |
|
if ($this->isRegistered($identifier)) { |
|
116
|
1 |
|
throw new DuplicateRegistrationException($identifier); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
3 |
|
$this->set($identifier, $value); |
|
120
|
2 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Overrides an existing identifier with the new value (!). |
|
125
|
|
|
* If the identifier ist not registered it will be registered. |
|
126
|
|
|
* @param string $identifier the identifier to register |
|
127
|
|
|
* @param mixed $value the identifier value to register |
|
128
|
|
|
* @throws \Brickoo\Component\Common\Exception\ReadonlyModeException |
|
129
|
|
|
* @throws \Brickoo\Component\Common\Exception\IdentifierLockedException |
|
130
|
|
|
* @return \Brickoo\Component\Common\Registry |
|
131
|
|
|
*/ |
|
132
|
3 |
|
public function override($identifier, $value) { |
|
133
|
3 |
|
Assert::isStringOrInteger($identifier); |
|
134
|
|
|
|
|
135
|
3 |
|
if ($this->isLocked($identifier)) { |
|
136
|
1 |
|
throw new IdentifierLockedException($identifier); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
2 |
|
$this->set($identifier, $value); |
|
140
|
1 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Unregister the identifier and his value. |
|
145
|
|
|
* @param string $identifier the identifier to unregister |
|
146
|
|
|
* @throws \Brickoo\Component\Common\Exception\ReadonlyModeException |
|
147
|
|
|
* @throws \Brickoo\Component\Common\Exception\IdentifierLockedException |
|
148
|
|
|
* @throws \Brickoo\Component\Common\Exception\IdentifierNotRegisteredException |
|
149
|
|
|
* @return \Brickoo\Component\Common\Registry |
|
150
|
|
|
*/ |
|
151
|
4 |
|
public function unregister($identifier) { |
|
152
|
4 |
|
Assert::isStringOrInteger($identifier); |
|
153
|
|
|
|
|
154
|
4 |
|
if ($this->isReadOnly()) { |
|
155
|
1 |
|
throw new ReadonlyModeException(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
3 |
|
if ($this->isLocked($identifier)) { |
|
159
|
1 |
|
throw new IdentifierLockedException($identifier); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
2 |
|
if (!$this->isRegistered($identifier)) { |
|
163
|
1 |
|
throw new IdentifierNotRegisteredException($identifier); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
1 |
|
unset ($this->registrations[$identifier]); |
|
167
|
1 |
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Check if the identifier is registered. |
|
172
|
|
|
* @param string $identifier the identifier to check |
|
173
|
|
|
* @return boolean |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function isRegistered($identifier) { |
|
176
|
1 |
|
Assert::isStringOrInteger($identifier); |
|
177
|
1 |
|
return array_key_exists($identifier, $this->registrations); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Sets the read only mode for all registrations. |
|
182
|
|
|
* True to allow read only, write operations will be not allowed. |
|
183
|
|
|
* False for enable read and write operations, locked identifiers will still being locked . |
|
184
|
|
|
* @param boolean $mode the mode to set |
|
185
|
|
|
* @return \Brickoo\Component\Common\Registry |
|
186
|
|
|
*/ |
|
187
|
1 |
|
public function setReadOnly($mode = true) { |
|
188
|
1 |
|
Assert::isBoolean($mode); |
|
189
|
|
|
|
|
190
|
1 |
|
$this->readOnly = $mode; |
|
191
|
1 |
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Check if the mode is currently set to read only. |
|
196
|
|
|
* @return boolean |
|
197
|
|
|
*/ |
|
198
|
2 |
|
public function isReadOnly() { |
|
199
|
2 |
|
return $this->readOnly; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Countable interface implementation. |
|
204
|
|
|
* Returns the number of registrations. |
|
205
|
|
|
* @see Countable::count() |
|
206
|
|
|
* @return integer |
|
207
|
|
|
*/ |
|
208
|
1 |
|
public function count() { |
|
209
|
1 |
|
return count($this->registrations); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Returns the number of locked identifiers. |
|
214
|
|
|
* @return integer |
|
215
|
|
|
*/ |
|
216
|
1 |
|
public function countLocked() { |
|
217
|
1 |
|
return count($this->locked); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** {@inheritDoc} */ |
|
221
|
1 |
|
public function isIdentifierAvailable($identifier) { |
|
222
|
1 |
|
return $this->isRegistered($identifier); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Set the registered identifier and value. |
|
227
|
|
|
* @param string $identifier |
|
228
|
|
|
* @param mixed $value |
|
229
|
|
|
* @throws Exception\DuplicateRegistrationException |
|
230
|
|
|
* @throws Exception\ReadonlyModeException |
|
231
|
|
|
* @return \Brickoo\Component\Common\Registry |
|
232
|
|
|
*/ |
|
233
|
3 |
|
private function set($identifier, $value) { |
|
234
|
3 |
|
if ($this->isReadOnly()) { |
|
235
|
2 |
|
throw new ReadonlyModeException(); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
2 |
|
$this->registrations[$identifier] = $value; |
|
239
|
2 |
|
return $this; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
|