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.
composer require bartosz-maciaszek/chain
In three simple steps:
- Create a context class that implements
BM\Chain\ChainContextInterface
. - Create an instance of
BM\Chain\ProcessorsQueue
and register processor with it. - Execute the chain by invoking
execute()
method and passing the context object.
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.
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.
Processor queue is an object that aggregates processors in the given order. It exposes methods that allow managing processors.
Chain is a class that is responsible for execution of the processor queue and passing the context object to them.
Have a look here.