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
|
29 |
|
public function __construct(Config $config, Cache $cache, Database $db) |
48
|
|
|
{ |
49
|
29 |
|
$this->config = $config; |
50
|
29 |
|
$this->cache = $cache; |
51
|
29 |
|
$this->db = $db; |
52
|
29 |
|
} |
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
|
1 |
|
$key .= '_' . $scope; |
66
|
|
|
} |
67
|
6 |
|
return $key; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Load texts from database storage |
72
|
|
|
* |
73
|
|
|
* @param string $lang |
74
|
|
|
* @param string $scope |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
19 |
|
public function loadFromDatabase($lang, $scope = null) |
78
|
|
|
{ |
79
|
19 |
|
$query = $this->getDb()->table($this->getTableName()) |
80
|
19 |
|
->where('lang', $lang); |
81
|
|
|
|
82
|
19 |
|
if (!is_null($scope)) { |
83
|
|
|
$query = $query->whereNested(function ($query) use ($scope) { |
84
|
1 |
|
$query->where('scope', 'global'); |
85
|
1 |
|
$query->orWhere('scope', $scope); |
86
|
1 |
|
}); |
87
|
|
|
} else { |
88
|
19 |
|
$query = $query->where('scope', 'global'); |
89
|
|
|
} |
90
|
|
|
|
91
|
19 |
|
$texts = $query->get(['key', 'value', 'lang', 'scope']); |
92
|
|
|
|
93
|
19 |
|
$array = []; |
94
|
19 |
|
foreach ($texts as $row) { |
95
|
12 |
|
$array[$row->key] = $row->value; |
96
|
|
|
} |
97
|
19 |
|
return $array; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Load all texts from database storage |
102
|
|
|
* |
103
|
|
|
* @param string $lang |
104
|
|
|
* @param string $scope |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function loadAllFromDatabase($lang = null, $scope = null) |
108
|
|
|
{ |
109
|
|
|
$query = $this->getDb()->table($this->getTableName()); |
110
|
|
|
|
111
|
|
|
if (!is_null($lang)) { |
112
|
|
|
$query = $query->where('lang', $lang); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!is_null($scope)) { |
116
|
|
|
$query = $query->whereNested(function ($query) use ($scope) { |
117
|
|
|
$query->where('scope', 'global'); |
118
|
|
|
$query->orWhere('scope', $scope); |
119
|
|
|
}); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$texts = $query->get(); |
123
|
|
|
|
124
|
|
|
$array = []; |
125
|
|
|
foreach ($texts as $row) { |
126
|
|
|
$array[$row->lang][$row->key] = $row; |
127
|
|
|
} |
128
|
|
|
return $array; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Load texts from cache storage |
133
|
|
|
* |
134
|
|
|
* @param string $lang |
135
|
|
|
* @param string $scope |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
2 |
|
public function loadFromCache($lang, $scope = null) |
139
|
|
|
{ |
140
|
2 |
|
$texts = $this->getCache()->get($this->getCacheName($lang, $scope)); |
141
|
|
|
|
142
|
2 |
|
return $texts; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Store texts in cache |
147
|
|
|
* |
148
|
|
|
* @param string $lang |
149
|
|
|
* @param array $texts |
150
|
|
|
* @param string $scope |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
4 |
|
public function storeInCache($lang, array $texts, $scope = null) |
154
|
|
|
{ |
155
|
4 |
|
$this->getCache()->put($this->getCacheName($lang, $scope), $texts, $this->config->get('cache.lifetime', 1440)); |
156
|
4 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Check if we must load texts from cache |
161
|
|
|
* |
162
|
|
|
* @param string $lang |
163
|
|
|
* @param string $scope |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
4 |
|
public function existsInCache($lang, $scope = null) |
167
|
|
|
{ |
168
|
4 |
|
return $this->getCache()->has($this->getCacheName($lang, $scope)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get a database connection instance. |
173
|
|
|
* |
174
|
|
|
* @return \Illuminate\Database\Connection |
175
|
|
|
*/ |
176
|
19 |
|
protected function getDb() |
177
|
|
|
{ |
178
|
19 |
|
$connection = $this->config->get('db.connection'); |
179
|
19 |
|
if ($connection == 'default') { |
180
|
19 |
|
return $this->db->connection(); |
181
|
|
|
} |
182
|
|
|
return $this->db->connection($connection); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get a cache driver instance. |
187
|
|
|
* |
188
|
|
|
* @return \Illuminate\Contracts\Cache\Repository |
189
|
|
|
*/ |
190
|
4 |
|
protected function getCache() |
191
|
|
|
{ |
192
|
4 |
|
$store = $this->config->get('cache.store', 'default'); |
193
|
4 |
|
if ($store == 'default') { |
194
|
|
|
return $this->cache->store(); |
195
|
|
|
} |
196
|
4 |
|
return $this->cache->store($store); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Save missing texts in database |
201
|
|
|
* |
202
|
|
|
* @param array $texts |
203
|
|
|
* @param string $scope |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
4 |
|
public function save(array $texts, $scope = null) |
207
|
|
|
{ |
208
|
4 |
|
if (empty($texts)) { |
209
|
1 |
|
return false; |
210
|
|
|
} |
211
|
|
|
|
212
|
4 |
|
$table = $this->getTableName(); |
213
|
4 |
|
$locales = $this->config->get('locales', []); |
214
|
4 |
|
if (is_null($scope)) { |
215
|
4 |
|
$scope = 'global'; |
216
|
|
|
} |
217
|
|
|
|
218
|
4 |
|
foreach ($texts as $k => $v) { |
219
|
4 |
|
foreach ($locales as $lang => $locale_data) { |
220
|
4 |
|
$exists = $this->getDb() |
221
|
4 |
|
->table($table) |
222
|
4 |
|
->where([ |
223
|
4 |
|
'key' => $k, |
224
|
4 |
|
'lang' => $lang, |
225
|
4 |
|
'scope' => $scope, |
226
|
4 |
|
])->first(); |
227
|
|
|
|
228
|
4 |
|
if ($exists) { |
229
|
1 |
|
continue; |
230
|
|
|
} |
231
|
|
|
|
232
|
4 |
|
$this->getDb() |
233
|
4 |
|
->table($table) |
234
|
4 |
|
->insert([ |
235
|
4 |
|
'key' => $k, |
236
|
4 |
|
'lang' => $lang, |
237
|
4 |
|
'scope' => $scope, |
238
|
4 |
|
'value' => $v, |
239
|
|
|
]); |
240
|
|
|
} |
241
|
|
|
} |
242
|
4 |
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get texts table name |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
20 |
|
public function getTableName() |
251
|
|
|
{ |
252
|
20 |
|
$table = $this->config->get('db.texts_table', 'texts'); |
253
|
20 |
|
return $table; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|