1 | <?php |
||
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 | 30 | public function __construct(Config $config, Cache $cache, Database $db) |
|
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 | 20 | public function loadFromDatabase($lang, $scope = null) |
|
78 | { |
||
79 | 20 | $query = $this->getDb()->table($this->getTableName()) |
|
80 | 20 | ->where('lang', $lang); |
|
81 | |||
82 | 20 | 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 | 20 | $query = $query->where('scope', 'global'); |
|
89 | } |
||
90 | |||
91 | 20 | $texts = $query->get(['key', 'value', 'lang', 'scope']); |
|
92 | |||
93 | 20 | $array = []; |
|
94 | 20 | foreach ($texts as $row) { |
|
95 | 13 | $array[$row->key] = $row->value; |
|
96 | } |
||
97 | 20 | 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) |
|
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) |
|
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) |
|
170 | |||
171 | /** |
||
172 | * Get a database connection instance. |
||
173 | * |
||
174 | * @return \Illuminate\Database\Connection |
||
175 | */ |
||
176 | 20 | protected function getDb() |
|
184 | |||
185 | /** |
||
186 | * Get a cache driver instance. |
||
187 | * |
||
188 | * @return \Illuminate\Contracts\Cache\Repository |
||
189 | */ |
||
190 | 4 | protected function getCache() |
|
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 | 21 | public function getTableName() |
|
255 | } |
||
256 |