slabphp/sequencer

SlabPHP Sequencer

v0.1.4 2018-02-12 00:00 UTC

This package is auto-updated.

Last update: 2024-09-22 05:07:33 UTC


README

Sequencer 库用于创建可重用的控制器类层次结构。向序列器中添加方法,以便子控制器可以向序列中添加额外项目。SlabPHP 框架利用这些来处理大多数控制器。

SlabPHP 是多年前构建的,我们非常清楚对受保护成员的反感。有关 SlabPHP、时间旅行、弃用、生活、爱情和其他信息,请参阅主要 SlabPHP 框架文档。

示例

假设我们有一个看起来像这样的父类

<?php

class Parent
{
    /**
     * @var \Slab\Sequencer\CallQueue
     */
    protected $sequence;

    /**
     * @var int
     */
    protected $value = 0;

    /**
     * Public constructor
     */
    public function __construct()
    {
        $this->sequence = new \Slab\Sequencer\CallQueue();
        $this->value = 1;
    }

    /**
     * This adds the "doSomethingAlways" method to the Sequencer call queue but does not actually run it yet
     */
    public function setSequence()
    {
        $this->sequence
            ->doSomethingAlways();
    }

    /**
     * Multiply value by 2
     */
    public function doSomethingAlways()
    {
        $this->value *= 2;
    }

    /**
     * Actually run the functions in the sequence and echo the output
     */
    final public function execute()
    {
        $this->sequence->execute($this);

        echo $this->value;
    }
}

您可以在各个地方使用这个控制器。但您想创建一个具有额外操作的类似控制器。您可以按如下方式扩展它

<?php

class Child extends Parent
{
    /**
     * Overload and extend the setSequence function
     */
    public function setSequence()
    {
        parent::setSequence();

        $this->sequence
            ->doSomethingElse();
    }

    /**
     * Multiply the value again by 3
     */
    public function doSomethingElse()
    {
        $this->value *= 3;
    }
}

如果您这样做

$parent = new Parent();
$parent->execute(); //echos 2

但如果您这样做

$child =  new Child();
$child->execute(); //echos 6