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_cache.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(sprintf( |
67
|
1 |
|
'%s: Loader [%s] must be a translation loader interface', |
68
|
1 |
|
__CLASS__, |
69
|
1 |
|
get_class($this->loader) |
70
|
1 |
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
foreach ($this->referableLocales as $locale) { |
74
|
4 |
|
if (!$this->cache->contains($this->findId($locale))) { |
75
|
4 |
|
$this->loader->setLocale($locale); |
76
|
4 |
|
$this->createInternal($locale); |
77
|
4 |
|
} |
78
|
5 |
|
} |
79
|
5 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
2 |
|
public function save() |
85
|
|
|
{ |
86
|
2 |
|
foreach ($this->referableLocales as $locale) { |
87
|
2 |
|
$this->findRestorableCache()->saveToTemp($this->findId($locale)); |
88
|
2 |
|
} |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function restore() |
95
|
|
|
{ |
96
|
1 |
|
foreach ($this->referableLocales as $locale) { |
97
|
1 |
|
$this->findRestorableCache()->restore($this->findId($locale)); |
98
|
1 |
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Creates PHP cache file internal processing. |
103
|
|
|
* |
104
|
|
|
* this method has a $locale argument for executing findAll() but no cache. |
105
|
|
|
* |
106
|
|
|
* @param string $locale |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
7 |
|
protected function createInternal($locale = null) |
111
|
|
|
{ |
112
|
7 |
|
$data = $this->load(); |
113
|
7 |
|
$this->cache->save($this->findId($locale), $data); |
114
|
|
|
|
115
|
6 |
|
return $data; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Finds a doctrine cache ID with locale. |
120
|
|
|
* |
121
|
|
|
* $locale argument has the potential to become null when findAll(). |
122
|
|
|
* |
123
|
|
|
* @param string $locale |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
10 |
|
protected function findId($locale = null) |
128
|
|
|
{ |
129
|
10 |
|
if (is_null($locale)) { |
130
|
4 |
|
$locale = $this->getLocale(); |
131
|
|
|
|
132
|
|
|
// no chance of this block |
133
|
|
|
// because of having getLocale() return value (always configured on setDefaultLocale()). |
134
|
4 |
|
if (is_null($locale)) { |
135
|
2 |
|
throw new \Exception('Locale must be set.'); |
136
|
|
|
} |
137
|
2 |
|
} |
138
|
|
|
|
139
|
8 |
|
return parent::findId().".{$locale}"; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Gets a locale. |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
5 |
|
protected function getLocale() |
148
|
|
|
{ |
149
|
5 |
|
return $this->currentLocale ?: $this->defaultLocale; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|