1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Kernel\Services; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Spl\Traits\Collectors\FilePathCollectorTrait; |
||
19 | use Traversable; |
||
20 | |||
21 | /** |
||
22 | * O2System Language |
||
23 | * |
||
24 | * This class is a collection, loader and manage of default languages data from O2System and User Applications. |
||
25 | * |
||
26 | * @package O2System\Kernel |
||
27 | */ |
||
28 | class Language implements \IteratorAggregate |
||
29 | { |
||
30 | use FilePathCollectorTrait; |
||
31 | |||
32 | /** |
||
33 | * Language::$defaultLocale |
||
34 | * |
||
35 | * Default language locale. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $defaultLocale = 'en'; |
||
40 | |||
41 | /** |
||
42 | * Language::$defaultIdeom |
||
43 | * |
||
44 | * Default language ideom. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $defaultIdeom = 'US'; |
||
49 | |||
50 | /** |
||
51 | * Language::$loaded |
||
52 | * |
||
53 | * List of loaded language files. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $loaded = []; |
||
58 | |||
59 | /** |
||
60 | * Language::$lines |
||
61 | * |
||
62 | * Languages Lines |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $lines = []; |
||
67 | |||
68 | // ------------------------------------------------------------------------ |
||
69 | |||
70 | /** |
||
71 | * Language::__construct |
||
72 | */ |
||
73 | public function __construct() |
||
74 | { |
||
75 | $this->setFileDirName('Languages'); |
||
76 | $this->addFilePath(PATH_KERNEL); |
||
77 | } |
||
78 | |||
79 | // ------------------------------------------------------------------------ |
||
80 | |||
81 | /** |
||
82 | * Language::setDefault |
||
83 | * |
||
84 | * Sets default language. |
||
85 | * |
||
86 | * @param string $default |
||
87 | * |
||
88 | * @return static |
||
89 | */ |
||
90 | public function setDefault($default) |
||
91 | { |
||
92 | $xDefault = explode('-', $default); |
||
93 | |||
94 | if (count($xDefault) == 2) { |
||
95 | list($locale, $ideom) = $xDefault; |
||
96 | $this->setDefaultLocale($locale); |
||
97 | $this->setDefaultIdeom($ideom); |
||
98 | } elseif (count($xDefault) == 1) { |
||
99 | $this->setDefaultLocale(reset($xDefault)); |
||
100 | } |
||
101 | |||
102 | if (class_exists('O2System\Framework', false) or class_exists('\O2System\Reactor', false)) { |
||
103 | if (services()->has('session')) { |
||
104 | session()->set('language', $this->getDefault()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
105 | } |
||
106 | |||
107 | if (count($this->loaded)) { |
||
108 | foreach ($this->loaded as $fileIndex => $filePath) { |
||
109 | unset($this->loaded[ $fileIndex ]); |
||
110 | $this->loadFile($fileIndex); |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return $this; |
||
116 | } |
||
117 | |||
118 | // ------------------------------------------------------------------------ |
||
119 | |||
120 | /** |
||
121 | * Language::getDefault |
||
122 | * |
||
123 | * Get default language with ideom |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getDefault() |
||
128 | { |
||
129 | return implode('-', [$this->defaultLocale, $this->defaultIdeom]); |
||
130 | } |
||
131 | |||
132 | // ------------------------------------------------------------------------ |
||
133 | |||
134 | /** |
||
135 | * Language::load |
||
136 | * |
||
137 | * Load language file into collections |
||
138 | * |
||
139 | * @param string|array $filenames |
||
140 | * |
||
141 | * @return static |
||
142 | */ |
||
143 | public function loadFile($filenames) |
||
144 | { |
||
145 | $filenames = is_string($filenames) ? [$filenames] : $filenames; |
||
146 | |||
147 | if (empty($filenames)) { |
||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | foreach ($filenames as $filename) { |
||
152 | $filename = dash($filename); |
||
153 | if ( ! $this->isLoaded($filename)) { |
||
154 | if (is_file($filename)) { |
||
155 | $this->parseFile($filename); |
||
156 | break; |
||
157 | } elseif (false !== ($filePath = $this->findFile($filename))) { |
||
158 | $this->parseFile($filePath); |
||
159 | break; |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | return $this; |
||
165 | } |
||
166 | |||
167 | // ------------------------------------------------------------------------ |
||
168 | |||
169 | /** |
||
170 | * Language::isLoaded |
||
171 | * |
||
172 | * Checks if the language file has been loaded. |
||
173 | * |
||
174 | * @param string $filePath |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function isLoaded($filePath) |
||
179 | { |
||
180 | return array_key_exists($this->getFileIndex($filePath), $this->loaded); |
||
181 | } |
||
182 | |||
183 | // ------------------------------------------------------------------------ |
||
184 | |||
185 | /** |
||
186 | * Language::getFileIndex |
||
187 | * |
||
188 | * Gets filepath index key. |
||
189 | * |
||
190 | * @param string $filePath |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | protected function getFileIndex($filePath) |
||
195 | { |
||
196 | $fileIndex = pathinfo($filePath, PATHINFO_FILENAME); |
||
197 | $fileIndex = str_replace('_' . $this->getDefault(), '', $fileIndex); |
||
198 | |||
199 | return $fileIndex; |
||
200 | } |
||
201 | |||
202 | // ------------------------------------------------------------------------ |
||
203 | |||
204 | /** |
||
205 | * Language::parseFile |
||
206 | * |
||
207 | * Parse INI language file into collections |
||
208 | * |
||
209 | * @param string $filePath Language INI filePath |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | protected function parseFile($filePath) |
||
214 | { |
||
215 | $lines = parse_ini_file($filePath, true, INI_SCANNER_RAW); |
||
216 | |||
217 | if (is_array($lines)) { |
||
218 | if (count($lines)) { |
||
219 | $this->loaded[ $this->getFileIndex($filePath) ] = $filePath; |
||
220 | |||
221 | $this->lines = array_merge($this->lines, $lines); |
||
222 | |||
223 | return true; |
||
224 | } |
||
225 | } |
||
226 | |||
227 | return false; |
||
228 | } |
||
229 | |||
230 | // ------------------------------------------------------------------------ |
||
231 | |||
232 | /** |
||
233 | * Language::findFile |
||
234 | * |
||
235 | * Find language file. |
||
236 | * |
||
237 | * @param string $filename |
||
238 | * |
||
239 | * @return string|bool Returns FALSE if failed. |
||
240 | */ |
||
241 | protected function findFile($filename) |
||
242 | { |
||
243 | $default = $this->getDefault(); |
||
244 | |||
245 | foreach ($this->filePaths as $filePath) { |
||
246 | $filePaths = [ |
||
247 | $filePath . $default . DIRECTORY_SEPARATOR . $filename . '.ini', |
||
248 | $filePath . $filename . '_' . $default . '.ini', |
||
249 | $filePath . $filename . '-' . $default . '.ini', |
||
250 | $filePath . $filename . '.ini', |
||
251 | ]; |
||
252 | |||
253 | foreach ($filePaths as $filePath) { |
||
0 ignored issues
–
show
|
|||
254 | if (is_file($filePath) AND ! in_array($filePath, $this->loaded)) { |
||
255 | return $filePath; |
||
256 | break; |
||
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other ![]() |
|||
257 | break; |
||
258 | } |
||
259 | } |
||
260 | |||
261 | unset($filePath); |
||
262 | } |
||
263 | |||
264 | return false; |
||
265 | } |
||
266 | |||
267 | // ------------------------------------------------------------------------ |
||
268 | |||
269 | /** |
||
270 | * Language::getDefaultLocale |
||
271 | * |
||
272 | * Gets default language locale. |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getDefaultLocale() |
||
277 | { |
||
278 | return $this->defaultLocale; |
||
279 | } |
||
280 | |||
281 | // ------------------------------------------------------------------------ |
||
282 | |||
283 | /** |
||
284 | * Language::setDefaultLocale |
||
285 | * |
||
286 | * Sets default language locale. |
||
287 | * |
||
288 | * @param string $defaultLocale |
||
289 | * |
||
290 | * @return static |
||
291 | */ |
||
292 | public function setDefaultLocale($defaultLocale) |
||
293 | { |
||
294 | $this->defaultLocale = strtolower($defaultLocale); |
||
295 | $this->defaultIdeom = strtoupper($defaultLocale); |
||
296 | |||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | // ------------------------------------------------------------------------ |
||
301 | |||
302 | /** |
||
303 | * Language::getDefaultIdeom |
||
304 | * |
||
305 | * Gets default language ideom |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getDefaultIdeom() |
||
310 | { |
||
311 | return $this->defaultIdeom; |
||
312 | } |
||
313 | |||
314 | // ------------------------------------------------------------------------ |
||
315 | |||
316 | /** |
||
317 | * Langauge::setDefaultIdeom |
||
318 | * |
||
319 | * Sets default language ideom |
||
320 | * |
||
321 | * @param string $defaultIdeom |
||
322 | * |
||
323 | * @return static |
||
324 | */ |
||
325 | public function setDefaultIdeom($defaultIdeom) |
||
326 | { |
||
327 | $this->defaultIdeom = strtoupper($defaultIdeom); |
||
328 | |||
329 | return $this; |
||
330 | } |
||
331 | |||
332 | // ------------------------------------------------------------------------ |
||
333 | |||
334 | /** |
||
335 | * Language::getLine |
||
336 | * |
||
337 | * Parse single language line of text |
||
338 | * |
||
339 | * @param string $line Language line key |
||
340 | * @param array $context Language line context |
||
341 | * |
||
342 | * @return mixed|null |
||
343 | */ |
||
344 | public function getLine($line, array $context = []) |
||
345 | { |
||
346 | $lineOffset = strtoupper($line); |
||
347 | |||
348 | if (empty($context)) { |
||
349 | $lineContent = isset($this->lines[ $lineOffset ]) ? $this->lines[ $lineOffset ] : $line; |
||
350 | } else { |
||
351 | $line = isset($this->lines[ $lineOffset ]) ? $this->lines[ $lineOffset ] : $line; |
||
352 | array_unshift($context, $line); |
||
353 | |||
354 | $lineContent = @call_user_func_array('sprintf', $context); |
||
355 | } |
||
356 | |||
357 | return str_replace(['PHP_EOL', 'PHP_EOL '], PHP_EOL, $lineContent); |
||
358 | } |
||
359 | |||
360 | // ------------------------------------------------------------------------ |
||
361 | |||
362 | /** |
||
363 | * Language::getIterator |
||
364 | * |
||
365 | * Retrieve an external iterator |
||
366 | * |
||
367 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
368 | * @return Traversable An instance of an object implementing <b>Iterator</b> or |
||
369 | * <b>Traversable</b> |
||
370 | * @since 5.0.0 |
||
371 | */ |
||
372 | public function getIterator() |
||
373 | { |
||
374 | return new \ArrayIterator($this->lines); |
||
375 | } |
||
376 | } |