1 | <?php |
||
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
207 | |||
208 | /** |
||
209 | * Get locale keys. |
||
210 | * |
||
211 | * @return array |
||
212 | */ |
||
213 | 12 | public function keys(): array |
|
228 | |||
229 | /** |
||
230 | * Get locales count. |
||
231 | * |
||
232 | * @return int |
||
233 | */ |
||
234 | 6 | public function count(): int |
|
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 |
|
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 |
|
267 | } |
||
268 |