Home

Awesome

http-interop middleware list universal module

This package simply providers an http-interop middleware list to any container-interop compatible framework/container. The middleware list is empty. Any package can come and add a middleware to the list.

Installation

This package is typically used by packages providing middlewares.

composer require thecodingmachine/middleware-list-universal-module

If your container supports autodiscovery by thecodingmachine/discovery, there is nothing more to do. Otherwise, you need to register the TheCodingMachine\MiddlewareListServiceProvider into your container.

Refer to your framework or container's documentation to learn how to register service providers.

Usage

The middleware queue is registered under the key MiddlewareListServiceProvider::MIDDLEWARES_QUEUE. This is a \SPLPriorityQueue.

Depending on the middleware you are registering, you generally have a fairly good idea of the order it should run compared to other middlewares.

Let's split the middlewares in 4 families:

Based on those 4 families, the MiddlewareListServiceProvider provides a SPL Priority Queue that one can use to register any middleware at the right point in the queue.

The service provider defines 12 constants you can use to insert a middleware at a given point:

Each "family" has 3 variants: EARLY, NORMAL and LATE, so you can add more fine grained tuning if you want a utility to be triggered before another one, etc...

So if you want to register a middleware, you would typically write:

$middlewareQueue = $container->get(MiddlewareListServiceProvider::MIDDLEWARES_QUEUE);
/* @var $middlewareQueue \SplPriorityQueue */
$middlewareQueue->insert($myMiddleware, MiddlewareOrder::UTILITY);