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
|
|
|
use Longman\LaravelMultiLang\Config; |
16
|
|
|
|
17
|
|
|
class Repository |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The instance of the config. |
22
|
|
|
* |
23
|
|
|
* @var \Longman\LaravelMultiLang\Config |
24
|
|
|
*/ |
25
|
|
|
protected $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The instance of the cache. |
29
|
|
|
* |
30
|
|
|
* @var \Illuminate\Cache\CacheManager |
31
|
|
|
*/ |
32
|
|
|
protected $cache; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The instance of the database. |
36
|
|
|
* |
37
|
|
|
* @var \Illuminate\Database\DatabaseManager |
38
|
|
|
*/ |
39
|
|
|
protected $db; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new MultiLang instance. |
43
|
|
|
* |
44
|
|
|
* @param string $environment |
|
|
|
|
45
|
|
|
* @param array $config |
46
|
|
|
* @param \Illuminate\Cache\CacheManager $cache |
47
|
|
|
* @param \Illuminate\Database\DatabaseManager $db |
48
|
|
|
*/ |
49
|
27 |
|
public function __construct(Config $config, Cache $cache, Database $db) |
50
|
|
|
{ |
51
|
27 |
|
$this->config = $config; |
52
|
27 |
|
$this->cache = $cache; |
53
|
27 |
|
$this->db = $db; |
54
|
27 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get cache key name based on lang and scope |
58
|
|
|
* |
59
|
|
|
* @param string $lang |
60
|
|
|
* @param string $scope |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
5 |
|
public function getCacheName($lang, $scope = null) |
64
|
|
|
{ |
65
|
5 |
|
$key = $this->config->get('db.texts_table', 'texts') . '_' . $lang; |
66
|
5 |
|
if (!is_null($scope)) { |
67
|
|
|
$key .= '_' . $scope; |
68
|
|
|
} |
69
|
5 |
|
return $key; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Load texts from database storage |
74
|
|
|
* |
75
|
|
|
* @param string $lang |
76
|
|
|
* @param string $scope |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
18 |
|
public function loadFromDatabase($lang, $scope = null) |
80
|
|
|
{ |
81
|
18 |
|
$query = $this->getDb()->table($this->getTableName()) |
82
|
18 |
|
->where('lang', $lang); |
83
|
|
|
|
84
|
18 |
|
if (!is_null($scope)) { |
85
|
|
|
$query = $query->whereNested(function ($query) use ($scope) { |
86
|
|
|
$query->where('scope', 'global'); |
87
|
|
|
$query->orWhere('scope', $scope); |
88
|
|
|
}); |
89
|
|
|
} else { |
90
|
18 |
|
$query = $query->where('scope', 'global'); |
91
|
|
|
} |
92
|
|
|
|
93
|
18 |
|
$texts = $query->get(['key', 'value', 'lang', 'scope']); |
94
|
|
|
|
95
|
18 |
|
$array = []; |
96
|
18 |
|
foreach ($texts as $row) { |
97
|
12 |
|
$array[$row->key] = $row->value; |
98
|
|
|
} |
99
|
18 |
|
return $array; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Load texts from cache storage |
104
|
|
|
* |
105
|
|
|
* @param string $lang |
106
|
|
|
* @param string $scope |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
2 |
|
public function loadFromCache($lang, $scope = null) |
110
|
|
|
{ |
111
|
2 |
|
$texts = $this->getCache()->get($this->getCacheName($lang, $scope)); |
112
|
|
|
|
113
|
2 |
|
return $texts; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Store texts in cache |
118
|
|
|
* |
119
|
|
|
* @param string $lang |
120
|
|
|
* @param array $texts |
121
|
|
|
* @param string $scope |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
4 |
|
public function storeInCache($lang, array $texts, $scope = null) |
125
|
|
|
{ |
126
|
4 |
|
$this->getCache()->put($this->getCacheName($lang, $scope), $texts, $this->config->get('cache.lifetime', 1440)); |
127
|
4 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Check if we must load texts from cache |
132
|
|
|
* |
133
|
|
|
* @param string $lang |
134
|
|
|
* @param string $scope |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
4 |
|
public function existsInCache($lang, $scope = null) |
138
|
|
|
{ |
139
|
4 |
|
return $this->getCache()->has($this->getCacheName($lang, $scope)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get a database connection instance. |
144
|
|
|
* |
145
|
|
|
* @return \Illuminate\Database\Connection |
146
|
|
|
*/ |
147
|
18 |
|
protected function getDb() |
148
|
|
|
{ |
149
|
18 |
|
$connection = $this->config->get('db.connection'); |
150
|
18 |
|
if ($connection == 'default') { |
151
|
18 |
|
return $this->db->connection(); |
152
|
|
|
} |
153
|
|
|
return $this->db->connection($connection); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get a cache driver instance. |
158
|
|
|
* |
159
|
|
|
* @return \Illuminate\Contracts\Cache\Repository |
160
|
|
|
*/ |
161
|
4 |
|
protected function getCache() |
162
|
|
|
{ |
163
|
4 |
|
$store = $this->config->get('cache.store', 'default'); |
164
|
4 |
|
if ($store == 'default') { |
165
|
|
|
return $this->cache->store(); |
166
|
|
|
} |
167
|
4 |
|
return $this->cache->store($store); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Save missing texts in database |
172
|
|
|
* |
173
|
|
|
* @param string $texts |
174
|
|
|
* @param string $scope |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
4 |
|
public function save($texts, $scope = null) |
178
|
|
|
{ |
179
|
4 |
|
if (empty($texts)) { |
180
|
1 |
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
$table = $this->getTableName(); |
184
|
4 |
|
$locales = $this->config->get('locales', []); |
185
|
4 |
|
if (!$scope) { |
|
|
|
|
186
|
4 |
|
$scope = 'global'; |
187
|
|
|
} |
188
|
|
|
|
189
|
4 |
|
foreach ($texts as $k => $v) { |
|
|
|
|
190
|
4 |
|
foreach ($locales as $lang => $locale_data) { |
191
|
4 |
|
$exists = $this->getDb() |
192
|
4 |
|
->table($table) |
193
|
4 |
|
->where([ |
194
|
4 |
|
'key' => $k, |
195
|
4 |
|
'lang' => $lang, |
196
|
4 |
|
'scope' => $scope, |
197
|
4 |
|
])->first(); |
198
|
|
|
|
199
|
4 |
|
if ($exists) { |
200
|
1 |
|
continue; |
201
|
|
|
} |
202
|
|
|
|
203
|
4 |
|
$this->getDb() |
204
|
4 |
|
->table($table) |
205
|
4 |
|
->insert([ |
206
|
4 |
|
'key' => $k, |
207
|
4 |
|
'lang' => $lang, |
208
|
4 |
|
'scope' => $scope, |
209
|
4 |
|
'value' => $v, |
210
|
|
|
]); |
211
|
|
|
} |
212
|
|
|
} |
213
|
4 |
|
return true; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get texts table name |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
19 |
|
public function getTableName() |
222
|
|
|
{ |
223
|
19 |
|
$table = $this->config->get('db.texts_table', 'texts'); |
224
|
19 |
|
return $table; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.