From 79789166462da1b9e8cf899c772bd907cc902a70 Mon Sep 17 00:00:00 2001 From: Martin Lindhe Date: Sun, 9 Feb 2020 08:54:37 +0100 Subject: [PATCH] Revert "Fix multi-file output (#96)" This reverts commit f546c3f520d085f508b73933000da95ad528e1f1. --- src/Commands/GenerateInclude.php | 2 +- src/Generator.php | 59 ++++++++++------ tests/GenerateTest.php | 118 +------------------------------ 3 files changed, 40 insertions(+), 139 deletions(-) diff --git a/src/Commands/GenerateInclude.php b/src/Commands/GenerateInclude.php index 70435db..30fb23d 100644 --- a/src/Commands/GenerateInclude.php +++ b/src/Commands/GenerateInclude.php @@ -50,7 +50,7 @@ public function handle() if ($multipleFiles || $multipleLocales) { $files = (new Generator($config)) - ->generateMultiple($root, $format); + ->generateMultiple($root, $format, $multipleLocales); if ($config['showOutputMessages']) { $this->info("Written to : " . $files); diff --git a/src/Generator.php b/src/Generator.php index 200f0c3..38cdd00 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -84,7 +84,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l } if (isset($locales[$noExt])) { - $locales[$noExt] = array_merge_recursive($local, $locales[$noExt]); + $locales[$noExt] = array_merge($local, $locales[$noExt]); } else { $locales[$noExt] = $local; } @@ -93,7 +93,22 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l $locales = $this->adjustVendor($locales); - return $this->encodeJson($locales, $format); + $jsonLocales = json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; + + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception('Could not generate JSON, error code '.json_last_error()); + } + + // formats other than 'es6' and 'umd' will become plain JSON + if ($format === 'es6') { + $jsBody = $this->getES6Module($jsonLocales); + } elseif ($format === 'umd') { + $jsBody = $this->getUMDModule($jsonLocales); + } else { + $jsBody = $jsonLocales; + } + + return $jsBody; } /** @@ -102,7 +117,7 @@ public function generateFromPath($path, $format = 'es6', $withVendor = false, $l * @return string * @throws Exception */ - public function generateMultiple($path, $format = 'es6') + public function generateMultiple($path, $format = 'es6', $multiLocales = false) { if (!is_dir($path)) { throw new Exception('Directory not found: ' . $path); @@ -127,7 +142,7 @@ public function generateMultiple($path, $format = 'es6') $this->availableLocales[] = $noExt; } if ($fileinfo->isDir()) { - $local = $this->allocateLocaleArray($fileinfo->getRealPath()); + $local = $this->allocateLocaleArray($fileinfo->getRealPath(), $multiLocales); } else { $local = $this->allocateLocaleJSON($fileinfo->getRealPath()); if ($local === null) continue; @@ -141,10 +156,20 @@ public function generateMultiple($path, $format = 'es6') } } } - foreach ($locales as $fileName => $data) { + foreach ($this->filesToCreate as $fileName => $data) { $fileToCreate = $jsPath . $fileName . '.js'; $createdFiles .= $fileToCreate . PHP_EOL; - $jsBody = $this->encodeJson([$fileName => $data], $format); + $jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception('Could not generate JSON, error code '.json_last_error()); + } + if ($format === 'es6') { + $jsBody = $this->getES6Module($jsonLocales); + } elseif ($format === 'umd') { + $jsBody = $this->getUMDModule($jsonLocales); + } else { + $jsBody = $jsonLocales; + } if (!is_dir(dirname($fileToCreate))) { mkdir(dirname($fileToCreate), 0777, true); @@ -178,7 +203,7 @@ private function allocateLocaleJSON($path) * @param string $path * @return array */ - private function allocateLocaleArray($path) + private function allocateLocaleArray($path, $multiLocales = false) { $data = []; $dir = new DirectoryIterator($path); @@ -218,6 +243,11 @@ private function allocateLocaleArray($path) if($filePath[0] === DIRECTORY_SEPARATOR) { $filePath = substr($filePath, 1); } + if ($multiLocales) { + $this->filesToCreate[$lastLocale][$lastLocale][$filePath] = $this->adjustArray($tmp); + } else { + $this->filesToCreate[$filePath][$lastLocale] = $this->adjustArray($tmp); + } } $data[$noExt] = $this->adjustArray($tmp); @@ -374,19 +404,4 @@ private function getES6Module($body) { return "export default {$body}"; } - - private function encodeJson($data, $format = 'es6') - { - $jsonLocales = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL; - if (json_last_error() !== JSON_ERROR_NONE) { - throw new Exception('Could not generate JSON, error code '.json_last_error()); - } - if ($format === 'es6') { - return $this->getES6Module($jsonLocales); - } elseif ($format === 'umd') { - return $this->getUMDModule($jsonLocales); - } - - return $jsonLocales; - } } diff --git a/tests/GenerateTest.php b/tests/GenerateTest.php index 1f218dc..68c6dd8 100644 --- a/tests/GenerateTest.php +++ b/tests/GenerateTest.php @@ -4,22 +4,13 @@ class GenerateTest extends \PHPUnit_Framework_TestCase { - /** - * @return string - */ - private function getRootDir() + private function generateLocaleFilesFrom(array $arr) { $root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand()); - + if (!is_dir($root)) { mkdir($root, 0777, true); } - return $root; - } - - private function generateLocaleFilesFrom(array $arr, $root = null) - { - $root = $root ?: $this->getRootDir(); foreach ($arr as $key => $val) { @@ -36,18 +27,6 @@ private function generateLocaleFilesFrom(array $arr, $root = null) return $root; } - private function generateJsonLocaleFilesFrom(array $arr, $root = null) - { - $root = $root ?: $this->getRootDir(); - - foreach ($arr as $lang => $data) { - $outFile = $root . '/' . $lang . '.json'; - file_put_contents($outFile, json_encode($data)); - } - - return $root; - } - private function destroyLocaleFilesFrom(array $arr, $root) { foreach ($arr as $key => $val) { @@ -59,11 +38,6 @@ private function destroyLocaleFilesFrom(array $arr, $root) } } - $jsonFile = $root . '/'. $key . '.json'; - if (file_exists($jsonFile)) { - unlink($jsonFile); - } - if (is_dir($root . '/' . $key)) { rmdir($root . '/' . $key); } @@ -112,43 +86,6 @@ function testBasic() $this->destroyLocaleFilesFrom($arr, $root); } - function testBasicJsonFiles() - { - $arr = [ - 'en' => [ - 'help' => [ - 'yes' => 'yes', - 'no' => 'no', - ] - ], - 'sv' => [ - 'help' => [ - 'yes' => 'ja', - 'no' => 'nej', - ] - ] - ]; - - $root = $this->generateJsonLocaleFilesFrom($arr); - $this->assertEquals( - 'export default {' . PHP_EOL - . ' "en": {' . PHP_EOL - . ' "help": {' . PHP_EOL - . ' "yes": "yes",' . PHP_EOL - . ' "no": "no"' . PHP_EOL - . ' }' . PHP_EOL - . ' },' . PHP_EOL - . ' "sv": {' . PHP_EOL - . ' "help": {' . PHP_EOL - . ' "yes": "ja",' . PHP_EOL - . ' "no": "nej"' . PHP_EOL - . ' }' . PHP_EOL - . ' }' . PHP_EOL - . '}' . PHP_EOL, - (new Generator([]))->generateFromPath($root)); - $this->destroyLocaleFilesFrom($arr, $root); - } - function testBasicES6Format() { $format = 'es6'; @@ -607,55 +544,4 @@ function testPluralization() $this->destroyLocaleFilesFrom($arr, $root); } - - function testBothJsonAndPhpFiles() - { - $root = $this->getRootDir(); - $jsonArr = [ - 'en' => [ - 'help' => [ - 'no' => 'no', - ] - ], - 'sv' => [ - 'help' => [ - 'no' => 'nej', - ] - ] - ]; - $this->generateJsonLocaleFilesFrom($jsonArr, $root); - - $phpArr = [ - 'en' => [ - 'help' => [ - 'yes' => 'yes', - ] - ], - 'sv' => [ - 'help' => [ - 'yes' => 'ja', - ] - ] - ]; - $this->generateLocaleFilesFrom($phpArr, $root); - - $this->assertEquals( - 'export default {' . PHP_EOL - . ' "en": {' . PHP_EOL - . ' "help": {' . PHP_EOL - . ' "no": "no",' . PHP_EOL - . ' "yes": "yes"' . PHP_EOL - . ' }' . PHP_EOL - . ' },' . PHP_EOL - . ' "sv": {' . PHP_EOL - . ' "help": {' . PHP_EOL - . ' "no": "nej",' . PHP_EOL - . ' "yes": "ja"' . PHP_EOL - . ' }' . PHP_EOL - . ' }' . PHP_EOL - . '}' . PHP_EOL, - (new Generator([]))->generateFromPath($root)); - - $this->destroyLocaleFilesFrom($jsonArr, $root); - } }