1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace Spryker\Shared\Kernel; |
||
9 | |||
10 | use Exception; |
||
11 | use InvalidArgumentException; |
||
12 | use Spryker\Shared\Config\Config; |
||
13 | use Spryker\Shared\Kernel\Locale\LocaleNotFoundException; |
||
14 | |||
15 | /** |
||
16 | * @deprecated Will be removed after dynamic multi-store is always enabled. |
||
17 | */ |
||
18 | class Store |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | public const APPLICATION_ZED = 'ZED'; |
||
24 | |||
25 | /** |
||
26 | * @var static|null |
||
27 | */ |
||
28 | protected static $instance; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $storeName; |
||
34 | |||
35 | /** |
||
36 | * List of all storeNames |
||
37 | * |
||
38 | * @var array<string> |
||
39 | */ |
||
40 | protected $allStoreNames; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $allStores; |
||
46 | |||
47 | /** |
||
48 | * List of locales |
||
49 | * |
||
50 | * E.g: "de" => "de_DE" |
||
51 | * |
||
52 | * @var array<string> |
||
53 | */ |
||
54 | protected $locales; |
||
55 | |||
56 | /** |
||
57 | * List of countries |
||
58 | * |
||
59 | * @var array<string> |
||
60 | */ |
||
61 | protected $countries; |
||
62 | |||
63 | /** |
||
64 | * Examples: DE, PL |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $currentCountry; |
||
69 | |||
70 | /** |
||
71 | * Examples: de_DE, pl_PL |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $currentLocale; |
||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $contexts; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected static $defaultStore; |
||
86 | |||
87 | /** |
||
88 | * Cache for data from stores.php to not open it too many times. |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected $stores; |
||
93 | |||
94 | /** |
||
95 | * Examples: EUR, PLN |
||
96 | * |
||
97 | * @link http://en.wikipedia.org/wiki/ISO_4217 |
||
98 | * |
||
99 | * @var string |
||
100 | */ |
||
101 | protected $currencyIsoCode; |
||
102 | |||
103 | /** |
||
104 | * @var array<string> |
||
105 | */ |
||
106 | protected $currencyIsoCodes = []; |
||
107 | |||
108 | /** |
||
109 | * @var array Keys are queue pool names, values are lists of queue connection names. |
||
110 | */ |
||
111 | protected $queuePools = []; |
||
112 | |||
113 | /** |
||
114 | * @var array<string> |
||
115 | */ |
||
116 | protected $storesWithSharedPersistence = []; |
||
117 | |||
118 | /** |
||
119 | * @var bool |
||
120 | */ |
||
121 | protected static $isDynamicStoreMode; |
||
122 | |||
123 | /** |
||
124 | * @return static |
||
125 | */ |
||
126 | public static function getInstance() |
||
127 | { |
||
128 | static::assertDynamicStoreModeDisabled(); |
||
129 | |||
130 | if (!static::$instance) { |
||
131 | static::$instance = new static(); |
||
132 | } |
||
133 | |||
134 | return static::$instance; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return bool |
||
139 | */ |
||
140 | public static function isDynamicStoreMode(): bool |
||
141 | { |
||
142 | if (getenv('SPRYKER_DYNAMIC_STORE_MODE')) { |
||
143 | return (bool)getenv('SPRYKER_DYNAMIC_STORE_MODE'); |
||
144 | } |
||
145 | |||
146 | if (static::$isDynamicStoreMode === null) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
147 | static::$isDynamicStoreMode = !file_exists(APPLICATION_ROOT_DIR . '/config/Shared/stores.php'); |
||
148 | } |
||
149 | |||
150 | return static::$isDynamicStoreMode; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @throws \Exception |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | public static function assertDynamicStoreModeDisabled(): void |
||
159 | { |
||
160 | if (static::isDynamicStoreMode()) { |
||
161 | throw new Exception('Dynamic store mode enabled. Store object cannot be used anymore.'); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | public static function getDefaultStore() |
||
169 | { |
||
170 | static::assertDynamicStoreModeDisabled(); |
||
171 | |||
172 | if (static::$defaultStore === null) { |
||
173 | static::$defaultStore = require APPLICATION_ROOT_DIR . '/config/Shared/default_store.php'; |
||
174 | } |
||
175 | |||
176 | return static::$defaultStore; |
||
177 | } |
||
178 | |||
179 | protected function __construct() |
||
180 | { |
||
181 | static::assertDynamicStoreModeDisabled(); |
||
182 | |||
183 | $currentStoreName = APPLICATION_STORE; |
||
184 | $this->initializeSetup($currentStoreName); |
||
185 | $this->publish(); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return void |
||
190 | */ |
||
191 | protected function publish() |
||
192 | { |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param string $currentStoreName |
||
197 | * |
||
198 | * @throws \Exception |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | protected function getStoreSetup($currentStoreName) |
||
203 | { |
||
204 | $stores = $this->getStores(); |
||
205 | |||
206 | if (array_key_exists($currentStoreName, $stores) === false) { |
||
207 | throw new Exception('Missing setup for store: ' . $currentStoreName); |
||
208 | } |
||
209 | |||
210 | return $stores; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @return array |
||
215 | */ |
||
216 | protected function getStores(): array |
||
217 | { |
||
218 | if ($this->stores === null) { |
||
219 | $this->stores = require APPLICATION_ROOT_DIR . '/config/Shared/stores.php'; |
||
220 | } |
||
221 | |||
222 | return $this->stores; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param string $currentStoreName |
||
227 | * |
||
228 | * @throws \Exception |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | public function initializeSetup($currentStoreName) |
||
233 | { |
||
234 | $stores = $this->getStoreSetup($currentStoreName); |
||
235 | $storeArray = $stores[$currentStoreName]; |
||
236 | $vars = get_object_vars($this); |
||
237 | foreach ($storeArray as $k => $v) { |
||
238 | if (!array_key_exists($k, $vars)) { |
||
239 | throw new Exception('Unknown setup-key: ' . $k); |
||
240 | } |
||
241 | $this->$k = $v; |
||
242 | } |
||
243 | |||
244 | $this->storeName = $currentStoreName; |
||
245 | $this->allStoreNames = array_keys($stores); |
||
246 | $this->allStores = $stores; |
||
247 | |||
248 | $this->setCurrencyIsoCode($this->getDefaultCurrencyCode()); |
||
249 | $this->setCurrentLocale((string)current($this->locales)); |
||
250 | $this->setCurrentCountry((string)current($this->countries)); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * @throws \Spryker\Shared\Kernel\Locale\LocaleNotFoundException |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getCurrentLocale() |
||
259 | { |
||
260 | if ($this->currentLocale === null) { |
||
261 | throw new LocaleNotFoundException('Locale is not defined.'); |
||
262 | } |
||
263 | |||
264 | return $this->currentLocale; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param string $locale string The locale, e.g. 'de_DE' |
||
269 | * |
||
270 | * @throws \InvalidArgumentException |
||
271 | * |
||
272 | * @return string The language, e.g. 'de' |
||
273 | */ |
||
274 | protected function getLanguageFromLocale($locale) |
||
275 | { |
||
276 | $position = strpos($locale, '_'); |
||
277 | if ($position === false) { |
||
278 | throw new InvalidArgumentException(sprintf('Invalid format for locale `%s`, expected `xx_YY`.', $locale)); |
||
279 | } |
||
280 | |||
281 | return substr($locale, 0, $position); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getCurrentLanguage() |
||
288 | { |
||
289 | return $this->getLanguageFromLocale($this->currentLocale); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @return array<string> |
||
294 | */ |
||
295 | public function getAllowedStores() |
||
296 | { |
||
297 | return $this->allStoreNames; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @return array<string> |
||
302 | */ |
||
303 | public function getInactiveStores() |
||
304 | { |
||
305 | $inActiveStores = []; |
||
306 | foreach ($this->getAllowedStores() as $store) { |
||
307 | if ($this->storeName !== $store) { |
||
308 | $inActiveStores[] = $store; |
||
309 | } |
||
310 | } |
||
311 | |||
312 | return $inActiveStores; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @return array<string> |
||
317 | */ |
||
318 | public function getLocales() |
||
319 | { |
||
320 | return $this->locales; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @param string $storeName |
||
325 | * |
||
326 | * @return array<string> |
||
327 | */ |
||
328 | public function getLocalesPerStore($storeName) |
||
329 | { |
||
330 | if (!array_key_exists($storeName, $this->allStores)) { |
||
331 | return []; |
||
332 | } |
||
333 | |||
334 | return $this->allStores[$storeName]['locales']; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return string |
||
339 | */ |
||
340 | public function getStoreName() |
||
341 | { |
||
342 | return $this->storeName; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @param string $storeName |
||
347 | * |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function setStoreName($storeName) |
||
351 | { |
||
352 | $this->storeName = $storeName; |
||
353 | |||
354 | return $this; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @param string $currentLocale |
||
359 | * |
||
360 | * @throws \InvalidArgumentException |
||
361 | * |
||
362 | * @return void |
||
363 | */ |
||
364 | public function setCurrentLocale($currentLocale) |
||
365 | { |
||
366 | if (!in_array($currentLocale, $this->locales)) { |
||
367 | throw new InvalidArgumentException(sprintf('"%s" locale is not a valid value. Please use one of "%s".', $currentLocale, implode('", "', $this->locales))); |
||
368 | } |
||
369 | |||
370 | $this->currentLocale = $currentLocale; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * @return array |
||
375 | */ |
||
376 | public function getContexts() |
||
377 | { |
||
378 | return $this->contexts; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @return array<string> |
||
383 | */ |
||
384 | public function getCountries() |
||
385 | { |
||
386 | return $this->countries; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param string $currentCountry |
||
391 | * |
||
392 | * @return void |
||
393 | */ |
||
394 | public function setCurrentCountry($currentCountry) |
||
395 | { |
||
396 | $this->currentCountry = $currentCountry; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @return string |
||
401 | */ |
||
402 | public function getCurrentCountry() |
||
403 | { |
||
404 | return $this->currentCountry; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * @return string |
||
409 | */ |
||
410 | public function getStorePrefix() |
||
411 | { |
||
412 | $prefix = Config::get(KernelConstants::STORE_PREFIX, ''); |
||
413 | $prefix .= $this->getStoreName(); |
||
414 | |||
415 | return $prefix; |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @param string $currencyIsoCode |
||
420 | * |
||
421 | * @throws \InvalidArgumentException |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | public function setCurrencyIsoCode($currencyIsoCode) |
||
426 | { |
||
427 | if (count($this->currencyIsoCodes) === 0) { |
||
428 | $this->currencyIsoCode = $currencyIsoCode; |
||
429 | |||
430 | return; |
||
431 | } |
||
432 | |||
433 | if (!in_array($currencyIsoCode, $this->currencyIsoCodes)) { |
||
434 | throw new InvalidArgumentException( |
||
435 | sprintf( |
||
436 | '"%s" currency is not a valid value. Please use one of "%s".', |
||
437 | $currencyIsoCode, |
||
438 | implode('", "', $this->currencyIsoCodes), |
||
439 | ), |
||
440 | ); |
||
441 | } |
||
442 | |||
443 | $this->currencyIsoCode = $currencyIsoCode; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @return array<string> |
||
448 | */ |
||
449 | public function getCurrencyIsoCodes() |
||
450 | { |
||
451 | return $this->currencyIsoCodes; |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * @return string |
||
456 | */ |
||
457 | public function getCurrencyIsoCode() |
||
458 | { |
||
459 | return $this->currencyIsoCode; |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * @param string $storeName |
||
464 | * |
||
465 | * @return array |
||
466 | */ |
||
467 | public function getConfigurationForStore($storeName) |
||
468 | { |
||
469 | return $this->getStoreSetup($storeName)[$storeName]; |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * @return string |
||
474 | */ |
||
475 | public function getDefaultCurrencyCode() |
||
476 | { |
||
477 | $defaultCurrencyCode = current($this->currencyIsoCodes); |
||
478 | if (!$defaultCurrencyCode) { |
||
479 | return $this->currencyIsoCode; |
||
480 | } |
||
481 | |||
482 | return $defaultCurrencyCode; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @return array |
||
487 | */ |
||
488 | public function getQueuePools() |
||
489 | { |
||
490 | return $this->queuePools; |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * @return array<string> |
||
495 | */ |
||
496 | public function getStoresWithSharedPersistence() |
||
497 | { |
||
498 | return $this->storesWithSharedPersistence; |
||
499 | } |
||
500 | } |
||
501 |