1 | <?php |
||
9 | class Reader |
||
10 | { |
||
11 | /** |
||
12 | * Translation items. |
||
13 | * |
||
14 | * @var \Illuminate\Support\Collection |
||
15 | */ |
||
16 | protected $translations; |
||
17 | |||
18 | /** |
||
19 | * @var Application |
||
20 | */ |
||
21 | protected $app; |
||
22 | |||
23 | /** |
||
24 | * @var \Illuminate\Filesystem\Filesystem |
||
25 | */ |
||
26 | protected $files; |
||
27 | |||
28 | /** |
||
29 | * @var string specify locale that we need to scan. |
||
30 | * Returns all if no locale specified. |
||
31 | */ |
||
32 | private $locale; |
||
33 | |||
34 | /** |
||
35 | * Reader. |
||
36 | * |
||
37 | * @param Application $app |
||
38 | * @param Filesystem $files |
||
39 | */ |
||
40 | 3 | public function __construct(Application $app, Filesystem $files) |
|
45 | |||
46 | /** |
||
47 | * Set reader locale. |
||
48 | * |
||
49 | * @param $locale |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function setLocale($locale) |
||
58 | |||
59 | /** |
||
60 | * Scan modules, app and overridden packages lang |
||
61 | * and return all defined translations. |
||
62 | * |
||
63 | * @return Collection |
||
64 | * @return array |
||
65 | */ |
||
66 | 2 | public function scan() |
|
76 | |||
77 | /** |
||
78 | * Scan a directory. |
||
79 | * |
||
80 | * @param string $path to directory to scan |
||
81 | */ |
||
82 | 2 | protected function scanDirectory($path) |
|
83 | { |
||
84 | 2 | foreach ($this->files->directories($path) as $directory) { |
|
85 | 2 | if ($this->isVendorDirectory($directory)) { |
|
86 | 2 | $this->scanVendorDirectory($directory); |
|
87 | } |
||
88 | else { |
||
89 | 2 | $this->loadTranslationsInDirectory($directory, $this->getLocaleFromDirectory($directory), null); |
|
90 | } |
||
91 | } |
||
92 | 2 | } |
|
93 | |||
94 | /** |
||
95 | * Scan overridden packages lang. |
||
96 | * |
||
97 | * @param $vendorsDirectory |
||
98 | */ |
||
99 | 2 | private function scanVendorDirectory($vendorsDirectory) |
|
100 | { |
||
101 | 2 | foreach ($this->files->directories($vendorsDirectory) as $vendorPath) { |
|
102 | 2 | $namespace = basename($vendorPath); |
|
103 | 2 | foreach ($this->files->directories($vendorPath) as $localePath) { |
|
104 | 2 | $this->loadTranslationsInDirectory($localePath, basename($localePath), $namespace); |
|
105 | } |
||
106 | } |
||
107 | 2 | } |
|
108 | |||
109 | /** |
||
110 | * Load all directory file translation (multiple group) into translations collection. |
||
111 | * |
||
112 | * @param $directory |
||
113 | * @param $locale |
||
114 | * @param $namespace |
||
115 | */ |
||
116 | 2 | private function loadTranslationsInDirectory($directory, $locale, $namespace) |
|
128 | |||
129 | /** |
||
130 | * Load file translation (group) into translations collection. |
||
131 | * |
||
132 | * @param $locale |
||
133 | * @param $group |
||
134 | * @param $namespace |
||
135 | * @param $file |
||
136 | */ |
||
137 | 2 | private function loadTranslations($locale, $group, $namespace, $file) |
|
162 | |||
163 | /** |
||
164 | * Return a full lang key. |
||
165 | * |
||
166 | * @param $namespace |
||
167 | * @param $group |
||
168 | * @param $key |
||
169 | * @return string |
||
170 | */ |
||
171 | 2 | private function fullKey($namespace, $group, $key) |
|
178 | |||
179 | /** |
||
180 | * Return relative path of language file. |
||
181 | * |
||
182 | * @param $file |
||
183 | * @return mixed |
||
184 | */ |
||
185 | 2 | private function sourceFile($file) |
|
189 | |||
190 | /** |
||
191 | * Return relative path related to base_path(). |
||
192 | * |
||
193 | * @param $path |
||
194 | * @return string |
||
195 | */ |
||
196 | 2 | private function toRelative($path) |
|
203 | |||
204 | /** |
||
205 | * Determine if a found locale is requested for scanning. |
||
206 | * If $this->locale is not set, we assume that all the locales were requested. |
||
207 | * |
||
208 | * @param string $locale the locale to check |
||
209 | * @return bool |
||
210 | */ |
||
211 | 2 | private function requestedLocale($locale) |
|
219 | |||
220 | /** |
||
221 | * Return locale from directory |
||
222 | * ie. resources/lang/en -> en. |
||
223 | * |
||
224 | * @param $directory |
||
225 | * @return string |
||
226 | */ |
||
227 | 2 | private function getLocaleFromDirectory($directory) |
|
231 | |||
232 | /** |
||
233 | * Return true if it is the vendor directory. |
||
234 | * |
||
235 | * @param $directory |
||
236 | * @return bool |
||
237 | */ |
||
238 | 3 | private function isVendorDirectory($directory) |
|
242 | } |
||
243 |