<?php
header('Content-type: text/plain');
$middleware = ['middle_a', 'middle_b', 'middle_c', 'middle_d', 'middle_e'];
function middle_a($output, $next) {
echo __FUNCTION__ . ":before\n";
$output = $next($output+1);
echo __FUNCTION__ . ":after\n";
return $output;
}
function middle_b($output, $next) {
echo __FUNCTION__ . ":before\n";
$output = $next($output+1);
echo __FUNCTION__ . ":after\n";
return $output;
}
function middle_c($output, $next) {
try {
echo __FUNCTION__ . ":before\n";
$output = $next($output+1);
echo __FUNCTION__ . ":after\n";
return $output;
}
catch (Exception1 $ex) {
echo __FUNCTION__ . ":after\n";
// echo __FUNCTION__ . " caught a `" . get_class($ex) . "` and throws a `Exception2`\n";
// throw new Exception2(-100);
return -100;
// return $next(-100);
}
}
function middle_d($output, $next) {
echo __FUNCTION__ . ":before\n";
$output = $next($output+1);
echo __FUNCTION__ . ":after\n";
return $output;
}
function middle_e($output, $next) {
echo __FUNCTION__ . ":before\n";
$output = $next($output+1);
echo __FUNCTION__ . ":after\n";
return $output;
}
class Exception1 extends Exception {}
class Exception2 extends Exception {}
class Exception3 extends Exception {}
function act($input) {
echo __FUNCTION__ . ":start\n";
throw new Exception1;
return $input;
}
$pipeline = function($input) {
return act($input);
};
foreach (array_reverse($middleware) as $cb) {
$pipeline = function($input) use ($cb, $pipeline) {
return $cb($input, $pipeline);
};
}
try {
var_dump($pipeline(0));
}
catch (\Exception $ex) {
exit("App caught a `" . get_class($ex) . "`");
}
- Output for 5.6.0 - 5.6.30, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
- middle_a:before
middle_b:before
middle_c:before
middle_d:before
middle_e:before
act:start
middle_c:after
middle_b:after
middle_a:after
int(-100)
preferences:
55.82 ms | 408 KiB | 5 Q