Download the PHP package noglitchyo/middleware-collection-request-handler without Composer
On this page you can find all versions of the php package noglitchyo/middleware-collection-request-handler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download noglitchyo/middleware-collection-request-handler
More information about noglitchyo/middleware-collection-request-handler
Files in noglitchyo/middleware-collection-request-handler
Package middleware-collection-request-handler
Short Description Simple PSR-15 middleware collection request handler.
License MIT
Informations about the package middleware-collection-request-handler
middleware-collection-request-handler
Lightweight & dead simple PSR-15 Server Request Handler implementation to process a collection of middlewares.
Description
PSR-15 request handler implementing both RequestHandlerInterface and MiddlewareInterface able to manage a collection of middlewares implementing the MiddlewareInterface.
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
: MiddlewareCollectionInterfaceContains 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), queue (FIFO).
-
$defaultRequestHandler = null
: RequestHandlerInterfaceProvides a default response implementing ResponseInterface 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, the default request handler might be your action class.*
- with the MVC pattern, 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.
Example
Below, this is how simple it is to get the middleware handler running:
Create a custom MiddlewareCollectionInterface implementation
It is easy to create a new MiddlewareCollectionInterface implementation if needed. The interface requires only 3 methods:
Tests
Would like to the run the test suite? Go ahead:
composer test
References
https://www.php-fig.org/psr/psr-15/
License
This project is licensed under the MIT License - see the LICENSE.md file for details.