Files
2026-03-08 22:58:56 +08:00

290 lines
7.1 KiB
PHP

<?php
namespace app\flow\config;
/**
* 流程配置类
* 支持动态配置流程步骤、策略和语音
*/
class ProcessConfig extends AbstractConfig
{
/**
* 步骤集合配置
*/
protected StepsConfig $stepsConfig;
/**
* 晨洗配置
*/
protected MorningWashConfig $morningWashConfig;
/**
* 时间验证配置
*/
protected TimeValidationConfig $timeValidationConfig;
/**
* 语音模板配置
*/
protected VoiceTemplatesConfig $voiceTemplatesConfig;
/**
* 构造函数
*
* @param array $config 原始配置数组(向下兼容)
*/
public function __construct(array $config = [])
{
$this->log("ProcessConfig 初始化中...");
$this->stepsConfig = StepsConfig::create($config['steps'] ?? [], $config['override_steps'] ?? true);
$this->morningWashConfig = MorningWashConfig::create($config['morning_wash'] ?? []);
$this->timeValidationConfig = TimeValidationConfig::create($config['time_validation'] ?? []);
$this->voiceTemplatesConfig = VoiceTemplatesConfig::create($config['voice_templates'] ?? []);
// node_status 覆盖 steps 中的 enabled
foreach (($config['node_status'] ?? []) as $code => $enabled) {
$this->stepsConfig->setEnabled($code, (bool)$enabled);
}
$this->log("ProcessConfig 初始化完成");
}
// ---- 步骤配置 ----
public function getStepsConfig(): StepsConfig
{
return $this->stepsConfig;
}
/**
* 获取所有步骤(StepConfig[])—— 兼容 ProcessEngine 调用
*/
public function getSteps(): array
{
return $this->stepsConfig->all();
}
/**
* 获取启用的步骤
*/
public function getEnabledSteps(): array
{
return $this->stepsConfig->enabled();
}
/**
* 获取步骤配置
*/
public function getStep(string $code): ?StepConfig
{
return $this->stepsConfig->find($code);
}
/**
* 添加步骤
*/
public function addStep(string $code, string $class, bool $enabled = true, array $expand = []): self
{
$this->stepsConfig->add($code, $class, $enabled, $expand);
return $this;
}
/**
* 移除步骤
*/
public function removeStep(string $code): self
{
$this->stepsConfig->remove($code);
return $this;
}
/**
* 设置节点启用状态
*/
public function setNodeEnabled(string $code, bool $enabled): self
{
$this->stepsConfig->setEnabled($code, $enabled);
return $this;
}
/**
* 检查节点是否启用
*/
public function isNodeEnabled(string $code): bool
{
return $this->stepsConfig->find($code)?->enabled ?? true;
}
/**
* 跳过某个步骤(禁用节点)
*/
public function skipStep(string $code): self
{
return $this->setNodeEnabled($code, false);
}
/**
* 启用某个步骤
*/
public function enableStep(string $code): self
{
return $this->setNodeEnabled($code, true);
}
// ---- 晨洗配置 ----
public function getMorningWashConfig(): MorningWashConfig
{
return $this->morningWashConfig;
}
public function setMorningWashConfig(MorningWashConfig $config): self
{
$this->morningWashConfig = $config;
return $this;
}
/**
* 快捷设置晨洗模式(支持字符串或枚举)
*/
public function setMorningWashMode(string|MorningMode $mode): self
{
if (is_string($mode)) {
$mode = MorningMode::from_name($mode) ?? MorningMode::DailyFirst;
}
$this->morningWashConfig->mode = $mode;
return $this;
}
// ---- 时间验证配置 ----
public function getTimeValidationConfig(): TimeValidationConfig
{
return $this->timeValidationConfig;
}
public function setTimeValidationConfig(TimeValidationConfig $config): self
{
$this->timeValidationConfig = $config;
return $this;
}
/**
* 设置步骤时长(秒)
*/
public function setStepDuration(string $stepCode, int $seconds): self
{
$this->timeValidationConfig->setDuration($stepCode, $seconds);
return $this;
}
// ---- 语音模板配置 ----
public function getVoiceTemplatesConfig(): VoiceTemplatesConfig
{
return $this->voiceTemplatesConfig;
}
public function setVoiceTemplatesConfig(VoiceTemplatesConfig $config): self
{
$this->voiceTemplatesConfig = $config;
return $this;
}
/**
* 设置指定步骤的自定义语音
*/
public function setStepVoice(string $templateKey, string $stepCode, string $voice): self
{
$this->voiceTemplatesConfig->setStepVoice($templateKey, $stepCode, $voice);
return $this;
}
// ---- 序列化 ----
public function toArray(): array
{
return [
'steps' => $this->stepsConfig->toArray(),
'morning_wash' => $this->morningWashConfig->toArray(),
'time_validation' => $this->timeValidationConfig->toArray(),
'voice_templates' => $this->voiceTemplatesConfig->toArray(),
];
}
public function saveToFile(string $filePath): bool
{
$content = "<?php\nreturn " . var_export($this->toArray(), true) . ";\n";
return file_put_contents($filePath, $content) !== false;
}
// ---- 静态工厂 ----
public static function fromArray(array $config): self
{
return new self($config);
}
public static function fromFile(string $filePath): self
{
if (!file_exists($filePath)) {
return new self();
}
$config = require $filePath;
return new self($config);
}
public static function createStandard(): self
{
return new self();
}
public static function createNoMorningWash(): self
{
$config = new self();
$config->setMorningWashMode(MorningMode::None);
$config->skipStep('晨洗');
return $config;
}
public static function createSimple(): self
{
$config = new self();
$config->setMorningWashMode(MorningMode::None);
$config->skipStep('漂洗');
$config->skipStep('消毒');
$config->skipStep('终末漂洗');
$config->skipStep('干燥');
return $config;
}
public static function createMachineWash(): self
{
$config = new self();
$config->setMorningWashMode(MorningMode::None);
$config->skipStep('漂洗');
$config->skipStep('消毒');
return $config;
}
public static function createNoDry(): self
{
$config = new self();
$config->skipStep('干燥');
return $config;
}
public static function createDryOnly(): self
{
$config = new self();
$config->setMorningWashMode(MorningMode::None);
$config->skipStep('晨洗');
$config->skipStep('清洗');
$config->skipStep('漂洗');
$config->skipStep('消毒');
$config->skipStep('终末漂洗');
return $config;
}
}