<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云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! ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看