Fix config yaml support

This commit is contained in:
crazywhalecc
2026-01-22 16:03:06 +08:00
parent 1865762f80
commit ae748757d1

View File

@@ -7,6 +7,7 @@ namespace StaticPHP\Config;
use StaticPHP\Exception\WrongUsageException; use StaticPHP\Exception\WrongUsageException;
use StaticPHP\Registry\Registry; use StaticPHP\Registry\Registry;
use StaticPHP\Runtime\SystemTarget; use StaticPHP\Runtime\SystemTarget;
use Symfony\Component\Yaml\Yaml;
class PackageConfig class PackageConfig
{ {
@@ -47,10 +48,12 @@ class PackageConfig
if ($content === false) { if ($content === false) {
throw new WrongUsageException("Failed to read package config file: {$file}"); throw new WrongUsageException("Failed to read package config file: {$file}");
} }
$data = json_decode($content, true); // judge extension
if (!is_array($data)) { $data = match (pathinfo($file, PATHINFO_EXTENSION)) {
throw new WrongUsageException("Invalid JSON format in package config file: {$file}"); 'json' => json_decode($content, true),
} 'yml', 'yaml' => Yaml::parse($content),
default => throw new WrongUsageException("Unsupported package config file format: {$file}"),
};
ConfigValidator::validateAndLintPackages(basename($file), $data); ConfigValidator::validateAndLintPackages(basename($file), $data);
foreach ($data as $pkg_name => $config) { foreach ($data as $pkg_name => $config) {
self::$package_configs[$pkg_name] = $config; self::$package_configs[$pkg_name] = $config;