jjanvier / yamo
Yet another middleware orchestrator. A simple PSR-15 implementation.
Requires
- http-interop/http-factory: ^0.3.0
- http-interop/http-middleware: ^0.4.1
- zendframework/zend-diactoros: ^1.4
Requires (Dev)
- http-interop/http-factory-diactoros: ^0.3.0
- katzgrau/klogger: ^1.2
- php-di/php-di: ^5.4
- phpspec/phpspec: ^3.4
- phpunit/phpunit: ^6.1
- psr/cache: ^1.0
- symfony/cache: ^3.2
This package is auto-updated.
Last update: 2024-11-17 10:24:45 UTC
README
Yamo is yet another PSR-15 middleware orchestrator.
The purpose of this library is just to help discovering and understanding the PSR-15 about HTTP middlewares.
Usage
Just provide the list of the middlewares you want to use:
$orchestrator = new Orchestrator([
// a list of Psr\Http\ServerMiddleware\MiddlewareInterface middlewares
]
);
$response = $orchestrator->process($request);
Please note that middlewares are called exactly in the order they are defined. For instance
$orchestrator = new Orchestrator([
$middleware1,
$middleware2,
$middleware3,
]
);
$response = $orchestrator->process($request);
will lead to the following execution flow:
Each middleware can, if needed, enrich the incoming request until it reaches the core application. The core application then processes the request to generate a response. This response is transmitted to each middleware which can also, if needed, enrich the response. Finally, when all middlewares have been reached, the orchestrator returns the final response.
Alternatively, you can also define the PSR-17 response factory factory you want to use. This factory will be used
to return a default 200 response in case no middleware provided one. The default response factory is Http\Factory\Diactoros\ResponseFactory
.
$customResponseFactory = new Http\Factory\Guzzle\ResponseFactory; // any Psr\Http\Message\ResponseFactoryInterface
$orchestrator = new Orchestrator([
// a list of Psr\Http\ServerMiddleware\MiddlewareInterface middlewares
],
$customResponseFactory
);
$response = $orchestrator->process($request);
Examples
For "academic" purpose, some middleware examples are provided in the examples folder. Those classes are not meant to be used. They can be considered as "advanced pseudo code" to help understand the reader.
The example application can be launched with php -S 127.0.0.1:8080 -t examples/src/
.
Real PSR-15 middlewares can be found in the middlewares/psr15-middlewares repository.