mirror of
https://github.com/kmvan/x-prober.git
synced 2026-08-13 00:37:08 +08:00
use Zustand instead of MobX
change xconfig.json to xconfig.toml
This commit is contained in:
@@ -1,59 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace InnStudio\Prober\Compiler;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Class ConfigGeneration
|
||||
* 负责解析 JSON 配置文件并将其转换为 PHP 类文件,同时同步到开发路径。
|
||||
* Responsible for parsing JSON config and converting it to a PHP class file, while syncing it to the dev path.
|
||||
*/
|
||||
final class ConfigGeneration
|
||||
{
|
||||
private $phpConfigPath = '';
|
||||
/**
|
||||
* 使用 PHP 8.0+ 构造函数属性提升 / Use PHP 8.0+ Constructor Property Promotion.
|
||||
*/
|
||||
public function __construct(
|
||||
private string $phpConfigPath,
|
||||
private string $configPath,
|
||||
private string $configPathDev
|
||||
) {
|
||||
}
|
||||
|
||||
private $configPath = '';
|
||||
|
||||
private $configPathDev = '';
|
||||
|
||||
public function __construct(array $args)
|
||||
/**
|
||||
* 执行配置生成与复制逻辑
|
||||
* Execute configuration generation and copying logic.
|
||||
*
|
||||
* @throws InvalidArgumentException 当主配置文件不存在时抛出 | Thrown when the main config file does not exist
|
||||
* @throws RuntimeException 当生成或复制失败时抛出 | Thrown when generation or copying fails
|
||||
*/
|
||||
public function generate(): void
|
||||
{
|
||||
[
|
||||
'phpConfigPath' => $this->phpConfigPath,
|
||||
'configPath' => $this->configPath,
|
||||
'configPathDev' => $this->configPathDev,
|
||||
] = $args;
|
||||
|
||||
// 验证主配置文件是否存在 / Validate if the main config file exists
|
||||
if ( ! is_file($this->configPath)) {
|
||||
$this->die("File invalid: {$this->configPath}");
|
||||
throw new InvalidArgumentException("[ConfigGeneration] File invalid: {$this->configPath}");
|
||||
}
|
||||
|
||||
if ( ! $this->genPhpConfig()) {
|
||||
$this->die('Error: can not generate content to dist.');
|
||||
}
|
||||
// 生成 PHP 配置文件 / Generate the PHP configuration file
|
||||
$this->genPhpConfig();
|
||||
|
||||
// 备份/同步配置文件到开发环境 / Copy/sync config file to dev environment
|
||||
$this->copyConfigToTmp();
|
||||
|
||||
$this->die('PHP config file generated successful.', false);
|
||||
echo "[ConfigGeneration] PHP config file generated successfully.\n";
|
||||
}
|
||||
|
||||
private function copyConfigToTmp(): bool
|
||||
/**
|
||||
* 复制配置文件到开发/临时路径
|
||||
* Copy the configuration file to the dev/temporary path.
|
||||
*/
|
||||
private function copyConfigToTmp(): void
|
||||
{
|
||||
return copy($this->configPath, $this->configPathDev);
|
||||
if ( ! copy($this->configPath, $this->configPathDev)) {
|
||||
throw new RuntimeException("[ConfigGeneration] Failed to copy config to dev path: {$this->configPathDev}");
|
||||
}
|
||||
}
|
||||
|
||||
private function genPhpConfig(): bool
|
||||
/**
|
||||
* 解析 JSON 并生成对应的 PHP 配置类
|
||||
* Parse JSON and generate the corresponding PHP configuration class.
|
||||
*/
|
||||
private function genPhpConfig(): void
|
||||
{
|
||||
$config = file_get_contents($this->configPath) ?: '';
|
||||
$configRaw = file_get_contents($this->configPath);
|
||||
|
||||
if ( ! $config) {
|
||||
return false;
|
||||
if (false === $configRaw || '' === $configRaw) {
|
||||
throw new RuntimeException("[ConfigGeneration] Failed to read or empty config file: {$this->configPath}");
|
||||
}
|
||||
|
||||
$config = json_decode($config, true);
|
||||
// 解析 JSON 数据 / Parse JSON data
|
||||
$configData = json_decode($configRaw, true);
|
||||
|
||||
if ( ! $config) {
|
||||
return false;
|
||||
if (null === $configData && \JSON_ERROR_NONE !== json_last_error()) {
|
||||
throw new RuntimeException('[ConfigGeneration] JSON decode error: ' . json_last_error_msg());
|
||||
}
|
||||
|
||||
$config = var_export($config, true);
|
||||
// 将数组转换为一等 PHP 代码格式 / Export array into clean PHP code format
|
||||
$exportedConfig = var_export($configData, true);
|
||||
|
||||
$configContent = <<<PHP
|
||||
<?php
|
||||
/**
|
||||
* 该文件是自动生成的。
|
||||
* The file is automatically generated.
|
||||
*/
|
||||
|
||||
@@ -61,22 +91,14 @@ namespace InnStudio\\Prober\\Components\\Config;
|
||||
|
||||
class ConfigApi
|
||||
{
|
||||
public static \$config = {$config};
|
||||
public static \$config = {$exportedConfig};
|
||||
}
|
||||
|
||||
PHP;
|
||||
|
||||
return (bool) file_put_contents($this->phpConfigPath, $configContent);
|
||||
}
|
||||
|
||||
private function die(string $msg, bool $die = true): void
|
||||
{
|
||||
$msg = "[StyleGeneration] {$msg}\n";
|
||||
|
||||
if ($die) {
|
||||
exit($msg);
|
||||
// 写入 PHP 文件 / Write to the PHP file
|
||||
if (false === file_put_contents($this->phpConfigPath, $configContent)) {
|
||||
throw new RuntimeException("[ConfigGeneration] Error: cannot write generated content to {$this->phpConfigPath}");
|
||||
}
|
||||
|
||||
echo $msg;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user