bartosz-maciaszek / chain
Actions chaining made easy for PHP7
Installs: 6 660
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=7.0
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: dev-feature/code-coverage-ver-4
- mockery/mockery: ~1.0@dev
- phpmd/phpmd: ~2.4
- phpspec/phpspec: ~2.5
- phpspec/prophecy: ~1.6@dev
- phpunit/phpcov: ~3.1
- phpunit/phpunit: ~5.4
- squizlabs/php_codesniffer: ~2.6
This package is not auto-updated.
Last update: 2025-02-01 21:36:21 UTC
README
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:
- 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.
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.