1 | <?php |
||
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 \Longman\LaravelMultiLang\Config $config |
||
45 | * @param \Illuminate\Cache\CacheManager $cache |
||
46 | * @param \Illuminate\Database\DatabaseManager $db |
||
47 | */ |
||
48 | 38 | public function __construct(Config $config, Cache $cache, Database $db) |
|
54 | |||
55 | /** |
||
56 | * Get cache key name based on lang and scope |
||
57 | * |
||
58 | * @param string $lang |
||
59 | * @param string $scope |
||
60 | * @return string |
||
61 | */ |
||
62 | 7 | public function getCacheName($lang, $scope = null) |
|
71 | |||
72 | /** |
||
73 | * Load texts from database storage |
||
74 | * |
||
75 | * @param string $lang |
||
76 | * @param string $scope |
||
77 | * @return array |
||
78 | */ |
||
79 | 24 | public function loadFromDatabase($lang, $scope = null) |
|
102 | |||
103 | /** |
||
104 | * Load all texts from database storage |
||
105 | * |
||
106 | * @param string $lang |
||
107 | * @param string $scope |
||
108 | * @return array |
||
109 | */ |
||
110 | 1 | public function loadAllFromDatabase($lang = null, $scope = null) |
|
111 | { |
||
112 | 1 | $query = $this->getDb()->table($this->getTableName()); |
|
113 | |||
114 | 1 | if (! is_null($lang)) { |
|
115 | 1 | $query = $query->where('lang', $lang); |
|
116 | } |
||
117 | |||
118 | 1 | if (! is_null($scope)) { |
|
119 | 1 | $query = $query->whereNested(function ($query) use ($scope) { |
|
120 | 1 | $query->where('scope', 'global'); |
|
121 | 1 | $query->orWhere('scope', $scope); |
|
122 | 1 | }); |
|
123 | } |
||
124 | |||
125 | 1 | $texts = $query->get(); |
|
126 | |||
127 | 1 | $array = []; |
|
128 | 1 | foreach ($texts as $row) { |
|
129 | 1 | $array[$row->lang][$row->key] = $row; |
|
130 | } |
||
131 | |||
132 | 1 | return $array; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Load texts from cache storage |
||
137 | * |
||
138 | * @param string $lang |
||
139 | * @param string $scope |
||
140 | * @return mixed |
||
141 | */ |
||
142 | 3 | public function loadFromCache($lang, $scope = null) |
|
148 | |||
149 | /** |
||
150 | * Store texts in cache |
||
151 | * |
||
152 | * @param string $lang |
||
153 | * @param array $texts |
||
154 | * @param string $scope |
||
155 | * @return $this |
||
156 | */ |
||
157 | 5 | public function storeInCache($lang, array $texts, $scope = null) |
|
163 | |||
164 | /** |
||
165 | * Check if we must load texts from cache |
||
166 | * |
||
167 | * @param string $lang |
||
168 | * @param string $scope |
||
169 | * @return bool |
||
170 | */ |
||
171 | 5 | public function existsInCache($lang, $scope = null) |
|
175 | |||
176 | /** |
||
177 | * Get a database connection instance. |
||
178 | * |
||
179 | * @return \Illuminate\Database\Connection |
||
180 | */ |
||
181 | 25 | protected function getDb() |
|
190 | |||
191 | /** |
||
192 | * Get a cache driver instance. |
||
193 | * |
||
194 | * @return \Illuminate\Contracts\Cache\Repository |
||
195 | */ |
||
196 | 5 | protected function getCache() |
|
205 | |||
206 | /** |
||
207 | * Save missing texts in database |
||
208 | * |
||
209 | * @param array $texts |
||
210 | * @param string $scope |
||
211 | * @return bool |
||
212 | */ |
||
213 | 4 | public function save(array $texts, $scope = null) |
|
255 | |||
256 | /** |
||
257 | * Get texts table name |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | 26 | public function getTableName() |
|
267 | } |
||
268 |