1 | <?php |
||
9 | class Loader |
||
10 | { |
||
11 | const CACHE_TEMPLATE = 'foo-translate-%s'; |
||
12 | |||
13 | const LIFETIME = 1000000; |
||
14 | |||
15 | /** @var string */ |
||
16 | protected $directory; |
||
17 | |||
18 | /** @var ICacheBridge */ |
||
19 | protected $cacheBridge; |
||
20 | |||
21 | /** |
||
22 | * Translator constructor. |
||
23 | * |
||
24 | * @param string $directory |
||
25 | * @param ICacheBridge|null $cacheBridge |
||
26 | */ |
||
27 | 6 | public function __construct(string $directory, ICacheBridge $cacheBridge = null) |
|
32 | |||
33 | /** |
||
34 | * @param string $lang |
||
35 | * |
||
36 | * @return array |
||
37 | */ |
||
38 | 4 | public function loadTranslations(string $lang): array |
|
39 | { |
||
40 | 4 | $cacheKey = $this->getCacheKey($lang); |
|
41 | |||
42 | 4 | if (null !== $this->cacheBridge && $this->cacheBridge->has($cacheKey)) { |
|
43 | 1 | return $this->cacheBridge->get($cacheKey); |
|
44 | } |
||
45 | |||
46 | 4 | $translations = $this->loadTranslationsFromFiles($lang); |
|
47 | |||
48 | 4 | if (null != $this->cacheBridge) { |
|
49 | 2 | $this->cacheBridge->set($cacheKey, $translations, static::LIFETIME); |
|
50 | } |
||
51 | |||
52 | 4 | return $translations; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $lang |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 4 | protected function getCacheKey(string $lang): string |
|
64 | |||
65 | /** |
||
66 | * @param string $lang |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | 4 | protected function loadTranslationsFromFiles(string $lang): array |
|
93 | } |
||
94 |