裝飾者模式在中介軟體中使用
<?php
namespace App;
interface Middleware
{
public function handle($next);
}
class Session implements Middleware
{
public function handle($next)
{
echo "<br/>Session Start<br/>";
$next();
echo "<br/>Session End<br/>";
}
}
class Mysql implements Middleware
{
public function handle($next)
{
echo "<br/>Mysql Start<br/>";
$next();
echo "<br/>Mysql end<br/>";
}
}
function run($next, $step)
{
return function () use ($next, $step) {
call_user_func_array([new $step, `handle`], [$next]);
};
}
$class = [Session::class, Mysql::class];
$callback = array_reduce($class, `App
un`, function () {
});
$callback();
執行結果
Mysql Start
Session Start
Session End
Mysql end