ai-refactor(flow): 调整抽象流程节点实现和依赖路径
- 修改 AbstractProcessNode 中 ProcessContext 的命名空间引用为 app\flow\context\ProcessContext - 引入 app\flow\vo\CanHandleResult 用于节点处理结果表示 - 更新前置策略执行后对成功状态的判断,改为调用 isSuccess() 方法 - 增加日志记录细节,便于调试策略执行中断时的错误信息 - 优化代码注释,提升代码可读性和维护性
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
namespace app\flow\nodes;
|
||||
|
||||
use app\flow\config\StepConfig;
|
||||
use app\flow\ProcessContext;
|
||||
use app\flow\context\ProcessContext;
|
||||
use app\flow\strategies\ProcessStrategyInterface;
|
||||
use app\flow\vo\CanHandleResult;
|
||||
use app\utils\Logger;
|
||||
|
||||
/**
|
||||
@@ -67,26 +68,33 @@ abstract class AbstractProcessNode implements ProcessNodeInterface
|
||||
$context = $this->executeBeforeStrategies($context);
|
||||
|
||||
// 如果前置策略返回错误,不再继续
|
||||
if (!$context->success) {
|
||||
if (!$context->isSuccess()) {
|
||||
Logger::debug('[{}-Node] 前置策略拦截 error={}', [
|
||||
$this->getCode(),
|
||||
$context->errorMessage,
|
||||
$context->getVoice()->errorMessage,
|
||||
]);
|
||||
return $context;
|
||||
}
|
||||
|
||||
|
||||
// 如果不能处理当前步骤,传递给下一个节点
|
||||
if (!$this->canHandle($context)) {
|
||||
$canHandleResult = $this->canHandle($context);
|
||||
if (!$canHandleResult->canHandle) {
|
||||
Logger::debug('[{}-Node] 不能处理当前步骤,跳过', [$this->getCode()]);
|
||||
// 如果有期望下一步提示,设置到上下文
|
||||
if ($canHandleResult->hasExpectedNextStep()) {
|
||||
$context = $context->builder()
|
||||
->expectedNextStep($canHandleResult->expectedNextStep)
|
||||
->build();
|
||||
}
|
||||
return $this->passToNext($context);
|
||||
}
|
||||
|
||||
// 输出当前节点
|
||||
Logger::debug('[{}-Node] 开始处理 step={} batch={}', [
|
||||
$this->getCode(),
|
||||
$context->currentStep,
|
||||
$context->batchNo ?: '-',
|
||||
$context->getCurrentStep(),
|
||||
$context->getBatchNo() ?: '-',
|
||||
]);
|
||||
|
||||
|
||||
@@ -95,25 +103,25 @@ abstract class AbstractProcessNode implements ProcessNodeInterface
|
||||
|
||||
Logger::debug('[{}-Node] 处理完成 step={} batch={} success={}', [
|
||||
$this->getCode(),
|
||||
$context->currentStep,
|
||||
$context->batchNo ?: '-',
|
||||
$context->success,
|
||||
$context->getCurrentStep(),
|
||||
$context->getBatchNo() ?: '-',
|
||||
$context->isSuccess(),
|
||||
]);
|
||||
|
||||
// 执行后置策略
|
||||
$context = $this->executeAfterStrategies($context);
|
||||
// 后置策略拦截
|
||||
if (!$context->success) {
|
||||
if (!$context->isSuccess()) {
|
||||
Logger::debug('[{}-Node] 后置策略拦截 error={}', [
|
||||
$this->getCode(),
|
||||
$context->errorMessage,
|
||||
$context->getVoice()->errorMessage,
|
||||
]);
|
||||
return $context;
|
||||
}
|
||||
|
||||
$nextNode = $this->getNext();
|
||||
// 跳过节点逻辑
|
||||
for ($i = 0; $i < $context->skipNodeCount; $i++) {
|
||||
for ($i = 0; $i < $context->getResult()->skipNodeCount; $i++) {
|
||||
Logger::debug('[{}-Node] 跳过节点 code={}', [$this->getCode(), $nextNode->getCode()]);
|
||||
$nextNode = $nextNode->getNext();
|
||||
}
|
||||
@@ -146,7 +154,7 @@ abstract class AbstractProcessNode implements ProcessNodeInterface
|
||||
*/
|
||||
public function isMatchReaderType(ProcessContext $context): bool
|
||||
{
|
||||
return $this->getCode() === $context->readerType;
|
||||
return $this->getCode() === $context->getReader()->type;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,8 +173,9 @@ abstract class AbstractProcessNode implements ProcessNodeInterface
|
||||
*/
|
||||
protected function stopNext(ProcessContext $context): ProcessContext
|
||||
{
|
||||
$context->skipNodeCount = count($this->getRemainingNodes());
|
||||
return $context;
|
||||
return $context->builder()
|
||||
->skipNodeCount(count($this->getRemainingNodes()))
|
||||
->build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,7 +223,7 @@ abstract class AbstractProcessNode implements ProcessNodeInterface
|
||||
foreach ($this->strategies as $strategy) {
|
||||
if ($strategy->getPhase() === 'before') {
|
||||
$context = $strategy->execute($context, $this);
|
||||
if (!$context->success) {
|
||||
if (!$context->isSuccess()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -287,10 +296,15 @@ abstract class AbstractProcessNode implements ProcessNodeInterface
|
||||
/**
|
||||
* 判断当前节点是否能处理该步骤
|
||||
* 默认实现:检查当前步骤是否匹配节点编码
|
||||
*
|
||||
* @return CanHandleResult 包含是否能处理以及期望下一步的结果对象
|
||||
*/
|
||||
public function canHandle(ProcessContext $context): bool
|
||||
public function canHandle(ProcessContext $context): CanHandleResult
|
||||
{
|
||||
return $context->currentStep === $this->getCode();
|
||||
if ($context->getCurrentStep() === $this->getCode()) {
|
||||
return CanHandleResult::yes();
|
||||
}
|
||||
return CanHandleResult::no();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user