An HTTP Interop compatible middleware dispatcher in Equip

Related tags

Laravel dispatch
Overview

Equip Dispatch

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

An HTTP Interop compatible middleware dispatcher in Equip. Attempts to be PSR-1, PSR-2, PSR-4, PSR-7, and PSR-15 compliant.

Heavily influenced by the design of Tari by ircmaxwell.

For more information, see the documentation.

Install

composer require equip/dispatch

Usage

The MiddlewareCollection is a container for middleware that acts as the entry point. It takes two arguments:

  • An array of $middleware which must be instances of server middleware.
  • A callable $default that acts as the terminator for the collection and returns an empty response.

Once the collection is prepared it can dispatched with a server request and will return the response for output.

Example

use Equip\Dispatch\MiddlewareCollection;

// Any implementation of PSR-15 MiddlewareInterface
$middleware = [
    new FooMiddleware(),
    // ...
];

// Default handler for end of collection
$default = function (ServerRequestInterface $request) {
    // Any implementation of PSR-7 ResponseInterface
    return new Response();
};

$collection = new MiddlewareCollection($middleware);

// Any implementation of PSR-7 ServerRequestInterface
$request = ServerRequest::fromGlobals();
$response = $collection->dispatch($request, $default);

Nested Collections

The MiddlewareCollection also implements the MiddlewareInterface to allow collections to be nested:

use Equip\Dispatch\MiddlewareCollection;

// Any implementation of PSR-15 MiddlewareInterface
$middleware = [
    new FooMiddleware(),

    // A nested collection
    new MiddlewareCollection(...),

    // More middleware
    new BarMiddleware(),
    // ...
];

$collection = new MiddlewareCollection($middleware);

// HTTP factories can also be used
$default = [$responseFactory, 'createResponse'];
$request = $serverRequestFactory->createRequest($_SERVER);

$response = $collection->dispatch($request, $default);
Comments
  • Stacks are middlewares too

    Stacks are middlewares too

    This is a consistent continuation of #2 and allows reusing Stack as ServerMiddlewareInterface in other Middleware containers:

    // I've used the splat operator:
    $stack = new Stack($middlewareOne, $middlewareTwo);
    
    $middlewares = [
       // …
       new AwesomePsrMiddleware(),
       // …
       $stack,
       // …
    ];
    
    // some foreign stack container:
    $foreign = new MiddlewareContainer($middlewares, …);
    $foreign->dispatch($request);
    

    See #6 for deatils.

    opened by schnittstabil 6
  • Use PHP 5.6 splat operator

    Use PHP 5.6 splat operator

    Conflicts #1 and #2, but I would prefer this one. Please note: this a predecessor of #5, which subsumes it.

    It think it is the most consistent one:

    $stack = new Stack($default);
    $stack->append($one);
    $stack->append($two);
    
    // same result as
    $stack = new Stack($default, $one);
    $stack->append($two);
    
    // same result as
    $stack = new Stack($default, $one, $two);
    

    Same with arrays:

    // same result as
    $middlewares = [];
    $middlewares[] = $one;
    $middlewares[] = $two;
    $stack = new Stack($default, ...$middlewares);
    
    // obviously the same result as
    $middlewares = [];
    array_push($middlewares, $one);
    array_push($middlewares, $two);
    $stack = new Stack($default, ...$middlewares);
    
    // same result as
    $middlewares = [
       $one,
       $two,
    ];
    $stack = new Stack($default, ...$middlewares);
    

    At the very end, another variant for complicated middleware names, showing that $default should be the very first parameter:

    $stack = new Stack($default, ...[
       new \Some\Very\Long\Namespace\For\A\Middleware\One($with, $many, $parameters),
       new \Some\Very\Long\Namespace\For\A\Middleware\Two($with, $many, $parameters),
    ]);
    
    opened by schnittstabil 3
  • Support latest PSR-15

    Support latest PSR-15

    This PR adds support for http-interop/http-server-middleware: ^1.0. Also removes Travis tests for PHP 5.6 and HHVM which are not supported by the PSR anymore.

    opened by tuupola 2
  • Readme bug

    Readme bug

    The Readme states:

    $stack = new Stack($middlewares, $default);
    $response = $stack->dispatch($request);
    

    However, currently that does not work. Actually, it have to state:

    $stack = new Stack($middlewares);
    $response = $stack->dispatch($request, $default);
    

    PRs which solves that:

    • #1 – Fix Stack implementation according to README.md
    • #2 – Fix README.md according to Stack implementation

    It's more a philosophical question, which one to choose:

    • #1 leads to #5 – and that Stacks can be seen as delegates too
    • #2 leads to #4 – and that Stacks are middlewares too
    opened by schnittstabil 2
  • Tag new release

    Tag new release

    Could you please tag a new releas? it seems the last commit has support for the new interface but the tagged one is still on: "http-interop/http-middleware": "^0.2.0"

    opened by cdekok 1
  • Stacks are delegates too

    Stacks are delegates too

    This is a consistent continuation of #3 and allows reusing Stack as DelegateInterface with other Middlewares:

    $middelware = new AwesomePsrMiddleware();
    $stack = new Stack($default, $middlewareOne, $middlewareTwo);
    
    $response = $middleware->process($request, $stack);
    

    See #6 for deatils.

    opened by schnittstabil 1
