Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created January 13, 2016 15:57
Show Gist options
  • Save sasezaki/669e2daa2337caf8e192 to your computer and use it in GitHub Desktop.
Save sasezaki/669e2daa2337caf8e192 to your computer and use it in GitHub Desktop.
An Middleware Builder in a tweet
<?php
// Minified
// class B{function __construct($m){$this->m = $m;} function __invoke($req,$res) {if($this->m)return array_shift($this->m)($req,$res,$this);}}
class B{
function __construct($m)
{
$this->m = $m;
}
function __invoke($req,$res)
{
if($this->m)return array_shift($this->m)($req,$res,$this);
}
}
require_once __DIR__.'/vendor/autoload.php';
$sender = function($q, $s, $n) {
$s = $n($q, $s);
echo $s->getBody();
return $s;
};
$middlewarex = function($q, $s, $n) {
$s->getBody()->write('Hello1');
$s = $n($q, $s);
$s->getBody()->write('Hello2');
return $s;
};
$app = function($q, $s, $n) {
$s->getBody()->write('APP');
return $s;
};
$req = Zend\Diactoros\ServerRequestFactory::fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
$res = new Zend\Diactoros\Response;
(new B([$sender, $middlewarex, $app]))($req,$res); // Hello1APPHello2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment