Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.

Commit 0e29f04

Browse files
authored
Make generateMultiple and allocateLocaleArray deterministic (#105)
1 parent 8f47124 commit 0e29f04

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/Generator.php

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,39 @@ public function generateMultiple($path, $format = 'es6', $multiLocales = false)
129129
$createdFiles = '';
130130
$dir = new DirectoryIterator($path);
131131
$jsBody = '';
132+
$files = [];
133+
132134
foreach ($dir as $fileinfo) {
133135
if (!$fileinfo->isDot()
134136
&& !in_array($fileinfo->getFilename(), array_merge(['vendor'], $this->config['excludes']))
135137
&& $fileinfo !== ''
136138
) {
137-
$noExt = $this->removeExtension($fileinfo->getFilename());
138-
if ($noExt !== '') {
139-
if (class_exists('App')) {
140-
App::setLocale($noExt);
141-
}
142-
if (!in_array($noExt, $this->availableLocales)) {
143-
$this->availableLocales[] = $noExt;
144-
}
145-
if ($fileinfo->isDir()) {
146-
$local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales);
147-
} else {
148-
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
149-
if ($local === null) continue;
150-
}
139+
$files[] = $fileinfo->getRealPath();
140+
}
141+
}
142+
asort($files);
151143

152-
if (isset($locales[$noExt])) {
153-
$locales[$noExt] = array_merge($local, $locales[$noExt]);
154-
} else {
155-
$locales[$noExt] = $local;
156-
}
144+
foreach ($files as $fileName) {
145+
$fileinfo = new \SplFileInfo($fileName);
146+
$noExt = $this->removeExtension($fileinfo->getFilename());
147+
if ($noExt !== '') {
148+
if (class_exists('App')) {
149+
App::setLocale($noExt);
150+
}
151+
if (!in_array($noExt, $this->availableLocales)) {
152+
$this->availableLocales[] = $noExt;
153+
}
154+
if ($fileinfo->isDir()) {
155+
$local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales);
156+
} else {
157+
$local = $this->allocateLocaleJSON($fileinfo->getRealPath());
158+
if ($local === null) continue;
159+
}
160+
161+
if (isset($locales[$noExt])) {
162+
$locales[$noExt] = array_merge($local, $locales[$noExt]);
163+
} else {
164+
$locales[$noExt] = $local;
157165
}
158166
}
159167
}
@@ -209,12 +217,19 @@ private function allocateLocaleArray($path, $multiLocales = false)
209217
$data = [];
210218
$dir = new DirectoryIterator($path);
211219
$lastLocale = last($this->availableLocales);
220+
$files = [];
212221
foreach ($dir as $fileinfo) {
213222
// Do not mess with dotfiles at all.
214223
if ($fileinfo->isDot()) {
215224
continue;
216225
}
217226

227+
$files[] = $fileinfo->getRealPath();
228+
}
229+
asort($files);
230+
231+
foreach ($files as $fileName) {
232+
$fileinfo = new \SplFileInfo($fileName);
218233
if ($fileinfo->isDir()) {
219234
// Recursivley iterate through subdirs, until everything is allocated.
220235

0 commit comments

Comments
 (0)