|
1 | 1 | <?php namespace MartinLindhe\VueInternationalizationGenerator;
|
2 | 2 |
|
3 |
| -use DirectoryIterator; |
4 |
| -use Exception; |
5 | 3 | use App;
|
6 |
| -use Traversable; |
| 4 | +use Exception; |
7 | 5 |
|
8 | 6 | class Generator
|
9 | 7 | {
|
@@ -53,20 +51,17 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
|
53 | 51 |
|
54 | 52 | $locales = [];
|
55 | 53 | $files = [];
|
56 |
| - $dir = new DirectoryIterator($path); |
| 54 | + $dirList = $this->getDirList($path); |
57 | 55 | $jsBody = '';
|
58 |
| - foreach ($dir as $fileinfo) { |
59 |
| - if (!$fileinfo->isDot()) { |
| 56 | + foreach ($dirList as $file) { |
60 | 57 | if(!$withVendor
|
61 |
| - && in_array($fileinfo->getFilename(), array_merge(['vendor'], $this->config['excludes'])) |
| 58 | + && in_array($file, array_merge(['vendor'], $this->config['excludes'])) |
62 | 59 | ) {
|
63 | 60 | continue;
|
64 | 61 | }
|
65 | 62 |
|
66 |
| - $files[] = $fileinfo->getRealPath(); |
67 |
| - } |
| 63 | + $files[] = $path . DIRECTORY_SEPARATOR . $file; |
68 | 64 | }
|
69 |
| - asort($files); |
70 | 65 |
|
71 | 66 | foreach ($files as $fileName) {
|
72 | 67 | $fileinfo = new \SplFileInfo($fileName);
|
@@ -127,25 +122,23 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false)
|
127 | 122 | $locales = [];
|
128 | 123 | $fileToCreate = '';
|
129 | 124 | $createdFiles = '';
|
130 |
| - $dir = new DirectoryIterator($path); |
| 125 | + $dirList = $this->getDirList($path); |
131 | 126 | $jsBody = '';
|
132 |
| - foreach ($dir as $fileinfo) { |
133 |
| - if (!$fileinfo->isDot() |
134 |
| - && !in_array($fileinfo->getFilename(), array_merge(['vendor'], $this->config['excludes'])) |
135 |
| - && $fileinfo !== '' |
136 |
| - ) { |
137 |
| - $noExt = $this->removeExtension($fileinfo->getFilename()); |
| 127 | + foreach ($dirList as $file) { |
| 128 | + if (!in_array($file, array_merge(['vendor'], $this->config['excludes']))) { |
| 129 | + $noExt = $this->removeExtension($file); |
138 | 130 | if ($noExt !== '') {
|
139 | 131 | if (class_exists('App')) {
|
140 | 132 | App::setLocale($noExt);
|
141 | 133 | }
|
142 | 134 | if (!in_array($noExt, $this->availableLocales)) {
|
143 | 135 | $this->availableLocales[] = $noExt;
|
144 | 136 | }
|
145 |
| - if ($fileinfo->isDir()) { |
146 |
| - $local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales); |
| 137 | + $filePath = $path . DIRECTORY_SEPARATOR . $file; |
| 138 | + if (is_dir($filePath)) { |
| 139 | + $local = $this->allocateLocaleArray($filePath, $multiLocales); |
147 | 140 | } else {
|
148 |
| - $local = $this->allocateLocaleJSON($fileinfo->getRealPath()); |
| 141 | + $local = $this->allocateLocaleJSON($filePath); |
149 | 142 | if ($local === null) continue;
|
150 | 143 | }
|
151 | 144 |
|
@@ -207,21 +200,15 @@ private function allocateLocaleJSON($path)
|
207 | 200 | private function allocateLocaleArray($path, $multiLocales = false)
|
208 | 201 | {
|
209 | 202 | $data = [];
|
210 |
| - $dir = new DirectoryIterator($path); |
| 203 | + $dirList = $this->getDirList($path); |
211 | 204 | $lastLocale = last($this->availableLocales);
|
212 |
| - foreach ($dir as $fileinfo) { |
213 |
| - // Do not mess with dotfiles at all. |
214 |
| - if ($fileinfo->isDot()) { |
215 |
| - continue; |
216 |
| - } |
217 |
| - |
218 |
| - if ($fileinfo->isDir()) { |
219 |
| - // Recursivley iterate through subdirs, until everything is allocated. |
220 |
| - |
221 |
| - $data[$fileinfo->getFilename()] = $this->allocateLocaleArray($path . DIRECTORY_SEPARATOR . $fileinfo->getFilename()); |
| 205 | + foreach ($dirList as $file) { |
| 206 | + $fileName = $path . DIRECTORY_SEPARATOR . $file; |
| 207 | + if (is_dir($fileName)) { |
| 208 | + // Recursively iterate through subdirs, until everything is allocated. |
| 209 | + $data[$file] = $this->allocateLocaleArray($fileName); |
222 | 210 | } else {
|
223 |
| - $noExt = $this->removeExtension($fileinfo->getFilename()); |
224 |
| - $fileName = $path . DIRECTORY_SEPARATOR . $fileinfo->getFilename(); |
| 211 | + $noExt = $this->removeExtension($file); |
225 | 212 |
|
226 | 213 | // Ignore non *.php files (ex.: .gitignore, vim swap files etc.)
|
227 | 214 | if (pathinfo($fileName, PATHINFO_EXTENSION) !== 'php') {
|
@@ -362,6 +349,17 @@ function ($matches) {
|
362 | 349 | );
|
363 | 350 | }
|
364 | 351 |
|
| 352 | + /** |
| 353 | + * Gets sorted directory list excluding dot files |
| 354 | + * |
| 355 | + * @param string $path |
| 356 | + * @return array |
| 357 | + */ |
| 358 | + private function getDirList($path) |
| 359 | + { |
| 360 | + return array_diff(scandir($path), ['.', '..']); |
| 361 | + } |
| 362 | + |
365 | 363 | /**
|
366 | 364 | * Returns filename, with extension stripped
|
367 | 365 | * @param string $filename
|
|
0 commit comments