Skip to content

Commit 1aad9d7

Browse files
committed
Deterministic multi-file output
My own attempt at martinlindhe#109 using scandir() because it is sorted. Required for martinlindhe#111
1 parent fc4e344 commit 1aad9d7

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/Generator.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,17 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l
5353

5454
$locales = [];
5555
$files = [];
56-
$dir = new DirectoryIterator($path);
56+
$dirList = $this->getDirList($path);
5757
$jsBody = '';
58-
foreach ($dir as $fileinfo) {
59-
if (!$fileinfo->isDot()) {
58+
foreach ($dirList as $file) {
6059
if(!$withVendor
61-
&& in_array($fileinfo->getFilename(), array_merge(['vendor'], $this->config['excludes']))
60+
&& in_array($file, array_merge(['vendor'], $this->config['excludes']))
6261
) {
6362
continue;
6463
}
6564

66-
$files[] = $fileinfo->getRealPath();
67-
}
65+
$files[] = $path . DIRECTORY_SEPARATOR . $file;
6866
}
69-
asort($files);
7067

7168
foreach ($files as $fileName) {
7269
$fileinfo = new \SplFileInfo($fileName);
@@ -127,25 +124,23 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false)
127124
$locales = [];
128125
$fileToCreate = '';
129126
$createdFiles = '';
130-
$dir = new DirectoryIterator($path);
127+
$dirList = $this->getDirList($path);
131128
$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());
129+
foreach ($dirList as $file) {
130+
if (!in_array($file, array_merge(['vendor'], $this->config['excludes']))) {
131+
$noExt = $this->removeExtension($file);
138132
if ($noExt !== '') {
139133
if (class_exists('App')) {
140134
App::setLocale($noExt);
141135
}
142136
if (!in_array($noExt, $this->availableLocales)) {
143137
$this->availableLocales[] = $noExt;
144138
}
145-
if ($fileinfo->isDir()) {
146-
$local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales);
139+
$filePath = $path . DIRECTORY_SEPARATOR . $file;
140+
if (is_dir($filePath)) {
141+
$local = $this->allocateLocaleArray($filePath, $multiLocales);
147142
} else {
148-
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
143+
$local = $this->allocateLocaleJSON($filePath);
149144
if ($local === null) continue;
150145
}
151146

@@ -207,21 +202,15 @@ private function allocateLocaleJSON($path)
207202
private function allocateLocaleArray($path, $multiLocales = false)
208203
{
209204
$data = [];
210-
$dir = new DirectoryIterator($path);
205+
$dirList = $this->getDirList($path);
211206
$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());
207+
foreach ($dirList as $file) {
208+
$fileName = $path . DIRECTORY_SEPARATOR . $file;
209+
if (is_dir($fileName)) {
210+
// Recursively iterate through subdirs, until everything is allocated.
211+
$data[$file] = $this->allocateLocaleArray($fileName);
222212
} else {
223-
$noExt = $this->removeExtension($fileinfo->getFilename());
224-
$fileName = $path . DIRECTORY_SEPARATOR . $fileinfo->getFilename();
213+
$noExt = $this->removeExtension($file);
225214

226215
// Ignore non *.php files (ex.: .gitignore, vim swap files etc.)
227216
if (pathinfo($fileName, PATHINFO_EXTENSION) !== 'php') {
@@ -362,6 +351,17 @@ function ($matches) {
362351
);
363352
}
364353

354+
/**
355+
* Gets sorted directory list excluding dot files
356+
*
357+
* @param string $path
358+
* @return array
359+
*/
360+
private function getDirList($path)
361+
{
362+
return array_diff(scandir($path), ['.', '..']);
363+
}
364+
365365
/**
366366
* Returns filename, with extension stripped
367367
* @param string $filename

0 commit comments

Comments
 (0)