Skip to content

Commit a17f3b1

Browse files
authored
Merge pull request #1 from murrant/deterministic_multi
Deterministic multi-file output
2 parents a5e8fe2 + b44903c commit a17f3b1

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

src/Generator.php

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php namespace MartinLindhe\VueInternationalizationGenerator;
22

3-
use DirectoryIterator;
4-
use Exception;
53
use App;
6-
use Traversable;
4+
use Exception;
75

86
class Generator
97
{
@@ -53,20 +51,17 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
5351

5452
$locales = [];
5553
$files = [];
56-
$dir = new DirectoryIterator($path);
54+
$dirList = $this->getDirList($path);
5755
$jsBody = '';
58-
foreach ($dir as $fileinfo) {
59-
if (!$fileinfo->isDot()) {
56+
foreach ($dirList as $file) {
6057
if(!$withVendor
61-
&& in_array($fileinfo->getFilename(), array_merge(['vendor'], $this->config['excludes']))
58+
&& in_array($file, array_merge(['vendor'], $this->config['excludes']))
6259
) {
6360
continue;
6461
}
6562

66-
$files[] = $fileinfo->getRealPath();
67-
}
63+
$files[] = $path . DIRECTORY_SEPARATOR . $file;
6864
}
69-
asort($files);
7065

7166
foreach ($files as $fileName) {
7267
$fileinfo = new \SplFileInfo($fileName);
@@ -127,25 +122,23 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false)
127122
$locales = [];
128123
$fileToCreate = '';
129124
$createdFiles = '';
130-
$dir = new DirectoryIterator($path);
125+
$dirList = $this->getDirList($path);
131126
$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);
138130
if ($noExt !== '') {
139131
if (class_exists('App')) {
140132
App::setLocale($noExt);
141133
}
142134
if (!in_array($noExt, $this->availableLocales)) {
143135
$this->availableLocales[] = $noExt;
144136
}
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);
147140
} else {
148-
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
141+
$local = $this->allocateLocaleJSON($filePath);
149142
if ($local === null) continue;
150143
}
151144

@@ -207,21 +200,15 @@ private function allocateLocaleJSON($path)
207200
private function allocateLocaleArray($path, $multiLocales = false)
208201
{
209202
$data = [];
210-
$dir = new DirectoryIterator($path);
203+
$dirList = $this->getDirList($path);
211204
$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);
222210
} else {
223-
$noExt = $this->removeExtension($fileinfo->getFilename());
224-
$fileName = $path . DIRECTORY_SEPARATOR . $fileinfo->getFilename();
211+
$noExt = $this->removeExtension($file);
225212

226213
// Ignore non *.php files (ex.: .gitignore, vim swap files etc.)
227214
if (pathinfo($fileName, PATHINFO_EXTENSION) !== 'php') {
@@ -362,6 +349,17 @@ function ($matches) {
362349
);
363350
}
364351

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+
365363
/**
366364
* Returns filename, with extension stripped
367365
* @param string $filename

0 commit comments

Comments
 (0)