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 | 28 | 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 | 5 | public function getCacheName($lang, $scope = null) |
|
62 | { |
||
63 | 5 | $key = $this->config->get('db.texts_table', 'texts') . '_' . $lang; |
|
64 | 5 | if (!is_null($scope)) { |
|
65 | $key .= '_' . $scope; |
||
66 | } |
||
67 | 5 | 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 | $query->where('scope', 'global'); |
||
85 | $query->orWhere('scope', $scope); |
||
86 | }); |
||
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 | 19 | } |
|
97 | 19 | return $array; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Load texts from cache storage |
||
102 | * |
||
103 | * @param string $lang |
||
104 | * @param string $scope |
||
105 | * @return mixed |
||
106 | */ |
||
107 | 2 | public function loadFromCache($lang, $scope = null) |
|
113 | |||
114 | /** |
||
115 | * Store texts in cache |
||
116 | * |
||
117 | * @param string $lang |
||
118 | * @param array $texts |
||
119 | * @param string $scope |
||
120 | * @return $this |
||
121 | */ |
||
122 | 4 | public function storeInCache($lang, array $texts, $scope = null) |
|
127 | |||
128 | /** |
||
129 | * Check if we must load texts from cache |
||
130 | * |
||
131 | * @param string $lang |
||
132 | * @param string $scope |
||
133 | * @return bool |
||
134 | */ |
||
135 | 4 | public function existsInCache($lang, $scope = null) |
|
139 | |||
140 | /** |
||
141 | * Get a database connection instance. |
||
142 | * |
||
143 | * @return \Illuminate\Database\Connection |
||
144 | */ |
||
145 | 19 | protected function getDb() |
|
153 | |||
154 | /** |
||
155 | * Get a cache driver instance. |
||
156 | * |
||
157 | * @return \Illuminate\Contracts\Cache\Repository |
||
158 | */ |
||
159 | 4 | protected function getCache() |
|
167 | |||
168 | /** |
||
169 | * Save missing texts in database |
||
170 | * |
||
171 | * @param array $texts |
||
172 | * @param string $scope |
||
173 | * @return bool |
||
174 | */ |
||
175 | 4 | public function save(array $texts, $scope = null) |
|
176 | { |
||
177 | 4 | if (empty($texts)) { |
|
178 | 1 | return false; |
|
179 | } |
||
180 | |||
181 | 4 | $table = $this->getTableName(); |
|
182 | 4 | $locales = $this->config->get('locales', []); |
|
183 | 4 | if (is_null($scope)) { |
|
184 | 4 | $scope = 'global'; |
|
185 | 4 | } |
|
186 | |||
187 | 4 | foreach ($texts as $k => $v) { |
|
188 | 4 | foreach ($locales as $lang => $locale_data) { |
|
189 | 4 | $exists = $this->getDb() |
|
190 | 4 | ->table($table) |
|
191 | 4 | ->where([ |
|
192 | 4 | 'key' => $k, |
|
193 | 4 | 'lang' => $lang, |
|
194 | 4 | 'scope' => $scope, |
|
195 | 4 | ])->first(); |
|
196 | |||
197 | 4 | if ($exists) { |
|
198 | 1 | continue; |
|
199 | } |
||
200 | |||
201 | 4 | $this->getDb() |
|
202 | 4 | ->table($table) |
|
203 | 4 | ->insert([ |
|
204 | 4 | 'key' => $k, |
|
205 | 4 | 'lang' => $lang, |
|
206 | 4 | 'scope' => $scope, |
|
207 | 4 | 'value' => $v, |
|
208 | 4 | ]); |
|
209 | 4 | } |
|
210 | 4 | } |
|
211 | 4 | return true; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get texts table name |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 20 | public function getTableName() |
|
224 | } |
||
225 |