schnittstabil / psr7-middleware-stack
Stack PSR-7 middlewares in a reusable way.
Installs: 111 343
Dependents: 3
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 1
Open Issues: 0
Requires (Dev)
- codeclimate/php-test-reporter: ^0.3.0
- fabpot/php-cs-fixer: ^1.10
- halleck45/phpmetrics: ^1.8
- mrclay/minify: ^2.2
- oscarotero/psr7-middlewares: ^3.12
- phpmd/phpmd: ^2.3
- psr/http-message: ^1.0
- satooshi/php-coveralls: ^1.0.1
- schnittstabil/sugared-phpunit: ^0.2.0
- slim/slim: ^3.3
- squizlabs/php_codesniffer: ^2.5
- willdurand/negotiation: ^2.0
README
Stack PSR-7 middlewares in a reusable way 🍡
Install
$ composer require schnittstabil/psr7-middleware-stack
Usage
<?php use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Schnittstabil\Psr7\MiddlewareStack\MiddlewareStack; $newMiddleware = (new MiddlewareStack()) ->add($someMiddleware4) ->add($someMiddleware3) ->add( function (RequestInterface $req, ResponseInterface $res, callable $next) { $res->getBody()->write('Greetings from the the 2nd middleware.'); return $next($req, $res); } ) ->add($someMiddleware1); ?>
Slim Example
Suppose we want to minify the response of some, but not all routes by Oscar Otero's psr7-middlewares in a reusable way.
Install dependencies:
$ composer require slim/slim $ composer require oscarotero/psr7-middlewares $ composer require mrclay/minify $ composer require willdurand/negotiation
app.php
<?php require __DIR__.'/vendor/autoload.php'; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr7Middlewares\Middleware; use Slim\App; use Slim\Http\Stream; use Schnittstabil\Psr7\MiddlewareStack\MiddlewareStack; // setup oscarotero/psr7-middlewares Middleware::setStreamFactory(function ($file, $mode) { return new Stream(fopen($file, $mode)); }); $app = new App(); $app->getContainer()['minifyMiddleware'] = function ($c) { return (new MiddlewareStack()) ->add(Middleware::Minify()) ->add(Middleware::FormatNegotiator()); }; $app->get('/', function (RequestInterface $request, ResponseInterface $response) { $body = '<!-- comment --><h1>Hello world!</h1><!-- comment -->'; return $response->write($body); })->add('minifyMiddleware'); $app->run(); ?>
License
MIT © Michael Mayer