1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ConfigCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 Yahoo Japan Corporation |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace YahooJapan\ConfigCacheBundle\ConfigCache\Locale; |
13
|
|
|
|
14
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\ConfigCache as BaseConfigCache; |
15
|
|
|
use YahooJapan\ConfigCacheBundle\ConfigCache\Locale\Loader\TranslationLoaderInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ConfigCache manages user configuration files with locale. |
19
|
|
|
*/ |
20
|
|
|
class ConfigCache extends BaseConfigCache implements ConfigCacheInterface |
21
|
|
|
{ |
22
|
|
|
const TAG_LOCALE = 'config.locale'; |
23
|
|
|
|
24
|
|
|
// for createing cache |
25
|
|
|
protected $referableLocales = array(); |
26
|
|
|
// for getting cache |
27
|
|
|
protected $defaultLocale; |
28
|
|
|
protected $currentLocale; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
14 |
|
public function setReferableLocales(array $locales) |
34
|
|
|
{ |
35
|
14 |
|
$this->referableLocales = $locales; |
36
|
|
|
|
37
|
14 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
14 |
|
public function setDefaultLocale($locale) |
44
|
|
|
{ |
45
|
14 |
|
$this->defaultLocale = $locale; |
46
|
|
|
|
47
|
14 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
4 |
|
public function setCurrentLocale($currentLocale) |
54
|
|
|
{ |
55
|
4 |
|
$this->currentLocale = $currentLocale; |
56
|
|
|
|
57
|
4 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
6 |
|
public function create() |
64
|
|
|
{ |
65
|
6 |
|
if (!($this->loader instanceof TranslationLoaderInterface)) { |
66
|
1 |
|
throw new \Exception('Loader must be a translation loader interface'); |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
foreach ($this->referableLocales as $locale) { |
70
|
4 |
|
if (!$this->cache->contains($this->findId($locale))) { |
71
|
4 |
|
$this->loader->setLocale($locale); |
72
|
4 |
|
$this->createInternal($locale); |
73
|
4 |
|
} |
74
|
5 |
|
} |
75
|
5 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
2 |
|
public function save() |
81
|
|
|
{ |
82
|
2 |
|
foreach ($this->referableLocales as $locale) { |
83
|
2 |
|
$this->findRestorableCache()->saveToTemp($this->findId($locale)); |
84
|
2 |
|
} |
85
|
2 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function restore() |
91
|
|
|
{ |
92
|
1 |
|
foreach ($this->referableLocales as $locale) { |
93
|
1 |
|
$this->findRestorableCache()->restore($this->findId($locale)); |
94
|
1 |
|
} |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Creates PHP cache file internal processing. |
99
|
|
|
* |
100
|
|
|
* this method has a $locale argument for executing findAll() but no cache. |
101
|
|
|
* |
102
|
|
|
* @param string $locale |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
7 |
|
protected function createInternal($locale = null) |
107
|
|
|
{ |
108
|
7 |
|
$data = $this->load(); |
109
|
7 |
|
$this->cache->save($this->findId($locale), $data); |
110
|
|
|
|
111
|
6 |
|
return $data; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Finds a doctrine cache ID with locale. |
116
|
|
|
* |
117
|
|
|
* $locale argument has the potential to become null when findAll(). |
118
|
|
|
* |
119
|
|
|
* @param string $locale |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
10 |
|
protected function findId($locale = null) |
124
|
|
|
{ |
125
|
10 |
|
if (is_null($locale)) { |
126
|
4 |
|
$locale = $this->getLocale(); |
127
|
|
|
|
128
|
|
|
// no chance of this block |
129
|
|
|
// because of having getLocale() return value (always configured on setDefaultLocale()). |
130
|
4 |
|
if (is_null($locale)) { |
131
|
2 |
|
throw new \Exception('Locale must be set.'); |
132
|
|
|
} |
133
|
2 |
|
} |
134
|
|
|
|
135
|
8 |
|
return parent::findId().".{$locale}"; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Gets a locale. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
5 |
|
protected function getLocale() |
144
|
|
|
{ |
145
|
5 |
|
return $this->currentLocale ?: $this->defaultLocale; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|