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\Properties\Properties; |
25
|
|
|
use AppserverIo\Collections\ArrayList; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This class is a container for the resources and provides methods for handling them. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2018 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/appserver-io/resources |
34
|
|
|
* @link http://www.appserver.io |
35
|
|
|
*/ |
36
|
|
|
class PropertyResourceBundle extends AbstractResourceBundle |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Holds the optional separator for the filename and the locale key. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
const SEPARATOR = '_'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Path to the property file with the resource properties. |
48
|
|
|
* |
49
|
|
|
* @var \AppserverIo\Lang\String |
50
|
|
|
*/ |
51
|
|
|
protected $config = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The initialized resource properties, |
55
|
|
|
* |
56
|
|
|
* @var \AppserverIo\Properties\Properties |
57
|
|
|
*/ |
58
|
|
|
protected $properties = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Initializes the resource bundle with the property resources from the passed file and the locale to use. |
62
|
|
|
* |
63
|
|
|
* @param \AppserverIo\Lang\String $config The path to the property file with the resources |
64
|
|
|
* @param \AppserverIo\Resources\SystemLocale $systemLocale The system locale to use |
65
|
|
|
*/ |
66
|
|
|
protected function __construct(String $config, SystemLocale $systemLocale) |
67
|
|
|
{ |
68
|
|
|
|
69
|
|
|
// initialize the configuration |
70
|
|
|
$this->config = $config; |
|
|
|
|
71
|
|
|
|
72
|
|
|
// invoke the parent method |
73
|
|
|
parent::__construct($systemLocale); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* This method initializes the property resource bundle for the properties with the passed name and locale. |
78
|
|
|
* |
79
|
|
|
* @param \AppserverIo\Lang\String $config Holds the name of the property file to load the resources from |
80
|
|
|
* @param \AppserverIo\Resources\SystemLocale $systemLocale Holds the system locale of the property resource bundle to load |
81
|
|
|
* |
82
|
|
|
* @return \AppserverIo\Resources\PropertyResourceBundle Holds the initialized property resource bundle |
83
|
|
|
*/ |
84
|
|
|
public static function getBundle(String $config, SystemLocale $systemLocale = null) |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
// use the default system locale if no locale has been passed |
88
|
|
|
if ($systemLocale == null) { |
89
|
|
|
$systemLocale = SystemLocale::getDefault(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// initialize and return the resource bundle |
93
|
|
|
$bundle = new PropertyResourceBundle($config, $systemLocale); |
94
|
|
|
$bundle->initialize(); |
95
|
|
|
return $bundle; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* This method parses the resource file depending on the actual locale and sets the values in the internal array. |
100
|
|
|
* |
101
|
|
|
* The separator default value is . but can be changed to some other value by passing it as paramter to this method. |
102
|
|
|
* |
103
|
|
|
* For example the filename for an application could be 'applicationresources.en_US.properties'. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function initialize() |
108
|
|
|
{ |
109
|
|
|
|
110
|
|
|
// add the Locale as string |
111
|
|
|
$this->config .= PropertyResourceBundle::SEPARATOR . $this->getSystemLocale()->__toString() . ".properties"; |
112
|
|
|
|
113
|
|
|
// initialize and load the Properties |
114
|
|
|
$this->properties = new Properties(); |
115
|
|
|
$this->properties->load($this->config); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* This method disconnects from the |
120
|
|
|
* database and frees the memory. |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function destroy() |
125
|
|
|
{ |
126
|
|
|
// @TODO Still to implement |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* This method searches in the container for the resource with the key passed as parameter. |
131
|
|
|
* |
132
|
|
|
* @param string $name Holds the key of the requested resource |
133
|
|
|
* @param \AppserverIo\Collections\ArrayList $parameter Holds an ArrayList with parameters with replacements for the placeholders in the resource string |
134
|
|
|
* |
135
|
|
|
* @return string Holds the requested resource value |
136
|
|
|
*/ |
137
|
|
|
public function find($name, ArrayList $parameter = null) |
138
|
|
|
{ |
139
|
|
|
|
140
|
|
|
// initialize an find the resource string |
141
|
|
|
$resource = ""; |
142
|
|
|
|
143
|
|
|
// get the value for the property with the passed key |
144
|
|
|
if (($property = $this->properties->getProperty($name)) != null) { |
145
|
|
|
$resource = $property; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// check if parameter for replacement are passed |
149
|
|
|
if ($parameter != null) { |
150
|
|
|
// replace the placeholders with the passed parameter |
151
|
|
|
foreach ($parameter as $key => $value) { |
152
|
|
|
$resource = str_replace('{' . $key . '}', $value, $resource); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// return the resource string |
157
|
|
|
return $resource; |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* This method replaces the resource string with the passed key in the resource file. |
162
|
|
|
* |
163
|
|
|
* If the resource string does not yes exist, the value will be attached |
164
|
|
|
* |
165
|
|
|
* @param string $name Holds the name of the resource string to replace |
166
|
|
|
* @param string $value Holds the value to replace the original one with |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
public function replace($name, $value) |
171
|
|
|
{ |
172
|
|
|
$this->properties->add($name, $value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* This method attaches the resource string with the passed key in the resource file. |
177
|
|
|
* |
178
|
|
|
* If the resource string already exists, the old one will be kept and the function returns FALSE, else it returns TRUE |
179
|
|
|
* |
180
|
|
|
* @param string $name Holds the name of the resource string to replace |
181
|
|
|
* @param string $value Holds the value to replace the original one with |
182
|
|
|
* |
183
|
|
|
* @return boolean FALSE if a resource string with the passed name already exists |
184
|
|
|
*/ |
185
|
|
|
public function attach($name, $value) |
186
|
|
|
{ |
187
|
|
|
|
188
|
|
|
// query whether or not the value with the passed key already exits |
189
|
|
|
if ($this->properties->exists($name)) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// if not, add the passed resource to the bundle |
194
|
|
|
$this->properties->add($name, $value); |
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* This method returns the first key found for the passed value. |
200
|
|
|
* |
201
|
|
|
* @param string $value Holds the resource value to return the key for |
202
|
|
|
* |
203
|
|
|
* @return string Holds the resource key for the passed value |
204
|
|
|
*/ |
205
|
|
|
public function findKeyByValue($value) |
206
|
|
|
{ |
207
|
|
|
|
208
|
|
|
// try to find the key for the passed value |
209
|
|
|
$key = array_search($value, $this->properties->toIndexedArray()); |
210
|
|
|
|
211
|
|
|
// check if the value exists in the resource bundle |
212
|
|
|
if ($key === false) { |
213
|
|
|
return; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// return the found key |
217
|
|
|
return $key; |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* This method returns the number of resources in the container. |
222
|
|
|
* |
223
|
|
|
* @return integer Number of resources in the container |
224
|
|
|
*/ |
225
|
|
|
public function count() |
226
|
|
|
{ |
227
|
|
|
return $this->properties->size(); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* This method saves the resource string back to the resource file. |
232
|
|
|
* |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
public function save() |
236
|
|
|
{ |
237
|
|
|
$this->properties->store($this->config); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns the keys of this resource bundle instance as an ArrayList. |
242
|
|
|
* |
243
|
|
|
* @return \AppserverIo\Collections\ArrayList Holds the keys of this resource bundle instance |
244
|
|
|
*/ |
245
|
|
|
public function getKeys() |
246
|
|
|
{ |
247
|
|
|
return new ArrayList($this->properties->getKeys()); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
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..