LocaleRepository::getLng()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 30
rs 9.4555
1
<?php
2
3
namespace App\Repositories;
4
5
use App\BaseModel as Model;
6
use Cache;
7
8
class LocaleRepository extends InitRepository
9
{
10
    public function __construct()
11
    {
12
        parent::__construct();
13
    }
14
15
    protected function getModelClass()
16
    {
17
        return Model::class;
18
    }
19
20
    public function getLng($lang)
21
    {
22
        if (!isset($lang)) {
23
            $lang = config('app.locale');
24
        }
25
26
        $files = glob(resource_path('lang/'.$lang.'/*.php'));
27
        $strings = [];
28
29
        foreach ($files as $file) {
30
            $name = basename($file, '.php');
31
            $strings[$name] = require $file;
32
        }
33
34
        //=== from modules
35
        $modules = config('module.modules');
36
        foreach ($modules as $module) {
37
            $module_files = glob(base_path('Modules/'.$module.'/resources/lang/'.$lang.'/*.php'));
38
39
            //add
40
            foreach ($module_files as $mfile) {
41
                $name = basename($mfile, '.php');
42
                $strings[$name] = require $mfile;
43
            }
44
        }
45
        //=== end modules
46
47
        $lng = 'window.i18n = '.json_encode($strings).';';
48
49
        return $lng;
50
    }
51
52
    public function getCacheLng($lang)
53
    {
54
        $cache_name = 'lng_'.$lang;
55
56
        if (Cache::has($cache_name) && !empty(Cache::get($cache_name))) {
57
            $data_cache = Cache::get($cache_name);
58
        } else {
59
            $data_cache = Cache::remember($cache_name, $this->cache_time_forever, function () use ($lang) {
60
                return $this->getLng($lang);
61
            });
62
        }
63
64
        return $data_cache;
65
    }
66
}
67