mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-07 08:45:40 +08:00
Implement caching for config file parsing to improve performance
This commit is contained in:
@@ -42,13 +42,22 @@ class ArtifactConfig
|
||||
if ($content === false) {
|
||||
throw new WrongUsageException("Failed to read artifact config file: {$file}");
|
||||
}
|
||||
$data = match (pathinfo($file, PATHINFO_EXTENSION)) {
|
||||
'json' => json_decode($content, true),
|
||||
'yml', 'yaml' => Yaml::parse($content),
|
||||
default => throw new WrongUsageException("Unsupported artifact config file format: {$file}"),
|
||||
};
|
||||
if (!is_array($data)) {
|
||||
throw new WrongUsageException("Invalid JSON format in artifact config file: {$file}");
|
||||
// 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 artifact config file format: {$file}"),
|
||||
};
|
||||
if (!is_array($data)) {
|
||||
throw new WrongUsageException("Invalid JSON format in artifact config file: {$file}");
|
||||
}
|
||||
if (is_array($data)) {
|
||||
ConfigCache::set($content, $data);
|
||||
}
|
||||
}
|
||||
ConfigValidator::validateAndLintArtifacts(basename($file), $data);
|
||||
foreach ($data as $artifact_name => $config) {
|
||||
@@ -68,6 +77,16 @@ class ArtifactConfig
|
||||
return self::$artifact_configs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore artifact configs from cache without re-parsing YAML files.
|
||||
*
|
||||
* @internal used by Registry cache layer only
|
||||
*/
|
||||
public static function _restoreFromCache(array $configs): void
|
||||
{
|
||||
self::$artifact_configs = array_merge(self::$artifact_configs, $configs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration for a specific artifact by name.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user