e040fccba6
- 修改 AbstractProcessNode 中 ProcessContext 的命名空间引用为 app\flow\context\ProcessContext - 引入 app\flow\vo\CanHandleResult 用于节点处理结果表示 - 更新前置策略执行后对成功状态的判断,改为调用 isSuccess() 方法 - 增加日志记录细节,便于调试策略执行中断时的错误信息 - 优化代码注释,提升代码可读性和维护性
78 lines
1.6 KiB
PHP
78 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace app\flow\strategies;
|
|
|
|
use app\flow\context\ProcessContext;
|
|
use app\flow\nodes\ProcessNodeInterface;
|
|
|
|
/**
|
|
* 策略抽象基类
|
|
*/
|
|
abstract class AbstractStrategy implements ProcessStrategyInterface
|
|
{
|
|
/**
|
|
* 策略配置
|
|
*/
|
|
protected array $config = [];
|
|
|
|
/**
|
|
* 策略执行阶段
|
|
*/
|
|
protected string $phase = 'before';
|
|
|
|
public function __construct(array $config = [])
|
|
{
|
|
$this->config = array_merge($this->config, $config);
|
|
}
|
|
|
|
/**
|
|
* 执行策略
|
|
*/
|
|
public function execute(ProcessContext $context, ProcessNodeInterface $node): ProcessContext
|
|
{
|
|
if (!$this->isApplicable($context, $node)) {
|
|
return $context;
|
|
}
|
|
return $this->doExecute($context, $node);
|
|
}
|
|
|
|
/**
|
|
* 具体执行逻辑,由子类实现
|
|
*/
|
|
abstract protected function doExecute(ProcessContext $context, ProcessNodeInterface $node): ProcessContext;
|
|
|
|
/**
|
|
* 获取策略执行阶段
|
|
*/
|
|
public function getPhase(): string
|
|
{
|
|
return $this->phase;
|
|
}
|
|
|
|
/**
|
|
* 设置执行阶段
|
|
*/
|
|
public function setPhase(string $phase): self
|
|
{
|
|
$this->phase = $phase;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 判断策略是否适用
|
|
* 默认总是适用
|
|
*/
|
|
public function isApplicable(ProcessContext $context, ProcessNodeInterface $node): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取配置
|
|
*/
|
|
protected function getConfig(string $key, mixed $default = null): mixed
|
|
{
|
|
return $this->config[$key] ?? $default;
|
|
}
|
|
}
|