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