diff options
Diffstat (limited to 'php-generator')
| -rw-r--r-- | php-generator/.gitignore | 2 | ||||
| -rw-r--r-- | php-generator/composer.json | 6 | ||||
| -rw-r--r-- | php-generator/generate.php | 78 |
3 files changed, 86 insertions, 0 deletions
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 + +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; |
