Files
static-php-cli/src/StaticPHP/Config/PackageConfig.php

152 lines
5.3 KiB
PHP
Raw Normal View History

2025-11-30 15:35:04 +08:00
<?php
declare(strict_types=1);
namespace StaticPHP\Config;
use StaticPHP\Exception\WrongUsageException;
2025-12-15 17:00:20 +08:00
use StaticPHP\Registry\Registry;
2025-11-30 15:35:04 +08:00
use StaticPHP\Runtime\SystemTarget;
2026-02-14 17:24:49 +08:00
use StaticPHP\Util\FileSystem;
2026-01-22 16:03:06 +08:00
use Symfony\Component\Yaml\Yaml;
2025-11-30 15:35:04 +08:00
class PackageConfig
{
private static array $package_configs = [];
/**
* Load package configurations from a specified directory.
* It will look for files matching the pattern 'pkg.*.json' and 'pkg.json'.
*/
public static function loadFromDir(string $dir, string $registry_name): array
2025-11-30 15:35:04 +08:00
{
if (!is_dir($dir)) {
throw new WrongUsageException("Directory {$dir} does not exist, cannot load pkg.json config.");
}
2026-03-13 17:02:35 +08:00
$dir = rtrim($dir, '/');
$loaded = [];
2026-02-14 17:24:49 +08:00
$files = FileSystem::scanDirFiles($dir, false);
2025-11-30 15:35:04 +08:00
if (is_array($files)) {
foreach ($files as $file) {
2025-12-15 17:00:20 +08:00
self::loadFromFile($file, $registry_name);
$loaded[] = $file;
2025-11-30 15:35:04 +08:00
}
}
if (file_exists("{$dir}/pkg.json")) {
2025-12-15 17:00:20 +08:00
self::loadFromFile("{$dir}/pkg.json", $registry_name);
$loaded[] = "{$dir}/pkg.json";
2025-11-30 15:35:04 +08:00
}
return $loaded;
2025-11-30 15:35:04 +08:00
}
/**
* Load package configurations from a specified JSON file.
*
* @param string $file the path to the json package configuration file
*/
public static function loadFromFile(string $file, string $registry_name): string
2025-11-30 15:35:04 +08:00
{
$content = @file_get_contents($file);
2025-11-30 15:35:04 +08:00
if ($content === false) {
throw new WrongUsageException("Failed to read package config file: {$file}");
}
// judge extension — use cache to skip redundant parsing
$data = ConfigCache::get($content);
if ($data !== null) {
logger()->debug("Config cache hit: {$file}");
} else {
$data = match (pathinfo($file, PATHINFO_EXTENSION)) {
'json' => json_decode($content, true),
'yml', 'yaml' => extension_loaded('yaml') ? yaml_parse($content) : Yaml::parse($content),
default => throw new WrongUsageException("Unsupported package config file format: {$file}"),
};
if (is_array($data)) {
ConfigCache::set($content, $data);
}
}
2025-11-30 15:35:04 +08:00
ConfigValidator::validateAndLintPackages(basename($file), $data);
foreach ($data as $pkg_name => $config) {
self::$package_configs[$pkg_name] = $config;
2025-12-15 17:00:20 +08:00
Registry::_bindPackageConfigFile($pkg_name, $registry_name, $file);
2026-02-02 13:32:35 +08:00
// Register inline artifact if present
if (isset($config['artifact']) && is_array($config['artifact'])) {
ArtifactConfig::registerInlineArtifact(
$pkg_name,
$config['artifact'],
$registry_name,
"inline in {$file}"
);
}
2025-11-30 15:35:04 +08:00
}
return $file;
2025-11-30 15:35:04 +08:00
}
2026-02-02 13:32:35 +08:00
public static function loadFromArray(array $data, string $registry_name): void
{
ConfigValidator::validateAndLintPackages('array_input', $data);
foreach ($data as $pkg_name => $config) {
self::$package_configs[$pkg_name] = $config;
Registry::_bindPackageConfigFile($pkg_name, $registry_name, 'array_input');
// Register inline artifact if present
if (isset($config['artifact']) && is_array($config['artifact'])) {
ArtifactConfig::registerInlineArtifact(
$pkg_name,
$config['artifact'],
$registry_name,
'inline in array_input'
);
}
}
}
2025-11-30 15:35:04 +08:00
/**
* Check if a package configuration exists.
*/
public static function isPackageExists(string $pkg_name): bool
{
return isset(self::$package_configs[$pkg_name]);
}
public static function getAll(): array
{
return self::$package_configs;
}
/**
* Get a specific field from a package configuration.
*
* @param string $pkg_name Package name
* @param null|string $field_name Package config field name
* @param null|mixed $default Default value if field not found
* @return mixed The value of the specified field or the default value
*/
public static function get(string $pkg_name, ?string $field_name = null, mixed $default = null): mixed
{
if (!self::isPackageExists($pkg_name)) {
return $default;
}
// use suffixes to find field
$suffixes = match (SystemTarget::getTargetOS()) {
'Windows' => ['@windows', ''],
'Darwin' => ['@macos', '@unix', ''],
'Linux' => ['@linux', '@unix', ''],
'BSD' => ['@freebsd', '@bsd', '@unix', ''],
};
if ($field_name === null) {
return self::$package_configs[$pkg_name];
}
if (in_array($field_name, ConfigValidator::SUFFIX_ALLOWED_FIELDS)) {
foreach ($suffixes as $suffix) {
$suffixed_field = $field_name . $suffix;
if (isset(self::$package_configs[$pkg_name][$suffixed_field])) {
return self::$package_configs[$pkg_name][$suffixed_field];
}
}
return $default;
}
return self::$package_configs[$pkg_name][$field_name] ?? $default;
}
}