From 328602707213990715fccbb98f46731b19289902 Mon Sep 17 00:00:00 2001 From: Zhineng Li Date: Thu, 12 Feb 2026 19:00:33 +0800 Subject: first commit --- php-generator/.gitignore | 2 ++ php-generator/composer.json | 6 ++++ php-generator/generate.php | 78 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 php-generator/.gitignore create mode 100644 php-generator/composer.json create mode 100644 php-generator/generate.php (limited to 'php-generator') diff --git a/php-generator/.gitignore b/php-generator/.gitignore new file mode 100644 index 0000000..d1502b0 --- /dev/null +++ b/php-generator/.gitignore @@ -0,0 +1,2 @@ +vendor/ +composer.lock diff --git a/php-generator/composer.json b/php-generator/composer.json new file mode 100644 index 0000000..b3f0f41 --- /dev/null +++ b/php-generator/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "php": "^8.4", + "symfony/var-exporter": "^8.0" + } +} diff --git a/php-generator/generate.php b/php-generator/generate.php new file mode 100644 index 0000000..3f7e574 --- /dev/null +++ b/php-generator/generate.php @@ -0,0 +1,78 @@ + '.PHP_EOL, $argv[0]); + exit(1); +} + +[$src, $dst] = [$argv[1], $argv[2]]; +$srcPath = new \SplFileInfo($src); +$dstPath = new \SplFileInfo($dst); + +// Validate the source path +if ($srcPath->getRealPath() === false) { + printf('The source path "%s" does not exist.'.PHP_EOL, $srcPath->getPathname()); + exit(1); +} + +if ($srcPath->isDir() === false) { + printf('The source path "%s" is not a directory.'.PHP_EOL, $srcPath->getPathname()); + exit(1); +} + +// Validate the destination path +if ($dstPath->getRealPath() === false) { + printf('The destination path "%s" does not exist.'.PHP_EOL, $dstPath->getPathname()); + exit(1); +} + +if ($dstPath->isDir() === false) { + printf('The destination path "%s" is not a directory.'.PHP_EOL, $dstPath->getPathname()); + exit(1); +} + +$iterator = new \RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator( + new \RecursiveDirectoryIterator($srcPath->getRealPath(), \FilesystemIterator::SKIP_DOTS), + static function (\SplFileInfo $file) use ($srcPath): bool { + if ($file->isDir()) { + $relativePath = substr($file->getRealPath(), strlen($srcPath->getRealPath())); + + return str_starts_with($relativePath, DIRECTORY_SEPARATOR.'en_us') + || str_starts_with($relativePath, DIRECTORY_SEPARATOR.'zh_cn'); + } + + return $file->isFile() && $file->getExtension() === 'json'; + } +)); + +foreach ($iterator as $file) { + $relativePath = substr($file->getRealPath(), strlen($srcPath->getRealPath())); + + printf('[-] Generate %s'.PHP_EOL, $relativePath); + + // Change the file extension from json to php + $out = $dstPath->getRealPath().substr($relativePath, 0, -4).'php'; + $outdir = dirname($out); + + if (! file_exists($outdir)) { + mkdir($outdir, 0o755, recursive: true); + } + + $contents = file_get_contents($file->getRealPath()); + $decoded = json_decode($contents, associative: true, flags: JSON_THROW_ON_ERROR); + $result = sprintf('