1
|
|
|
<?PHP |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Resources\Interfaces\Resources |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/resources |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Resources; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Lang\String; |
24
|
|
|
use AppserverIo\Collections\HashMap; |
25
|
|
|
use AppserverIo\Collections\ArrayList; |
26
|
|
|
use AppserverIo\Resources\Interfaces\ResourcesInterface; |
27
|
|
|
use AppserverIo\Resources\Interfaces\ResourceBundleInterface; |
28
|
|
|
use AppserverIo\Resources\Exceptions\SystemLocaleNotExistsException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Abstract class of all resources. |
32
|
|
|
* |
33
|
|
|
* @author Tim Wagner <[email protected]> |
34
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
35
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
36
|
|
|
* @link https://github.com/appserver-io/resources |
37
|
|
|
* @link http://www.appserver.io |
38
|
|
|
*/ |
39
|
|
|
abstract class AbstractResources implements ResourcesInterface |
40
|
|
|
{ |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Holds the flag to return null if a requested propert does not exists. |
44
|
|
|
* |
45
|
|
|
* @var boolean |
46
|
|
|
*/ |
47
|
|
|
protected $returnNull = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Holds the default system locale of this resources. |
51
|
|
|
* |
52
|
|
|
* @var \AppserverIo\Resources\SystemLocale |
53
|
|
|
*/ |
54
|
|
|
protected $defaultSystemLocale = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Holds the name of the resource bundle |
58
|
|
|
* |
59
|
|
|
* @var \AppserverIo\Lang\String |
60
|
|
|
*/ |
61
|
|
|
protected $name = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Holds the HashMap with the initialized ResourceBundle instances. |
65
|
|
|
* |
66
|
|
|
* @var \AppserverIo\Collections\HashMap |
67
|
|
|
*/ |
68
|
|
|
protected $bundles = null; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The constructor initializes the resources with the database connection to |
72
|
|
|
* load the resources from. |
73
|
|
|
* |
74
|
|
|
* @param \AppserverIo\Lang\String $name Holds the logical name of the Resources to create |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function __construct(String $name) |
79
|
|
|
{ |
80
|
|
|
$this->setName($name); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* This method sets the name of the resource bundle. |
85
|
|
|
* |
86
|
|
|
* @param \AppserverIo\Lang\String $name The name of the resource bundle |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function setName(String $name) |
91
|
|
|
{ |
92
|
|
|
$this->name = $name; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* This method returns the name of the resource bundle. |
97
|
|
|
* |
98
|
|
|
* @return \AppserverIo\Lang\String The name of the resource bundle |
99
|
|
|
*/ |
100
|
|
|
public function getName() |
101
|
|
|
{ |
102
|
|
|
return $this->name; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return TRUE if resource getter methods will return null instead of throwing an exception on invalid |
107
|
|
|
* key values. |
108
|
|
|
* |
109
|
|
|
* @return boolean TRUE if null is returned for invalid key values. |
110
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesInterface::isReturnNull() |
111
|
|
|
*/ |
112
|
|
|
public function isReturnNull() |
113
|
|
|
{ |
114
|
|
|
return $this->returnNull; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set a flag determining whether resource getter methods should return null instead of throwing an |
119
|
|
|
* exception on invalid key values. |
120
|
|
|
* |
121
|
|
|
* @param boolean $returnNull The new flag value |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesInterface::setReturnNull($returnNull) |
125
|
|
|
*/ |
126
|
|
|
public function setReturnNull($returnNull) |
127
|
|
|
{ |
128
|
|
|
$this->returnNull = $returnNull; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Sets the default system locale for this resources. |
133
|
|
|
* |
134
|
|
|
* @param \AppserverIo\Resources\SystemLocale $systemLocale The default system locale for this resources |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function setDefaultSystemLocale(SystemLocale $systemLocale) |
139
|
|
|
{ |
140
|
|
|
$this->defaultSystemLocale = $systemLocale; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns the default system locale for this resources. |
145
|
|
|
* |
146
|
|
|
* @return \AppserverIo\Resources\SystemLocale The default system locale for this resources |
147
|
|
|
*/ |
148
|
|
|
public function getDefaultSystemLocale() |
149
|
|
|
{ |
150
|
|
|
return $this->defaultSystemLocale; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Create (if necessary) and return a Resources instance for the specified logical name, with a |
155
|
|
|
* configuration based on the specified configuration string. |
156
|
|
|
* |
157
|
|
|
* @return void |
158
|
|
|
* @throws \AppserverIo\Resources\Exceptions\ResourcesException Is thrown if an exception occurs when the Resources are initialized |
159
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesInterface::initialize() |
160
|
|
|
*/ |
161
|
|
|
public function initialize() |
162
|
|
|
{ |
163
|
|
|
$this->bundles = new HashMap(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Release any internal references to Resources instances that have been returned previously, after |
168
|
|
|
* calling the destroy() method on each such instance. |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesInterface::destroy() |
172
|
|
|
*/ |
173
|
|
|
public function destroy() |
174
|
|
|
{ |
175
|
|
|
|
176
|
|
|
// invoke the destroy method of the resource bundle instances |
177
|
|
|
foreach ($this->bundles as $bundle) { |
178
|
|
|
$bundle->destroy(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// reset the HashMap |
182
|
|
|
$this->bundles = new HashMap(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* This method saves all resource files back to the file system. |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
public function save() |
191
|
|
|
{ |
192
|
|
|
|
193
|
|
|
// iterate over all resources and save them |
194
|
|
|
foreach ($this->bundles as $resourcesBundle) { |
195
|
|
|
$resourcesBundle->save(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Returns the keys of this Resources instance |
201
|
|
|
* as an ArrayList. |
202
|
|
|
* |
203
|
|
|
* @return \AppserverIo\Collections\ArrayList Holds the keys of this Resources instance |
204
|
|
|
* @see \AppserverIo\Resources\Interfaces\ResourcesInterface::getKeys() |
205
|
|
|
*/ |
206
|
|
|
public function getKeys() |
207
|
|
|
{ |
208
|
|
|
|
209
|
|
|
// initialize the array with the keys |
210
|
|
|
$keys = array(); |
211
|
|
|
|
212
|
|
|
// iterate over all bundles and the keys of the bundles |
213
|
|
|
foreach ($this->bundles as $bundle) { |
214
|
|
|
foreach ($bundle->getKeys() as $key) { |
215
|
|
|
if (!in_array($key, $keys)) { |
216
|
|
|
$keys[] = $key; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// return the ArrayList with the unique keys |
222
|
|
|
return new ArrayList($keys); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* This method adds the passed resource bundle instance to the HashMap with the initialized bundles. |
227
|
|
|
* |
228
|
|
|
* If resources with the same system locale as the system locale of the passed resource bundle instance |
229
|
|
|
* exists, they will be replaced by the passed one. |
230
|
|
|
* |
231
|
|
|
* @param \AppserverIo\Resources\Interfaces\ResourceBundleInterface $newBundle Holds the resource bundle to add |
232
|
|
|
* |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
protected function add(ResourceBundleInterface $newBundle) |
236
|
|
|
{ |
237
|
|
|
$this->bundles->add($newBundle->getSystemLocale()->__toString(), $newBundle); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* This method returns the resources for the passed system locale. |
242
|
|
|
* |
243
|
|
|
* If the requested resources does not exist an exception is thrown. |
244
|
|
|
* |
245
|
|
|
* @param \AppserverIo\Resources\SystemLocale $systemLocale Holds the system locale to return the resources for |
246
|
|
|
* |
247
|
|
|
* @return \AppserverIo\Resources\Interfaces\ResourceBundleInterface Holds the initialized resource bundle instance |
248
|
|
|
* @throws \AppserverIo\Resources\Exceptions\SystemLocaleNotExistsException Is throw if the requested resources is not available |
249
|
|
|
*/ |
250
|
|
|
protected function get(SystemLocale $systemLocale) |
251
|
|
|
{ |
252
|
|
|
|
253
|
|
|
// check if resources for the passed system locale exists |
254
|
|
|
if (!$this->exists($systemLocale)) { |
255
|
|
|
throw new SystemLocaleNotExistsException('ResourceBundle for system locale ' . $systemLocale->__toString() . ' is not available'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// returns the requested resources |
259
|
|
|
return $this->bundles->get($systemLocale->__toString()); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* This method checks if resources for the passed system locale already exists in the bundle or not. |
264
|
|
|
* |
265
|
|
|
* @param \AppserverIo\Resources\SystemLocale $systemLocale Holds the system locale to check for |
266
|
|
|
* |
267
|
|
|
* @return boolean TRUE if resources for the passed system locale exists, else FALSE |
268
|
|
|
*/ |
269
|
|
|
protected function exists(SystemLocale $systemLocale) |
270
|
|
|
{ |
271
|
|
|
return $this->bundles->exists($systemLocale->__toString()); |
|
|
|
|
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..