1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel MultiLang package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\LaravelMultiLang; |
12
|
|
|
|
13
|
|
|
use Illuminate\Cache\CacheManager as Cache; |
14
|
|
|
use Illuminate\Database\DatabaseManager as Database; |
15
|
|
|
|
16
|
|
|
class Repository |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The instance of the config. |
21
|
|
|
* |
22
|
|
|
* @var \Longman\LaravelMultiLang\Config |
23
|
|
|
*/ |
24
|
|
|
protected $config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The instance of the cache. |
28
|
|
|
* |
29
|
|
|
* @var \Illuminate\Cache\CacheManager |
30
|
|
|
*/ |
31
|
|
|
protected $cache; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The instance of the database. |
35
|
|
|
* |
36
|
|
|
* @var \Illuminate\Database\DatabaseManager |
37
|
|
|
*/ |
38
|
|
|
protected $db; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new MultiLang instance. |
42
|
|
|
* |
43
|
|
|
* @param \Longman\LaravelMultiLang\Config $config |
44
|
|
|
* @param \Illuminate\Cache\CacheManager $cache |
45
|
|
|
* @param \Illuminate\Database\DatabaseManager $db |
46
|
|
|
*/ |
47
|
31 |
|
public function __construct(Config $config, Cache $cache, Database $db) |
48
|
|
|
{ |
49
|
31 |
|
$this->config = $config; |
50
|
31 |
|
$this->cache = $cache; |
51
|
31 |
|
$this->db = $db; |
52
|
31 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get cache key name based on lang and scope |
56
|
|
|
* |
57
|
|
|
* @param string $lang |
58
|
|
|
* @param string $scope |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
6 |
|
public function getCacheName($lang, $scope = null) |
62
|
|
|
{ |
63
|
6 |
|
$key = $this->config->get('db.texts_table', 'texts') . '_' . $lang; |
64
|
6 |
|
if (! is_null($scope)) { |
65
|
4 |
|
$key .= '_' . $scope; |
66
|
4 |
|
} |
67
|
|
|
|
68
|
6 |
|
return $key; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Load texts from database storage |
73
|
|
|
* |
74
|
|
|
* @param string $lang |
75
|
|
|
* @param string $scope |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
21 |
|
public function loadFromDatabase($lang, $scope = null) |
79
|
|
|
{ |
80
|
21 |
|
$query = $this->getDb()->table($this->getTableName()) |
81
|
21 |
|
->where('lang', $lang); |
82
|
|
|
|
83
|
21 |
|
if (! is_null($scope)) { |
84
|
|
|
$query = $query->whereNested(function ($query) use ($scope) { |
85
|
21 |
|
$query->where('scope', 'global'); |
86
|
21 |
|
$query->orWhere('scope', $scope); |
87
|
21 |
|
}); |
88
|
21 |
|
} else { |
89
|
1 |
|
$query = $query->where('scope', 'global'); |
90
|
|
|
} |
91
|
|
|
|
92
|
21 |
|
$texts = $query->get(['key', 'value', 'lang', 'scope']); |
93
|
|
|
|
94
|
21 |
|
$array = []; |
95
|
21 |
|
foreach ($texts as $row) { |
96
|
14 |
|
$array[$row->key] = $row->value; |
97
|
21 |
|
} |
98
|
|
|
|
99
|
21 |
|
return $array; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Load all texts from database storage |
104
|
|
|
* |
105
|
|
|
* @param string $lang |
106
|
|
|
* @param string $scope |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function loadAllFromDatabase($lang = null, $scope = null) |
110
|
|
|
{ |
111
|
|
|
$query = $this->getDb()->table($this->getTableName()); |
112
|
|
|
|
113
|
|
|
if (! is_null($lang)) { |
114
|
|
|
$query = $query->where('lang', $lang); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (! is_null($scope)) { |
118
|
|
|
$query = $query->whereNested(function ($query) use ($scope) { |
119
|
|
|
$query->where('scope', 'global'); |
120
|
|
|
$query->orWhere('scope', $scope); |
121
|
|
|
}); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$texts = $query->get(); |
125
|
|
|
|
126
|
|
|
$array = []; |
127
|
|
|
foreach ($texts as $row) { |
128
|
|
|
$array[$row->lang][$row->key] = $row; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $array; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Load texts from cache storage |
136
|
|
|
* |
137
|
|
|
* @param string $lang |
138
|
|
|
* @param string $scope |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
2 |
|
public function loadFromCache($lang, $scope = null) |
142
|
|
|
{ |
143
|
2 |
|
$texts = $this->getCache()->get($this->getCacheName($lang, $scope)); |
144
|
|
|
|
145
|
2 |
|
return $texts; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Store texts in cache |
150
|
|
|
* |
151
|
|
|
* @param string $lang |
152
|
|
|
* @param array $texts |
153
|
|
|
* @param string $scope |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
4 |
|
public function storeInCache($lang, array $texts, $scope = null) |
157
|
|
|
{ |
158
|
4 |
|
$this->getCache()->put($this->getCacheName($lang, $scope), $texts, $this->config->get('cache.lifetime', 1440)); |
159
|
|
|
|
160
|
4 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Check if we must load texts from cache |
165
|
|
|
* |
166
|
|
|
* @param string $lang |
167
|
|
|
* @param string $scope |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
4 |
|
public function existsInCache($lang, $scope = null) |
171
|
|
|
{ |
172
|
4 |
|
return $this->getCache()->has($this->getCacheName($lang, $scope)); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Get a database connection instance. |
177
|
|
|
* |
178
|
|
|
* @return \Illuminate\Database\Connection |
179
|
|
|
*/ |
180
|
21 |
|
protected function getDb() |
181
|
|
|
{ |
182
|
21 |
|
$connection = $this->config->get('db.connection'); |
183
|
21 |
|
if ($connection == 'default') { |
184
|
21 |
|
return $this->db->connection(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $this->db->connection($connection); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get a cache driver instance. |
192
|
|
|
* |
193
|
|
|
* @return \Illuminate\Contracts\Cache\Repository |
194
|
|
|
*/ |
195
|
4 |
|
protected function getCache() |
196
|
|
|
{ |
197
|
4 |
|
$store = $this->config->get('cache.store', 'default'); |
198
|
4 |
|
if ($store == 'default') { |
199
|
|
|
return $this->cache->store(); |
200
|
|
|
} |
201
|
|
|
|
202
|
4 |
|
return $this->cache->store($store); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Save missing texts in database |
207
|
|
|
* |
208
|
|
|
* @param array $texts |
209
|
|
|
* @param string $scope |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
4 |
|
public function save(array $texts, $scope = null) |
213
|
|
|
{ |
214
|
4 |
|
if (empty($texts)) { |
215
|
1 |
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
4 |
|
$table = $this->getTableName(); |
219
|
4 |
|
$locales = $this->config->get('locales', []); |
220
|
4 |
|
if (is_null($scope)) { |
221
|
1 |
|
$scope = 'global'; |
222
|
1 |
|
} |
223
|
|
|
|
224
|
4 |
|
foreach ($texts as $k => $v) { |
225
|
4 |
|
foreach ($locales as $lang => $locale_data) { |
226
|
4 |
|
$exists = $this->getDb() |
227
|
4 |
|
->table($table) |
228
|
4 |
|
->where([ |
229
|
4 |
|
'key' => $k, |
230
|
4 |
|
'lang' => $lang, |
231
|
4 |
|
'scope' => $scope, |
232
|
4 |
|
])->first(); |
233
|
|
|
|
234
|
4 |
|
if ($exists) { |
235
|
1 |
|
continue; |
236
|
|
|
} |
237
|
|
|
|
238
|
4 |
|
$this->getDb() |
239
|
4 |
|
->table($table) |
240
|
4 |
|
->insert([ |
241
|
4 |
|
'key' => $k, |
242
|
4 |
|
'lang' => $lang, |
243
|
4 |
|
'scope' => $scope, |
244
|
4 |
|
'value' => $v, |
245
|
4 |
|
]); |
246
|
4 |
|
} |
247
|
4 |
|
} |
248
|
|
|
|
249
|
4 |
|
return true; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get texts table name |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
22 |
|
public function getTableName() |
258
|
|
|
{ |
259
|
22 |
|
$table = $this->config->get('db.texts_table', 'texts'); |
260
|
|
|
|
261
|
22 |
|
return $table; |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|