gameplayjdk / task-chain
适用于任务链模式的轻量级库。
v1.0.1
2020-03-02 18:58 UTC
Requires
- php: >= 7.1
This package is auto-updated.
Last update: 2024-08-29 04:55:01 UTC
README
适用于任务链模式的轻量级库。
该模式允许以清晰易懂且可扩展的方式构建和定义顺序任务。
安装
composer require gameplayjdk/task-chain
用法
<?php require __DIR__ . '/vendor/autoload.php'; use TaskChain\TaskChain; use TaskChain\TaskInterface; $chain = new TaskChain(); /** * Class TaskAdd */ class TaskAdd implements TaskInterface { /** * @var int */ private $number; /** * TaskAdd constructor. * @param int $number */ public function __construct(int $number) { $this->number = $number; } /** * @inheritDoc */ public function accept($input): bool { return is_int($input); } /** * @inheritDoc */ public function process($input, callable $nextCallback) { echo print_r($this, true); echo PHP_EOL; return $nextCallback($input + $this->number); } } /** * Class TaskMul */ class TaskMul implements TaskInterface { /** * @var int */ private $number; /** * TaskMul constructor. * @param int $number */ public function __construct(int $number) { $this->number = $number; } /** * @inheritDoc */ public function accept($input): bool { return is_int($input); } /** * @inheritDoc */ public function process($input, callable $nextCallback) { echo print_r($this, true); echo PHP_EOL; return $nextCallback($input * $this->number); } } // Add "1", priority 2. $chain->add(new TaskAdd(1), 2); // Multiply by "2", priority 3. $chain->add(new TaskMul(2), 3); // Once a chain is ran, the priority information gets lost (with the built-in implementation). echo $chain->run(1); echo PHP_EOL;
许可证
它是MIT许可证。