diff options
| author | Zhineng Li <[email protected]> | 2026-02-12 19:00:33 +0800 |
|---|---|---|
| committer | Zhineng Li <[email protected]> | 2026-02-12 19:00:33 +0800 |
| commit | 328602707213990715fccbb98f46731b19289902 (patch) | |
| tree | dfbcb2455ad96af007c562f28c4055c75edd4bb7 /php-generator/generate.php | |
| download | acs-metadata-build-328602707213990715fccbb98f46731b19289902.tar.gz acs-metadata-build-328602707213990715fccbb98f46731b19289902.zip | |
first commit
Diffstat (limited to 'php-generator/generate.php')
| -rw-r--r-- | php-generator/generate.php | 78 |
1 files changed, 78 insertions, 0 deletions
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 + +declare(strict_types=1); + +use Symfony\Component\VarExporter\VarExporter; + +require __DIR__.'/vendor/autoload.php'; + +// Requires three parameters +// 1. The script name +// 2. The source path for metadata JSON files +// 3. The destination path to store the artifact +if ($argc !== 3) { + printf('Usage: php %s <src> <dst>'.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('<?php return %s;'.PHP_EOL, VarExporter::export($decoded)); + + file_put_contents($out, $result, flags: LOCK_EX); +} + +echo '[-] Generate successfully'.PHP_EOL; |
