🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## 示例 ### 宠物食物 <details> <summary>main.php</summary> ``` <?php interface Handler{ public function setNext(Handler $handler): Handler; public function handle(string $request): ?string; } abstract class AbstractHandler implements Handler{ /** * 设置handle并返回需设置的handler * @var Handler */ private $nextHandler; /** * @param Handler $handler * @return Handler */ public function setNext(Handler $handler): Handler{ $this->nextHandler = $handler; return $handler; } public function handle(string $request): ?string{ if($this->nextHandler){ return $this->nextHandler->handle($request); } return null; } } class MonkeyHandler extends AbstractHandler{ public function handle(string $request): ?string{ if($request === "Banana"){ return "Monkey: I'll eat the ".$request.".\n"; }else{ return parent::handle($request); } } } class SquirrelHandler extends AbstractHandler{ public function handle(string $request): ?string{ if($request === "Nut"){ return "Squirrel: I'll eat the ".$request.".\n"; }else{ return parent::handle($request); } } } class DogHandler extends AbstractHandler{ public function handle(string $request): ?string{ if($request === "MeatBall"){ return "Dog: I'll eat the ".$request.".\n"; }else{ return parent::handle($request); } } } function clientCode(Handler $handler){ foreach(["Nut", "Banana", "Cup of coffee"] as $food){ echo "Client: Who wants a ".$food."?\n"; $result = $handler->handle($food); if($result){ echo " ".$result; }else{ echo " ".$food." was left untouched.\n"; } } } $monkey = new MonkeyHandler(); $squirrel = new SquirrelHandler(); $dog = new DogHandler(); $monkey->setNext($squirrel)->setNext($dog); echo "Chain: Monkey > Squirrel > Dog\n\n"; clientCode($monkey); echo "\n"; echo "Subchain: Squirrel > Dog\n\n"; // $squirrel 并没有 $dog 的handle clientCode($squirrel); ``` </details> <br /> 输出 ``` Chain: Monkey > Squirrel > Dog Client: Who wants a Nut? Squirrel: I'll eat the Nut. Client: Who wants a Banana? Monkey: I'll eat the Banana. Client: Who wants a Cup of coffee? Cup of coffee was left untouched. Subchain: Squirrel > Dog Client: Who wants a Nut? Squirrel: I'll eat the Nut. Client: Who wants a Banana? Banana was left untouched. Client: Who wants a Cup of coffee? Cup of coffee was left untouched. ``` ### http中间件 <details> <summary>main.php</summary> ``` <?php abstract class Middleware { /** * @var Middleware */ private $next; public function linkWith(Middleware $next): Middleware { $this->next = $next; return $next; } public function check(string $email, string $password): bool { if (!$this->next) { return true; } return $this->next->check($email, $password); } } // 账户检测中间件 class UserExistsMiddleware extends Middleware { private $server; public function __construct(Server $server) { $this->server = $server; } public function check(string $email, string $password): bool { if (!$this->server->hasEmail($email)) { echo "UserExistsMiddleware: This email is not registered!\n"; return false; } if (!$this->server->isValidPassword($email, $password)) { echo "UserExistsMiddleware: Wrong password!\n"; return false; } return parent::check($email, $password); } } // 角色中间接 class RoleCheckMiddleware extends Middleware { public function check(string $email, string $password): bool { if ($email === "admin@example.com") { echo "RoleCheckMiddleware: Hello, admin!\n"; return true; } echo "RoleCheckMiddleware: Hello, user!\n"; return parent::check($email, $password); } } // 节流中间件 class ThrottlingMiddleware extends Middleware { private $requestPerMinute; private $request; private $currentTime; public function __construct(int $requestPerMinute) { $this->requestPerMinute = $requestPerMinute; $this->currentTime = time(); } public function check(string $email, string $password): bool { if (time() > $this->currentTime + 60) { $this->request = 0; $this->currentTime = time(); } $this->request++; if ($this->request > $this->requestPerMinute) { echo "ThrottlingMiddleware: Request limit exceeded!\n"; die(); } return parent::check($email, $password); } } class Server { private $users = []; /** * @var Middleware */ private $middleware; public function setMiddleware(Middleware $middleware): void { $this->middleware = $middleware; } public function logIn(string $email, string $password): bool { if ($this->middleware->check($email, $password)) { echo "Server: Authorization has been successful!\n"; // Do something useful for authorized users. return true; } return false; } public function register(string $email, string $password): void { $this->users[$email] = $password; } public function hasEmail(string $email): bool { return isset($this->users[$email]); } public function isValidPassword(string $email, string $password): bool { return $this->users[$email] === $password; } } $server = new Server(); $server->register("admin@example.com", "admin_pass"); $server->register("user@example.com", "user_pass"); $middleware = new ThrottlingMiddleware(2); $middleware ->linkWith(new UserExistsMiddleware($server)) ->linkWith(new RoleCheckMiddleware()); $server->setMiddleware($middleware); do { echo "\nEnter your email:\n"; $email = readline(); echo "Enter your password:\n"; $password = readline(); $success = $server->logIn($email, $password); } while (!$success); ``` </details> <br /> 输出 ``` Enter your email: asd Enter your password: 123 UserExistsMiddleware: This email is not registered! Enter your email: admin@example.com Enter your password: wrong UserExistsMiddleware: Wrong password! Enter your email: admin@example.com Enter your password: letmein ThrottlingMiddleware: Request limit exceeded! Enter your email: admin@example.com Enter your password: admin_pass RoleCheckMiddleware: Hello, admin! Server: Authorization has been successful! ```