enalquiler/lazy-middleware

Enalquiler PSR-15 Lazy Middleware

2.1.0 2018-10-03 21:19 UTC

This package is not auto-updated.

Last update: 2024-04-26 06:13:28 UTC


README

A dead-simple lazy PSR-15 middleware.

Installation

composer require enalquiler/lazy-middleware

Usage

With Zend Stratigility

Zend Stratigility

<?php

use Zend\Diactoros\Response;
use Zend\Diactoros\Server;
use Zend\Stratigility\MiddlewarePipe;
use Zend\Stratigility\NoopFinalHandler;
use Enalquiler\Middleware\SymfonyMiddleware;
use function Enalquiler\Middleware\lazy;

require __DIR__ . '/../vendor/autoload.php';

$app = new MiddlewarePipe();
$app->setResponsePrototype(new Response());

$server = Server::createServer($app, $_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);

$app
    ->pipe('/foo', lazy(function() {
        // Run heavy computations
        return new HardToBuildMiddleware();
    }))
;

$server->listen(new NoopFinalHandler());

With Middleman

<?php

use Psr\Http\Message\RequestInterface as Request;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use mindplay\middleman\Dispatcher;
use Enalquiler\Middleware\lazy;

$dispatcher = new Dispatcher([
    lazy(function() {
        // Run heavy computations
        return new HardToBuildMiddleware();
    }),
    function (Request $request) {
        return (new Response())->withBody(...); // abort middleware stack and return the response
    },
    // ...
]);

$response = $dispatcher->dispatch(new ServerRequest($_SERVER, $_FILES));

Running the tests

php vendor/bin/phpunit

Authors

  • David Martínez - Initial work
  • Christian Soronellas
  • Enalquiler Engineering

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Acknowledgments