phlllpe/

chain

创建责任链的库

v1.0.0 2021-12-27 22:17 UTC

This package is auto-updated.

Last update: 2024-08-28 04:08:11 UTC


README

Scrutinizer Code Quality Code Coverage Build Status Code Intelligence Status

如何安装

composer req phlllpe/chain

用法

  • 创建处理器,示例
namespace Any\Handler;

use Chain\AbstractHandler;
use Chain\ContextInterface;

class MyHandler extends AbstractHandler 
{
    public function handle(ContextInterface $context)
    {
        // TO DO ANY ACTION HERE, WITH ANY TEST
        if ($context->get('any') == 'HERE') {
            $context->set('myClass', static::class);
        }
        
        return parent::handle($context);
    }
}
  • 定义一个序列,使用处理器设置/分析你的上下文;
namespace Any\Service;

use Chain\Context;
use Any\Handler\{
    MainHandler,
    AddressHandler,
    BudgetHandler,
    FamilyHandler
};

class MyService
{

    public function __invoke()
    {
        $context = new Context();
        $context->set('name', 'Philipe Fernandes');
        
        $mainHandler = new MainHandler();
        $addressHandler = new AddressHandler();
        $budgetHandler = new BudgetHandler();
        $familyHandler = new FamilyHandler();
    
        $mainHandler
            ->setNext($addressHandler)
            ->setNext($budgetHandler)
            ->setNext($familyHandler)
            
        (new Runner())->run($mainHandler, $context);
    }
}