Actions chaining made easy for PHP7

dev-master 2016-09-17 13:29 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:13:39 UTC


README

Build Status Minimum PHP Version Dependency Status

About

This is a simple library that makes it easier to chain actions/processors. Useful for any kind of data processing with reusable pieces of code in a specific order.

The library is inspired with Chain of Responsibility design pattern and middleware-like approach where one middleware executes its logic and, conditionally, runs next middleware.

How do I install it?

composer require bartosz-maciaszek/chain

How do I use it?

In three simple steps:

  1. Create a context class that implements BM\Chain\ChainContextInterface.
  2. Create an instance of BM\Chain\ProcessorsQueue and register processor with it.
  3. Execute the chain by invoking execute() method and passing the context object.

What is the context class?

This is a class that stores some information and is being passed to each of the registered processor. Context can contain some initial input data of any type. Processors are meant to use that data during execution. Obviously, they can always store anything in the context. You're in charge here.

What are the processors?

Processors are callable's that contain some logic to be executed. They can be closures or classes implementing __invoke method. The processors always take two arguments: context and next processor in the queue. They are responsible for execution of next one, otherwise the chain breaks.

What is the processor queue?

Processor queue is an object that aggregates processors in the given order. It exposes methods that allow managing processors.

What is the chain?

Chain is a class that is responsible for execution of the processor queue and passing the context object to them.

Any examples?

Have a look here.