, (*1)
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., (*2)
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., (*3)
How do I install it?
composer require bartosz-maciaszek/chain
How do I use it?
In three simple steps:, (*4)
- 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.
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., (*5)
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., (*6)
What is the processor queue?
Processor queue is an object that aggregates processors in the given
order. It exposes methods that allow managing processors., (*7)
What is the chain?
Chain is a class that is responsible for execution of the processor
queue and passing the context object to them., (*8)
Any examples?
Have a look here., (*9)