Completed
Pull Request — master (#42)
by ARCANEDEV
12:15 queued 11:06
created

TransManager::loadTranslations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelLang;
6
7
use Arcanedev\LaravelLang\Contracts\TransManager as TransManagerContract;
8
use Arcanedev\LaravelLang\Entities\{Locale, LocaleCollection};
9
use Illuminate\Filesystem\Filesystem;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class     TransManager
14
 *
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class TransManager implements TransManagerContract
18
{
19
    /* -----------------------------------------------------------------
20
     |  Properties
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Lang directories paths.
26
     *
27
     * @var array
28
     */
29
    private $paths   = [];
30
31
    /**
32
     * Translations collection.
33
     *
34
     * @var array
35
     */
36
    private $locales = [];
37
38
    /**
39
     * The filesystem instance.
40
     *
41
     * @var \Illuminate\Filesystem\Filesystem
42
     */
43
    private $filesystem;
44
45
    /**
46
     * Excluded folders.
47
     *
48
     * @var array
49
     */
50
    private $excludedFolders = [
51
        'script',
52
        'vendor',
53
    ];
54
55
    /* -----------------------------------------------------------------
56
     |  Constructor
57
     | -----------------------------------------------------------------
58
     */
59
60
    /**
61
     * Make TransManager instance.
62
     *
63
     * @param  \Illuminate\Filesystem\Filesystem  $filesystem
64
     * @param  array                              $paths
65
     */
66 132
    public function __construct(Filesystem $filesystem, array $paths)
67
    {
68 132
        $this->filesystem = $filesystem;
69 132
        $this->paths      = $paths;
70
71 132
        $this->load();
72 132
    }
73
74
    /* -----------------------------------------------------------------
75
     |  Getters & Setters
76
     | -----------------------------------------------------------------
77
     */
78
79
    /**
80
     * Get the translation paths.
81
     *
82
     * @return array
83
     */
84 132
    public function getPaths(): array
85
    {
86 132
        return $this->paths;
87
    }
88
89
    /* -----------------------------------------------------------------
90
     |  Main Methods
91
     | -----------------------------------------------------------------
92
     */
93
94
    /**
95
     * Load lang files.
96
     */
97 132
    private function load(): void
98
    {
99 132
        $paths = $this->getPaths();
100
101 132
        $this->locales['app'] = $this->loadTranslations($paths['app']);
102
103 132
        foreach ($paths['vendors'] as $path) {
104 132
            $this->locales['vendor-php'] = $this->loadTranslations($path);
105 132
            $this->locales['vendor-json'] = $this->loadJsonTranslations($path);
106
        }
107 132
    }
108
109
    /**
110
     * Load translation files.
111
     *
112
     * @param  string  $dirPath
113
     *
114
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection
115
     */
116 132
    private function loadTranslations(string $dirPath): LocaleCollection
117
    {
118 132
        $locales = new LocaleCollection;
119
120 132
        foreach ($this->filesystem->directories($dirPath) as $path) {
121 132
            if ($this->isExcluded($path))
122 132
                continue;
123
124 132
            $locales->addOne(
125 132
                new Locale(basename($path), $path, $this->loadTranslationFiles($path))
126
            );
127
        }
128
129 132
        return $locales;
130
    }
131
132
    /**
133
     * Load the locale translation files.
134
     *
135
     * @param  string  $path
136
     *
137
     * @return array
138
     */
139 132
    private function loadTranslationFiles(string $path): array
140
    {
141 132
        $files = [];
142
143 132
        foreach ($this->filesystem->allFiles($path) as $file) {
144 132
            if ($file->getExtension() !== 'php')
145 132
                continue;
146
147 132
            $key = str_replace(
148 132
                ['.php', DIRECTORY_SEPARATOR], ['', '.'], $file->getRelativePathname()
149
            );
150
151 132
            $files[$key] = [
152 132
                'path'    => $file->getRealPath(),
153 132
                'content' => $this->filesystem->getRequire($file),
154
            ];
155
        }
156
157 132
        return $files;
158
    }
159
160
    /**
161
     * Load json translation files.
162
     *
163
     * @param  string  $path
164
     *
165
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection
166
     */
167 132
    private function loadJsonTranslations(string $path): LocaleCollection
168
    {
169 132
        $locales = new LocaleCollection;
170
171 132
        foreach ($this->filesystem->files($path) as $file) {
172
            if ($file->getExtension() !== 'json')
173
                continue;
174
175
            $locales->addOne(
176
                new Locale($file->getBasename('.json'), $file->getRealPath())
177
            );
178
        }
179
180 132
        return $locales;
181
    }
182
183
    /**
184
     * Get locale collection by group location.
185
     *
186
     * @param  string      $group
187
     * @param  mixed|null  $default
188
     *
189
     * @return \Arcanedev\LaravelLang\Entities\LocaleCollection|null
190
     */
191 102
    public function getCollection(string $group, $default = null): ?LocaleCollection
192
    {
193 102
        return Arr::get($this->locales, $group, $default);
194
    }
195
196
    /**
197
     * Get a locale translations from a group.
198
     *
199
     * @param  string  $group
200
     * @param  string  $locale
201
     * @param  null    $default
202
     *
203
     * @return \Arcanedev\LaravelLang\Entities\Locale|null
204
     */
205 18
    public function getFrom(string $group, string $locale, $default = null): ?Locale
206
    {
207 18
        if ( ! $this->hasCollection($group)) {
208 6
            return $default;
209
        }
210
211 12
        $locales = $this->getCollection($group);
212
213 12
        return $locales->get($locale, $default);
214
    }
215
216
    /**
217
     * Get locale keys.
218
     *
219
     * @return array
220
     */
221 12
    public function keys(): array
222
    {
223 12
        $locales = array_map(function (LocaleCollection $locales) {
224 12
            $keys = $locales->keys()->toArray();
225 12
            return array_combine($keys, $keys);
226 12
        }, $this->locales);
227
228 12
        $all = [];
229
230 12
        foreach ($locales as $keys) {
231 12
            $all = array_merge($all, $keys);
232
        }
233
234 12
        return $all;
235
    }
236
237
    /**
238
     * Get locales count.
239
     *
240
     * @return int
241
     */
242 6
    public function count(): int
243
    {
244 6
        return count($this->keys());
245
    }
246
247
    /* -----------------------------------------------------------------
248
     |  Check Methods
249
     | -----------------------------------------------------------------
250
     */
251
252
    /**
253
     * Check if a translation group exists.
254
     *
255
     * @param  string  $group
256
     *
257
     * @return bool
258
     */
259 24
    public function hasCollection(string $group): bool
260
    {
261 24
        return Arr::has($this->locales, $group);
262
    }
263
264
    /**
265
     * Check if the given path is excluded.
266
     *
267
     * @param  string  $path
268
     *
269
     * @return bool
270
     */
271 132
    private function isExcluded(string $path): bool
272
    {
273 132
        return in_array($key = basename($path), $this->excludedFolders);
274
    }
275
}
276