Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noglitchyo/middleware-collection-request-handler
Lightweight & dead simple PSR-15 Server Request Handler implementation to process a collection of middlewares.
https://github.com/noglitchyo/middleware-collection-request-handler
adr dispatcher http middleware middleware-collection middleware-dispatchers psr-15 psr-7 request-handler
Last synced: 3 months ago
JSON representation
Lightweight & dead simple PSR-15 Server Request Handler implementation to process a collection of middlewares.
- Host: GitHub
- URL: https://github.com/noglitchyo/middleware-collection-request-handler
- Owner: noglitchyo
- License: mit
- Created: 2019-06-23T11:46:46.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-07-15T14:35:25.000Z (over 5 years ago)
- Last Synced: 2024-07-09T21:55:43.473Z (4 months ago)
- Topics: adr, dispatcher, http, middleware, middleware-collection, middleware-dispatchers, psr-15, psr-7, request-handler
- Language: PHP
- Homepage:
- Size: 45.9 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-psr15-middlewares - noglitchyo/middleware-collection-request-handler - Lightweight & simple PSR-15 server request handler implementation to handle middleware collection. (Packages / Dispatcher)
README
# middleware-collection-request-handler
Lightweight & dead simple PSR-15 Server Request Handler implementation to process a collection of middlewares.
![PHP from Packagist](https://img.shields.io/packagist/php-v/noglitchyo/middleware-collection-request-handler.svg)
[![Build Status](https://travis-ci.org/noglitchyo/middleware-collection-request-handler.svg?branch=master)](https://travis-ci.org/noglitchyo/middleware-collection-request-handler)
[![codecov](https://codecov.io/gh/noglitchyo/middleware-collection-request-handler/branch/master/graph/badge.svg)](https://codecov.io/gh/noglitchyo/middleware-collection-request-handler)
![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/noglitchyo/middleware-collection-request-handler.svg)
![Packagist](https://img.shields.io/packagist/l/noglitchyo/middleware-collection-request-handler.svg)### Description
PSR-15 request handler implementing both [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php) and [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php)
able to manage a collection of middlewares implementing the [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php).It can be used either as a RequestHandler or as a Middleware to fit into any implementation.
Comes with a set of middleware collections using different strategy (LIFO, FIFO...) on how the middlewares from the collection are provided to the RequestHandler, and also provides a simple MiddlewareCollectionInterface to implement in a glimpse your own strategy.
### Goals
- Simplicity
- Interoperability### Getting started
#### Requirements
- PHP >= 7.3
#### Installation
`composer require noglitchyo/middleware-collection-request-handler`
#### Run
Create a new instance of the request handler class which can be use as a request handler or as a middleware.
##### From the constructor
`RequestHandler::__construct(MiddlewareCollectionInterface $middlewareCollection, RequestHandlerInterface $defaultRequestHandler = null)`
- ***`$middlewareCollection`*** : [MiddlewareCollectionInterface](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/MiddlewareCollectionInterface.php)
Contains the middlewares and defines the strategy used to store the middlewares and to retrieve the next middleware.
Some implementations with common strategies are provided: [stack (LIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplStackMiddlewareCollection.php), [queue (FIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplQueueMiddlewareCollection.php).- ***`$defaultRequestHandler = null`*** : [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php)
Provides a default response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php) if none of the middlewares in the collection was able to create one.
Some examples of what could be a "default request handler":
- with the [ADR pattern](https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder), the default request handler might be your action class.*
- with the [MVC pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), the default request handler might be the action method of your controller.##### From the factory method
`RequestHandler::fromCallable(callable $callable, MiddlewareCollectionInterface $middlewareCollection)`
It creates a RequestHandler instance by wrapping the given `callable` inside an anonymous instance of RequestHandlerInterface.
The callable is the $defaultRequestHandler. **It MUST returns a response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php).**##### Example
Below, this is how simple it is to get the middleware handler running:
```php
handle($request);
}
}
]);// Let's add one more middleware in the collection (this time, from a callable)
$middlewareCollection->addFromCallable(function(ServerRequestInterface $request, RequestHandlerInterface $handler){
return $handler->handle($request);
});// Instantiate a new request handler with a default handler and the middleware collection.
$requestHandler = RequestHandler::fromCallable(
function (ServerRequestInterface $serverRequest){
return new /* instance of ResponseInterface */;
},
$middlewareCollection
);// As a RequestHandler:
// Pass the request to the request handler which will dispatch the request to the middlewares.
$response = $requestHandler->handle(/* ServerRequestInterface */);```
#### Create a custom MiddlewareCollectionInterface implementation
It is easy to create a new MiddlewareCollectionInterface implementation if needed. The interface requires only 3 methods:
```php