Skip to content

Instantly share code, notes, and snippets.

@uzulla
Created April 10, 2016 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzulla/5ce4d98e3f703a578c65ad76efadca5c to your computer and use it in GitHub Desktop.
Save uzulla/5ce4d98e3f703a578c65ad76efadca5c to your computer and use it in GitHub Desktop.
<?php
class MiddlewareResolver
{
public $queue = [];
public function __construct(array $list)
{
$this->queue = $list;
}
public function __invoke($a, $b):ResponseInterface
{
$_ = array_shift($this->queue);
$_ = $_ ?? function($a, $b, $this){ return $b; };
return $_->__invoke($a, $b, $this);
}
}
class ResponseInterface
{
// dummy
}
$next2 = function($a, $b, $next){
echo "in next2".PHP_EOL;
echo "finish".PHP_EOL;
$b = $next($a, $b);
echo "out next2".PHP_EOL;
return $b;
};
$next1 = function($a, $b, $next) {
echo "in next".PHP_EOL;
$b = $next($a, $b);
echo "out next".PHP_EOL;
return $b;
};
$next = function($a, $b, $next){
echo "in b".PHP_EOL;
$b = $next($a, $b);
echo "out b".PHP_EOL;
return $b;
};
$seq = new MiddlewareResolver([
$next,
$next1,
$next2,
]);
$seq(1,new ResponseInterface());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment