mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-07 08:45:40 +08:00
v3 base
This commit is contained in:
68
src/StaticPHP/Config/ArtifactConfig.php
Normal file
68
src/StaticPHP/Config/ArtifactConfig.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace StaticPHP\Config;
|
||||
|
||||
use StaticPHP\Exception\WrongUsageException;
|
||||
|
||||
class ArtifactConfig
|
||||
{
|
||||
private static array $artifact_configs = [];
|
||||
|
||||
public static function loadFromDir(string $dir): void
|
||||
{
|
||||
if (!is_dir($dir)) {
|
||||
throw new WrongUsageException("Directory {$dir} does not exist, cannot load artifact config.");
|
||||
}
|
||||
$files = glob("{$dir}/artifact.*.json");
|
||||
if (is_array($files)) {
|
||||
foreach ($files as $file) {
|
||||
self::loadFromFile($file);
|
||||
}
|
||||
}
|
||||
if (file_exists("{$dir}/artifact.json")) {
|
||||
self::loadFromFile("{$dir}/artifact.json");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load artifact configurations from a specified JSON file.
|
||||
*/
|
||||
public static function loadFromFile(string $file): void
|
||||
{
|
||||
$content = file_get_contents($file);
|
||||
if ($content === false) {
|
||||
throw new WrongUsageException("Failed to read artifact config file: {$file}");
|
||||
}
|
||||
$data = json_decode($content, true);
|
||||
if (!is_array($data)) {
|
||||
throw new WrongUsageException("Invalid JSON format in artifact config file: {$file}");
|
||||
}
|
||||
ConfigValidator::validateAndLintArtifacts(basename($file), $data);
|
||||
foreach ($data as $artifact_name => $config) {
|
||||
self::$artifact_configs[$artifact_name] = $config;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all loaded artifact configurations.
|
||||
*
|
||||
* @return array<string, array> an associative array of artifact configurations
|
||||
*/
|
||||
public static function getAll(): array
|
||||
{
|
||||
return self::$artifact_configs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration for a specific artifact by name.
|
||||
*
|
||||
* @param string $artifact_name the name of the artifact
|
||||
* @return null|array the configuration array for the specified artifact, or null if not found
|
||||
*/
|
||||
public static function get(string $artifact_name): ?array
|
||||
{
|
||||
return self::$artifact_configs[$artifact_name] ?? null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user