1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| $application = function($name) { echo "this is a {$name} application\n" . PHP_EOL; };
$auth = function($handler) { return function($name) use ($handler) { echo "{$name} need a auth middleware\n" . PHP_EOL; return $handler($name); }; };
$filter = function($handler) { return function($name) use ($handler) { echo "{$name} need a filter middleware\n" . PHP_EOL; return $handler($name); }; };
$log = function($handler) { return function($name) use ($handler) { $return = $handler($name); echo "{$name} need a log middleware\n" . PHP_EOL; return $return; }; };
$stack = [];
function pack_middleware($handler, $stack) { foreach (array_reverse($stack) as $key => $middleware) { $handler = $middleware($handler); } return $handler; }
$stack['log'] = $log; $stack['filter'] = $filter; $stack['auth'] = $auth; $run = pack_middleware($application, $stack);
$run('Laravle');
|