Giter VIP home page Giter VIP logo

psr7-middleware-stack's Introduction

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

psr7-middleware-stack's People

Contributors

schnittstabil avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

urvishsspl

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.