Code Monkey home page Code Monkey logo

pipe's Introduction

Pipe

"Surf's Away"

Docs aren't finished yet! Hang ten! Pipe is a simple implementation of the Pipeline Design Pattern.

Installation

Pipe can be installed with Composer (requires Composer, of course):

composer require simoneddy/pipe

Alternatively, you can clone this repo and point your autoloader to its src directory.

Usage

The idea of a Pipeline is to run a "Payload" object through a series of Processor classes. Each Processor class might handle or update the Payload object in a different way. Once each Processor has run, the updated Payload object is returned.

Building a Pipeline

By default, Pipelines must implement the Eddy\Pipe\PipelineInterface.

Pipe's Pipeline class provides a simple implementation that is adequate in many cases.

In order for a Pipeline to be useful it requires some Processors. Processors are classes implementing the Eddy\Pipe\ProcessorInterface, which defines a single process() method. This method must accept and return a Payload object (more on Payloads below - they're important!), but whatever it does to the Payload is up to you.

The default Pipeline can be constructed like so:

use Eddy\Pipe\Pipeline;

// Create a new Pipeline passing an array of Processors to its constructor:
$pipeline = new Pipeline([
    new MyCoolProcessor,
    new MyCoolerProcessor,
    new MySecondRateProcessor,
    // etc
]);

// Alternatively, construct the Pipeline and then add processors afterwards.
$pipeline = new Pipeline;

// You can add an array of Processors with the addProcessors() method:
$pipeline->addProcessors([
    new AnotherCoolProcessor,
    new NotQuiteACoolProcessor
]);

// Or you can add Processors individually with the addProcessor() method:
$pipeline->addProcessor(new UniqueProcessor);

// addProcessor() returns the Pipeline object, so calls can be chained together:
$pipeline->addProcessor(new PrettyGoodProcessor)
    ->addProcessor(new NotShabbyProcessor)
    ->addProcessor(new NeverEndingProcessor);

Building a Payload

Now you need a Payload! You must build a Payload object yourself, but that's where the fun lies!

Payload objects must implement Eddy\Pipe\PayloadInterface to be accepted by Pipelines and Processors. This interface does not specify any methods, and exists solely to allow objects through your Pipelines.

What the Payload actually contains and does is entirely up to you and the requirements of your project, although it is expected to be useable by your Processors.

A Payload class might look something like this:

namespace MyApp;

use Eddy\Pipe\PayloadInterface;

// Implement the PayloadInterface to make the object processable:
class MyCoolPayload implements PayloadInterface
{
    // For this example, let's make the Payload contain a basic string property.
    // We'll call it $text for creativity.
    protected $text = 'My excellent text';

    // Let's say our Processors will manipulate our $text property.
    // They will need to access the current text:
    public function getText()
    {
        return $this->text;
    }

    // And they will need to be able to update the text:
    public function updateText(string $text)
    {
        $this->text = $text;
    }

    // Any other required methods are also added.
}

In the above example our Payload is fairly simple, and only deals with a single string. Your Payload objects can be as basic or as complex as required, provided your Processors can use them.

Processing a Payload

Processing a Payload is done via a Pipeline's process() method. An instance of your Payload class is passed to this method, and the processed Payload is returned. By default, the process() method will only ever return an instance of Eddy\Pipe\PayloadInterface, and will throw an Exception if it cannot.

// Using the example Payload from the previous section:
$payload = new MyApp\MyCoolPayload;

// Send our Payload through our Pipeline for processing:
$result = $pipeline->process($payload);

The default Pipeline contains an __invoke() magic method (though it is not defined in the Eddy\Pipe\PipelineInterface), and can be used as a function/callable:

pipe's People

Contributors

simonceddy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.