Files
tcpserver-flow/app/flow/strategies/AbstractStrategy.php
T
2026-03-08 22:58:56 +08:00

78 lines
1.6 KiB
PHP

<?php
namespace app\flow\strategies;
use app\flow\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;
}
}