feat: 实现TCP Server
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
<?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 {
|
||||
$this->steps = array_merge(self::defaultSteps(), $parsed);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从原始配置数组创建
|
||||
* 读取 $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('存储', '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'];
|
||||
$expand = array_diff_key($step, array_flip($known));
|
||||
return new StepConfig(
|
||||
$step['code'],
|
||||
$step['class'],
|
||||
$step['enabled'] ?? true,
|
||||
$expand
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user