Owner
Equip
Equip
Nachricht is an message dispatcher which focuses on distributing workloads

Nachricht Nachricht is a message dispatcher which focuses on distributing workloads. Features Directly dispatch messages Dispatch messages via AMQP au

JTL Software 1 Jan 18, 2022
Simple PSR-7 compatible response sender

Simple PSR-7 compatible response sender

Lazzard 4 Nov 5, 2022
Service manager for Slim compatible with Laravel packages

SlimServices SlimServices is a service manager for the Slim PHP microframework based on Laravel 4 service providers and DI container, allowing you to

its 76 Jul 23, 2022
The fastest way to make a powerful JSON:API compatible Rest API with Laravel.

The first fully customizable Laravel JSON:API builder. "CRUD" and protect your resources with 0 (zero) extra line of code. Installation You can instal

BinarCode 288 Aug 8, 2022
Simple Laravel 5+ (5.4) Theme Switcher with Middleware!

Laravel Theme The simpliest of theme switching for Laravel 5 Installation Usage Installation Laravel 5.1+ Install Laravel Theme manager: composer requ

null 11 Aug 30, 2020
Sqlcommenter is a plugin/middleware/wrapper to augment SQL statements from laravel

sqlcommenter is a plugin/middleware/wrapper to augment SQL statements from laravel with comments that can be used later to correlate user code with SQL statements.

Google 7 Jul 14, 2022
Dashboard to view your http client requests in laravel application

Laravel Blanket is a package with wraps laravel http client requests and provide logs for request and response, also give option to retry any request from dashboard and more...

Ahmed waleed 215 Dec 29, 2022
This is a laravel 4 package for the server and client side of datatables at http://datatables.net/

Datatable Important This package will not receive any new updates! You can still use this package, but be preparared that there is no active developme

Nils Plaschke 388 Dec 30, 2022
Laravel Logable is a simple way to log http request in your Laravel application.

Laravel Logable is a simple way to log http request in your Laravel application. Requirements php >= 7.4 Laravel version >= 6.0 Installation composer

Sagar 6 Aug 25, 2022
Execute Laravel Artisan commands via REST APIs and HTTP requests safely.

Artisan Api There might be some times you wanted to execute an Artisan command, but you did not have access to shell or SSH. Here we brought REST API

Alireza 11 Sep 7, 2022
Renders consistent HTTP JSON responses for API-based projects

Laravel API Response is a package that helps to provide and render a consistent HTTP JSON responses to API calls as well as converting and formatting

Kennedy Osaze 43 Nov 20, 2022
YCOM Impersonate. Login as selected YCOM user 🧙‍♂️in frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Friends Of REDAXO 17 Sep 12, 2022
A simple and flexible PHP middleware dispatcher based on PSR-7, PSR-11, and PSR-15

Woohoo Labs. Harmony Woohoo Labs. Harmony is a PSR-15 compatible middleware dispatcher. Harmony was born to be a totally flexible and almost invisible

Woohoo Labs. 153 Sep 5, 2022
Standards either proposed or approved by the Framework Interop Group

PHP Framework Interoperability Group The idea behind the group is for project representatives to talk about the commonalities between our projects and

PHP-FIG 12.4k Jan 8, 2023
The Kafka Enqueue transport - This is an implementation of Queue Interop specification

Supporting Enqueue Enqueue is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and

Enqueue 40 Oct 6, 2022
WordPress Interop Bundle

WordPress Interop Bundle Introduction This bundle integrates williarin/wordpress-interop with Symfony. Installation composer require williarin/wordpre

William Arin 1 Jan 26, 2022
PSR-15 compatible middleware for Whoops, the pretty error handler

PSR-15 middleware for Whoops A PSR-15 compatible middleware for Whoops, the fantastic pretty error handler for PHP. Installation You can install the l

Franz Liedke 25 May 20, 2022
PSR-7 middleware foundation for building and dispatching middleware pipelines

laminas-stratigility From "Strata", Latin for "layer", and "agility". This package supersedes and replaces phly/conduit. Stratigility is a port of Sen

Laminas Project 47 Dec 22, 2022
[READ-ONLY] The event dispatcher library for CakePHP. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Event Library This library emulates several aspects of how events are triggered and managed in popular JavaScript libraries such as jQuery: An

CakePHP 21 Oct 6, 2022
Dispatcher is a Laravel artisan command scheduling tool used to schedule artisan commands within your project so you don't need to touch your crontab when deploying.

Dispatcher Dispatcher allows you to schedule your artisan commands within your Laravel project, eliminating the need to touch the crontab when deployi

Indatus 1.1k Jan 5, 2023