Files

178 lines
5.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\flow\config;
/**
* 步骤集合配置类
* 持有完整的流程步骤列表,并封装 override_steps 合并逻辑
*/
class StepsConfig extends AbstractConfig
{
/**
* 步骤列表(StepConfig[]
*/
public array $steps {
get => $this->steps;
}
/**
* @param array $rawSteps 来自配置文件的原始步骤数组(每项为 ['code'=>..., 'class'=>..., 'enabled'=>...]
* @param bool $override true=完全替换默认步骤;false=追加到默认步骤之后
*/
public function __construct(array $rawSteps = [], bool $override = true)
{
$parsed = array_map(fn(array $s) => self::createStep($s), $rawSteps);
// 无自定义步骤时始终使用默认列表,不受 override 影响
if (empty($parsed)) {
$this->steps = self::defaultSteps();
} elseif ($override) {
$this->steps = $parsed;
} else {
// 核心逻辑:合并默认步骤 + 自定义步骤,重复 code 以自定义步骤为准
// 1. 将默认步骤转为「code => StepConfig」的关联数组(方便快速查找/覆盖)
$defaultStepsMap = [];
foreach (self::defaultSteps() as $defaultStep) {
$defaultStepsMap[$defaultStep->code] = $defaultStep;
}
// 2. 遍历自定义步骤,覆盖默认步骤中相同 code 的元素
foreach ($parsed as $customStep) {
$defaultStepsMap[$customStep->code] = $customStep;
}
// 3. 转回索引数组(重置键名从0开始),赋值给 $this->steps
$this->steps = array_values($defaultStepsMap);
}
}
/**
* 从原始配置数组创建
* 读取 $config['steps'] 与 $config['override_steps']
*/
public static function create(array $config = [], bool $override = true): self
{
$instance = new self($config ?? [], $override);
$instance->log("加载[steps]步骤配置,来源: {}", [empty($config) ? "默认配置" : "自定义配置"]);
return $instance;
}
// ---- 默认步骤 ----
/**
* 标准手工洗流程默认步骤列表
*
* @return StepConfig[]
*/
public static function defaultSteps(): array
{
return [
new StepConfig('晨洗', 'MorningWashNode'),
new StepConfig('重复刷卡', 'DuplicateCheckNode'),
new StepConfig('清洗', 'WashNode'),
new StepConfig('漂洗', 'RinseNode'),
new StepConfig('消毒', 'DisinfectNode'),
new StepConfig('终末漂洗', 'FinalRinseNode'),
new StepConfig('干燥', 'DryNode'),
new StepConfig('结束', 'EndNode'),
new StepConfig('机洗', 'MachineWashNode'),
new StepConfig('虚拟清洗机', 'VirtualWashMachineNode'),
new StepConfig('存储', 'StorageNode'),
new StepConfig('内镜放入', 'StorageInNode'),
new StepConfig('内镜取出', 'StorageOutNode'),
new StepConfig('Close', 'CloseNode'),
];
}
// ---- 步骤查询 ----
/**
* 获取所有步骤
*
* @return StepConfig[]
*/
public function all(): array
{
return $this->steps;
}
/**
* 获取启用的步骤
*
* @return StepConfig[]
*/
public function enabled(): array
{
return array_values(array_filter($this->steps, fn(StepConfig $s) => $s->enabled));
}
/**
* 按 code 查找步骤
*/
public function find(string $code): ?StepConfig
{
foreach ($this->steps as $step) {
if ($step->code === $code) {
return $step;
}
}
return null;
}
// ---- 步骤修改 ----
/**
* 追加一个步骤
*/
public function add(string $code, string $class, bool $enabled = true, array $expand = []): self
{
$this->steps[] = new StepConfig($code, $class, $enabled, $expand);
return $this;
}
/**
* 移除步骤
*/
public function remove(string $code): self
{
$steps = array_values(array_filter($this->steps, fn(StepConfig $s) => $s->code !== $code));
$this->steps = $steps;
return $this;
}
/**
* 设置节点启用状态
*/
public function setEnabled(string $code, bool $enabled): self
{
foreach ($this->steps as $step) {
if ($step->code === $code) {
$step->enabled = $enabled;
}
}
return $this;
}
public function toArray(): array
{
return array_map(fn(StepConfig $s) => $s->toArray(), $this->steps);
}
// ---- 内部工厂 ----
/**
* 从数组创建单个步骤(兼容旧格式 ['code'=>..., 'class'=>..., 'enabled'=>...]
*/
private static function createStep(array $step): StepConfig
{
$known = ['code', 'class', 'enabled', 'required'];
$expand = array_diff_key($step, array_flip($known));
return new StepConfig(
$step['code'],
$step['class'],
$step['enabled'] ?? true,
$step['required'] ?? [],
$expand
);
}
}