Completed
Push — master ( 83b029...ea7456 )
by ARCANEDEV
14s queued 12s
created

TransManager::loadJsonDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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