Download the PHP package meuhmeuhconcept/processor without Composer
On this page you can find all versions of the php package meuhmeuhconcept/processor. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download meuhmeuhconcept/processor
More information about meuhmeuhconcept/processor
Files in meuhmeuhconcept/processor
Package processor
Short Description Mechanism to process a work from a request
License MIT
Informations about the package processor
Processor
Mechanism to process a work from a request
Installation
You can install Processor with Composer
How to use it ?
The main interest of this library is the ChainProcessor
. It contains multiple processors
who can procced a particular work from a request and generate a response.
The ChainProcessor
ask to each processors contains on it if it can proceed the job for a particular request.
The first processor who awser that it can do it will be use.
The response contains several things like the request itself, the status code, name of processor who do the job and obviously the output of the job.
Build a Request
Before use the processor you have to build your request. You request can be what ever you want : a simple string, an array, a object... This object will be use to determine if processor can do the job and to do it.
Example
Proceed the job
You just have to use a Processor
to try to do the job. The best way is to use a ChainProcessor
who contains several processors.
In fact you can directly try to do the job with a ChainProcessor
.
If the job can't be done the processor return a response with the special status code Mmc\Processor\Component\ResponseStatusCode::NOT_IMPLEMENTED
.
Use the Response
The fisrt thing that you have to do with a Response
is to consult the status code to know how the job is done.
You can also know which Processor
had done the job (when you use a ChainProcessor
) with the next method :
Obviously you can get the initial request (i.e. to get the input) with the method $response->getRequest()
.
And the more important is to get the result of the job.
Build your own Processor
This library is not ready to play because there is no Processor
define in it.
You have to create them to do the job that you want.
To do it you just have to create a class who implements Mmc\Processor\Component\Processor
interface.
Now, you just have to add it on a ChainProcessor
.
Easiers ways
- use
Mmc\Processor\Component\ProcessorTrait
and implementsMmc\Processor\Component\Processor
- extends
Mmc\Processor\Component\AbstractProcessor
(who useMmc\Processor\Component\ProcessTrait
)
Why ? Because the process method :
- already test if request is supported by processor before proceed
- create and return a
Response
withResponseStatusCode::INTERNAL_ERROR
status code if your processor returnnull
- create and return a
Response
withResponseStatusCode::INTERNAL_ERROR
status code if your processor throw an exception. - create and return a
Response
withResponseStatusCode::OK
status code if your processor return a not null value who is not an instance ofResponse
The difference with previous method is that 'process' method have to be call 'doProcess' and be protected
.
Example with AbstractProcessor
Example with ProcessorTrait
Integration with other libraries
Symfony
meuhmeuhconcept/processor
contains a CompilerPass
to link automatically Processor
to a ChainProcessor
.
Configuration
You can use this CompilerPass
with class Mmc\Processor\Bridge\Symfony\DependencyInjection\Compiler\ProcessorPass
or just add the bundle in your Symfony kernel like this :
To use it you have to require symfony/dependency-injection
and symfony/options-resolver
packages (if you don't use Symfony fullstack).
Uses
Now you can use the tag mmc.processor.chain
on a service (which is instance of ChainProcessor
).
Every services tagged with the name of this service will be add in it.
You can add priority
parameter on the tag to set the priority of the processor (default priority is 10).
In this example, the ChainProcessor
my_chain will receive the two Processor
p1 and p2.
What now ?
The input of the Processor
if no constraint by a type hinting, so you can use really what you want to create your request.
The ChainProcessor
can do what you want... if it contains Processor
who can do it.