php管道模式手测

34次阅读

共计 1028 个字符,预计需要花费 3 分钟才能阅读完成。

先写在这里,待完善

interface PipelineInterface
{public function send($traveler);
    public function through($stops);
    public function via($method);
    public function process();}

interface StageInterface
{public function handle($payload);
}

class StageOne implements StageInterface
{public function handle($payload) 
    {echo $payload . 'He is a';}
} 

class StageTwo implements StageInterface
{public function handle($payload) {echo 'awesomeman';}
} 

class Pipe implements PipelineInterface
{
    protected $container;
    protected $passable;
    protected $pipes = [];
    protected $via = 'handle';

    public function __construct($container)
    {$this->container = $container;}

    public function send($passable)
    {
        $this->passable = $passable;

        return $this;
    }

    public function through($pipes)
    {$this->pipes = is_array($pipes) ? $pipes : func_get_args();

        return $this;
    }

    public function via($method)
    {
        $this->via = $method;

        return $this;
    }

    public function process()
    {foreach ($this->pipes as $pipe) {call_user_func([$pipe, $this->via], $this->passable);
        }
    }
}
$container = 'a';
$payload = 'wa';
$pipe = new Pipe($container);
$pipe->send($payload)->through([(new StageOne), (new StageTwo)])->process();

正文完
 0