GitXplorerGitXplorer
s

psr7-middleware-stack

public
4 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
e62da0bf1dae2c92014e254baef0c8b7e8c342e6

s/sami/sugared-sami/

sschnittstabil committed 9 years ago
Unverified
e1fca0bc6f2dc33f3a092795f86fcfe55a89a702

fix namespace, s/phpunit/sugared-phpunit/ and simplifications

sschnittstabil committed 9 years ago
Unverified
a8f43312cf0e068c120428953db281111f1a9ed7

workaround scrutinizer bug

sschnittstabil committed 9 years ago
Unverified
ddafa8cf82e70671a07f1ef322acefcf92a62fb5

tweak production size

sschnittstabil committed 9 years ago
Unverified
0d56606fa623862a92e662c114dc9419d2ca89a5

export-ignore

sschnittstabil committed 9 years ago
Unverified
cba22bbe85b8c2d38b03f596a0822260b81bd6a1

drop Stack::create() support

sschnittstabil committed 9 years ago

README

The README file for this repository.

Psr7\MiddlewareStack Build Status Coverage Status Scrutinizer Code Quality Code Climate

SensioLabsInsight

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