Created
April 10, 2016 10:45
-
-
Save uzulla/5ce4d98e3f703a578c65ad76efadca5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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