1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PWWEB\Localisation. |
5
|
|
|
* |
6
|
|
|
* Localisation Registrar. |
7
|
|
|
* |
8
|
|
|
* @author Frank Pillukeit <[email protected]> |
9
|
|
|
* @copyright 2020 pw-websolutions.com |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PWWEB\Localisation; |
14
|
|
|
|
15
|
|
|
use Illuminate\Cache\CacheManager; |
16
|
|
|
use Illuminate\Contracts\Cache\Repository; |
17
|
|
|
use Illuminate\Support\Collection; |
18
|
|
|
use PWWeb\Localisation\Contracts\Address; |
19
|
|
|
use PWWeb\Localisation\Contracts\Address\Type as AddressType; |
20
|
|
|
use PWWeb\Localisation\Contracts\Country; |
21
|
|
|
use PWWeb\Localisation\Contracts\Currency; |
22
|
|
|
use PWWeb\Localisation\Contracts\Language; |
23
|
|
|
|
24
|
|
|
class LocalisationRegistrar |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The cache repository. |
28
|
|
|
* |
29
|
|
|
* @var Repository |
30
|
|
|
*/ |
31
|
|
|
protected $cache; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The cache manager object. |
35
|
|
|
* |
36
|
|
|
* @var \Illuminate\Cache\CacheManager |
37
|
|
|
*/ |
38
|
|
|
protected $cacheManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The address class used for the package. |
42
|
|
|
* Can be either original value or overwritten for custom use. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $addressClass; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The country class used for the package. |
50
|
|
|
* Can be either original value or overwritten for custom use. |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $countryClass; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The currency class used for the package. |
58
|
|
|
* Can be either original value or overwritten for custom use. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $currencyClass; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* The language class used for the package. |
66
|
|
|
* Can be either original value or overwritten for custom use. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $languageClass; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* The set of addresses available in the cache. |
74
|
|
|
* |
75
|
|
|
* @var \Illuminate\Support\Collection |
76
|
|
|
*/ |
77
|
|
|
protected $addresses; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* The set of languages available in the cache. |
81
|
|
|
* |
82
|
|
|
* @var \Illuminate\Support\Collection |
83
|
|
|
*/ |
84
|
|
|
protected $languages; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The cache expiration time. |
88
|
|
|
* |
89
|
|
|
* @var DateInterval|integer |
|
|
|
|
90
|
|
|
*/ |
91
|
|
|
public static $cacheExpirationTime; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* The cache key. |
95
|
|
|
* |
96
|
|
|
* @var string |
97
|
|
|
*/ |
98
|
|
|
public static $cacheKey; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The cache model key. |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
public static $cacheModelKey; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* PermissionRegistrar constructor. |
109
|
|
|
* |
110
|
|
|
* @param \Illuminate\Cache\CacheManager $cacheManager The cache manager object |
111
|
|
|
*/ |
112
|
|
|
public function __construct(CacheManager $cacheManager) |
113
|
|
|
{ |
114
|
|
|
$this->addressClass = config('pwweb.localisation.models.address'); |
115
|
|
|
$this->addressTypeClass = config('pwweb.localisation.models.address_type'); |
|
|
|
|
116
|
|
|
$this->countryClass = config('pwweb.localisation.models.country'); |
117
|
|
|
$this->currencyClass = config('pwweb.localisation.models.currency'); |
118
|
|
|
$this->languageClass = config('pwweb.localisation.models.language'); |
119
|
|
|
|
120
|
|
|
$this->cacheManager = $cacheManager; |
121
|
|
|
$this->initializeCache(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Initialize the cache for the package. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
protected function initializeCache() |
130
|
|
|
{ |
131
|
|
|
self::$cacheExpirationTime = config('pwweb.localisation.cache.expiration_time', config('pwweb.localisation.cache_expiration_time')); |
132
|
|
|
|
133
|
|
|
self::$cacheKey = config('pwweb.localisation.cache.key'); |
134
|
|
|
self::$cacheModelKey = config('pwweb.localisation.cache.model_key'); |
135
|
|
|
|
136
|
|
|
$this->cache = $this->getCacheStoreFromConfig(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Retrieve the cache store from the configuration of the package. |
141
|
|
|
* |
142
|
|
|
* @return Repository Cache store |
143
|
|
|
*/ |
144
|
|
|
protected function getCacheStoreFromConfig(): Repository |
145
|
|
|
{ |
146
|
|
|
// the 'default' fallback here is from the localisation.php config file, where 'default' means to use config(cache.default) |
147
|
|
|
$cacheDriver = config('pwweb.localisation.cache.store', 'default'); |
148
|
|
|
|
149
|
|
|
// when 'default' is specified, no action is required since we already have the default instance |
150
|
|
|
if ('default' === $cacheDriver) { |
151
|
|
|
return $this->cacheManager->store(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// if an undefined cache store is specified, fallback to 'array' which is Laravel's closest equiv to 'none' |
155
|
|
|
if (false === \array_key_exists($cacheDriver, config('cache.stores'))) { |
156
|
|
|
$cacheDriver = 'array'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->cacheManager->store($cacheDriver); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Register the languages check method. |
164
|
|
|
* |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function registerLanguages(): bool |
168
|
|
|
{ |
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Flush the cache. |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function forgetCachedAddresses() |
178
|
|
|
{ |
179
|
|
|
$this->addresses = null; |
180
|
|
|
|
181
|
|
|
return $this->cache->forget(self::$cacheKey); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Flush the cache. |
186
|
|
|
* |
187
|
|
|
* @return bool |
188
|
|
|
*/ |
189
|
|
|
public function forgetCachedLanguages() |
190
|
|
|
{ |
191
|
|
|
$this->languages = null; |
192
|
|
|
|
193
|
|
|
return $this->cache->forget(self::$cacheKey); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Clear class languages. |
198
|
|
|
* This is only intended to be called by the LocalisationServiceProvider on boot, |
199
|
|
|
* so that long-running instances like Swoole don't keep old data in memory. |
200
|
|
|
* |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
|
|
public function clearClassLanguages() |
204
|
|
|
{ |
205
|
|
|
$this->languages = null; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the addresses based on the passed params. |
210
|
|
|
* |
211
|
|
|
* @param array $params additional parameters for query |
212
|
|
|
* |
213
|
|
|
* @return \Illuminate\Support\Collection |
214
|
|
|
*/ |
215
|
|
|
public function getAddresses(array $params = []): Collection |
216
|
|
|
{ |
217
|
|
|
if (null === $this->addresses) { |
218
|
|
|
$this->addresses = $this->cache->remember( |
219
|
|
|
self::$cacheKey . '.addresses', |
220
|
|
|
self::$cacheExpirationTime, |
221
|
|
|
function () { |
222
|
|
|
return $this->getAddressClass() |
223
|
|
|
//->with('system_address_types') |
224
|
|
|
->get(); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
$addresses = clone $this->addresses; |
230
|
|
|
|
231
|
|
|
foreach ($params as $attr => $value) { |
232
|
|
|
$attr = 'type' === $attr ? 'type.name' : $attr; |
233
|
|
|
$addresses = $addresses->where($attr, $value); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $addresses; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get the languages based on the passed params. |
241
|
|
|
* |
242
|
|
|
* @param array $params additional parameters for query |
243
|
|
|
* |
244
|
|
|
* @return \Illuminate\Support\Collection |
245
|
|
|
*/ |
246
|
|
|
public function getLanguages(array $params = []): Collection |
247
|
|
|
{ |
248
|
|
|
if (null === $this->languages) { |
249
|
|
|
$this->languages = $this->cache->remember( |
250
|
|
|
self::$cacheKey, |
251
|
|
|
self::$cacheExpirationTime, |
252
|
|
|
function () { |
253
|
|
|
return $this->getLanguageClass() |
254
|
|
|
//->with('countries') |
255
|
|
|
->get(); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$languages = clone $this->languages; |
261
|
|
|
|
262
|
|
|
foreach ($params as $attr => $value) { |
263
|
|
|
$languages = $languages->where($attr, $value); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $languages; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Get an instance of the address class. |
271
|
|
|
* |
272
|
|
|
* @return PWWeb\Localisation\Contract\Address |
|
|
|
|
273
|
|
|
*/ |
274
|
|
|
public function getAddressClass(): Address |
275
|
|
|
{ |
276
|
|
|
return app($this->addressClass); |
|
|
|
|
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get an instance of the address class. |
281
|
|
|
* |
282
|
|
|
* @return PWWeb\Localisation\Contract\Address\Type |
|
|
|
|
283
|
|
|
*/ |
284
|
|
|
public function getAddressTypeClass(): AddressType |
285
|
|
|
{ |
286
|
|
|
return app($this->addressTypeClass); |
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get an instance of the country class. |
291
|
|
|
* |
292
|
|
|
* @return PWWeb\Localisation\Contract\Country |
|
|
|
|
293
|
|
|
*/ |
294
|
|
|
public function getCountryClass(): Country |
295
|
|
|
{ |
296
|
|
|
return app($this->countryClass); |
|
|
|
|
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Get an instance of the currency class. |
301
|
|
|
* |
302
|
|
|
* @return PWWeb\Localisation\Contract\Currency |
|
|
|
|
303
|
|
|
*/ |
304
|
|
|
public function getCurrencyClass(): Currency |
305
|
|
|
{ |
306
|
|
|
return app($this->currencyClass); |
|
|
|
|
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Get an instance of the language class. |
311
|
|
|
* |
312
|
|
|
* @return PWWeb\Localisation\Contract\Language |
|
|
|
|
313
|
|
|
*/ |
314
|
|
|
public function getLanguageClass(): Language |
315
|
|
|
{ |
316
|
|
|
return app($this->languageClass); |
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Set the instance of the address class. |
321
|
|
|
* |
322
|
|
|
* @param string $addressClass the address class to be used |
323
|
|
|
* |
324
|
|
|
* @return object |
325
|
|
|
*/ |
326
|
|
|
public function setAddressClass(string $addressClass) |
327
|
|
|
{ |
328
|
|
|
$this->addressClass = $addressClass; |
329
|
|
|
|
330
|
|
|
return $this; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Set the instance of the language class. |
335
|
|
|
* |
336
|
|
|
* @param string $languageClass the language class to be used |
337
|
|
|
* |
338
|
|
|
* @return object |
339
|
|
|
*/ |
340
|
|
|
public function setLanguageClass(string $languageClass) |
341
|
|
|
{ |
342
|
|
|
$this->languageClass = $languageClass; |
343
|
|
|
|
344
|
|
|
return $this; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Get the instance of the Cache Store. |
349
|
|
|
* |
350
|
|
|
* @return \Illuminate\Contracts\Cache\Store |
351
|
|
|
*/ |
352
|
|
|
public function getCacheStore(): \Illuminate\Contracts\Cache\Store |
353
|
|
|
{ |
354
|
|
|
return $this->cache->getStore(); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|