mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-08 01:15:37 +08:00
Separate package config
This commit is contained in:
@@ -77,4 +77,19 @@ class ArtifactConfig
|
||||
{
|
||||
return self::$artifact_configs[$artifact_name] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an inline artifact configuration.
|
||||
* Used when artifact is defined inline within a package configuration.
|
||||
*
|
||||
* @param string $artifact_name Artifact name (usually same as package name)
|
||||
* @param array $config Artifact configuration
|
||||
* @param string $registry_name Registry name
|
||||
* @param string $source_info Source info for debugging
|
||||
*/
|
||||
public static function registerInlineArtifact(string $artifact_name, array $config, string $registry_name, string $source_info = 'inline'): void
|
||||
{
|
||||
self::$artifact_configs[$artifact_name] = $config;
|
||||
Registry::_bindArtifactConfigFile($artifact_name, $registry_name, $source_info);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class ConfigValidator
|
||||
'type' => ConfigType::STRING,
|
||||
'depends' => ConfigType::LIST_ARRAY, // @
|
||||
'suggests' => ConfigType::LIST_ARRAY, // @
|
||||
'artifact' => ConfigType::STRING,
|
||||
'artifact' => [self::class, 'validateArtifactField'], // STRING or OBJECT
|
||||
'license' => [ConfigType::class, 'validateLicenseField'],
|
||||
'lang' => ConfigType::STRING,
|
||||
'frameworks' => ConfigType::LIST_ARRAY, // @
|
||||
@@ -102,7 +102,14 @@ class ConfigValidator
|
||||
if (!is_array($data)) {
|
||||
throw new ValidationException("{$config_file_name} is broken");
|
||||
}
|
||||
|
||||
// Define allowed artifact fields
|
||||
$allowed_artifact_fields = ['source', 'source-mirror', 'binary', 'binary-mirror', 'metadata'];
|
||||
|
||||
foreach ($data as $name => $artifact) {
|
||||
// First pass: validate unknown fields
|
||||
self::validateNoInvalidFields('artifact', $name, $artifact, $allowed_artifact_fields);
|
||||
|
||||
foreach ($artifact as $k => $v) {
|
||||
// check source field
|
||||
if ($k === 'source' || $k === 'source-mirror') {
|
||||
@@ -202,6 +209,11 @@ class ConfigValidator
|
||||
throw new ValidationException("Package [{$name}] in {$config_file_name} of type '{$pkg['type']}' must have an 'artifact' field");
|
||||
}
|
||||
|
||||
// validate and lint inline artifact object if present
|
||||
if (isset($pkg['artifact']) && is_array($pkg['artifact'])) {
|
||||
self::validateAndLintInlineArtifact($name, $data[$name]['artifact']);
|
||||
}
|
||||
|
||||
// check if "php-extension" package has php-extension specific fields and validate inside
|
||||
if ($pkg['type'] === 'php-extension') {
|
||||
self::validatePhpExtensionFields($name, $pkg);
|
||||
@@ -234,6 +246,19 @@ class ConfigValidator
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate artifact field - can be string (reference) or object (inline).
|
||||
*
|
||||
* @param mixed $value Field value
|
||||
*/
|
||||
public static function validateArtifactField(mixed $value): bool
|
||||
{
|
||||
if (!is_string($value) && !is_assoc_array($value)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate an artifact download object field.
|
||||
*
|
||||
@@ -373,4 +398,19 @@ class ConfigValidator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate and lint inline artifact object structure.
|
||||
*
|
||||
* @param string $pkg_name Package name
|
||||
* @param array $artifact Inline artifact configuration (passed by reference to apply linting)
|
||||
*/
|
||||
private static function validateAndLintInlineArtifact(string $pkg_name, array &$artifact): void
|
||||
{
|
||||
// Validate and lint as if it's a standalone artifact
|
||||
$temp_data = [$pkg_name => $artifact];
|
||||
self::validateAndLintArtifacts("inline artifact in package '{$pkg_name}'", $temp_data);
|
||||
// Write back the linted artifact configuration
|
||||
$artifact = $temp_data[$pkg_name];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ class PackageConfig
|
||||
throw new WrongUsageException("Directory {$dir} does not exist, cannot load pkg.json config.");
|
||||
}
|
||||
$loaded = [];
|
||||
$files = glob("{$dir}/pkg.*.json");
|
||||
$files = glob("{$dir}/*");
|
||||
if (is_array($files)) {
|
||||
foreach ($files as $file) {
|
||||
self::loadFromFile($file, $registry_name);
|
||||
@@ -58,10 +58,39 @@ class PackageConfig
|
||||
foreach ($data as $pkg_name => $config) {
|
||||
self::$package_configs[$pkg_name] = $config;
|
||||
Registry::_bindPackageConfigFile($pkg_name, $registry_name, $file);
|
||||
|
||||
// Register inline artifact if present
|
||||
if (isset($config['artifact']) && is_array($config['artifact'])) {
|
||||
ArtifactConfig::registerInlineArtifact(
|
||||
$pkg_name,
|
||||
$config['artifact'],
|
||||
$registry_name,
|
||||
"inline in {$file}"
|
||||
);
|
||||
}
|
||||
}
|
||||
return $file;
|
||||
}
|
||||
|
||||
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'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a package configuration exists.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user