diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-11-24 04:58:38 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-11-24 04:58:38 +0900 |
| commit | 67094790d2d9db5c99e7c136f49061a78698e57d (patch) | |
| tree | 02feb966e74c7c2d1b6a77d8310502aa9758649b /vhosts/t/phpcon-kagawa-2025/src/App.php | |
| parent | a071111365f9760b2f97fa3f6e12aee9f75dd15d (diff) | |
| download | nil.ninja-67094790d2d9db5c99e7c136f49061a78698e57d.tar.gz nil.ninja-67094790d2d9db5c99e7c136f49061a78698e57d.tar.zst nil.ninja-67094790d2d9db5c99e7c136f49061a78698e57d.zip | |
Add vhosts/t/phpcon-kagawa-2025/
Diffstat (limited to 'vhosts/t/phpcon-kagawa-2025/src/App.php')
| -rw-r--r-- | vhosts/t/phpcon-kagawa-2025/src/App.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/vhosts/t/phpcon-kagawa-2025/src/App.php b/vhosts/t/phpcon-kagawa-2025/src/App.php new file mode 100644 index 0000000..85d5ed4 --- /dev/null +++ b/vhosts/t/phpcon-kagawa-2025/src/App.php @@ -0,0 +1,61 @@ +<?php + +declare(strict_types=1); + +namespace Nsfisis\TinyPhpHttpd; + +use Nsfisis\TinyPhpHttpd\Http\Server; +use Nsfisis\TinyPhpHttpd\PhpConKagawa2025\CookieEatHandler; +use Nsfisis\TinyPhpHttpd\PhpConKagawa2025\CookieHandler; +use Nsfisis\TinyPhpHttpd\PhpConKagawa2025\GetHandler; +use Nsfisis\TinyPhpHttpd\PhpConKagawa2025\HealthHandler; +use Nsfisis\TinyPhpHttpd\PhpConKagawa2025\NotFoundHandler; +use Nsfisis\TinyPhpHttpd\PhpConKagawa2025\PostHandler; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\RequestHandlerInterface; + +final class App implements RequestHandlerInterface +{ + private Server $server; + + /** + * @var array<string, RequestHandlerInterface> + */ + private array $routes = []; + + private RequestHandlerInterface $notFoundHandler; + + public function __construct(string $host, int $port) + { + $this->server = new Server($host, $port); + $this->notFoundHandler = new NotFoundHandler($this->server, $this->server); + } + + public function run(): void + { + $this->addRoute('/phpcon-kagawa-2025/health/', new HealthHandler($this->server, $this->server)); + $this->addRoute('/phpcon-kagawa-2025/get/', new GetHandler($this->server, $this->server)); + $this->addRoute('/phpcon-kagawa-2025/post/', new PostHandler($this->server, $this->server)); + $this->addRoute('/phpcon-kagawa-2025/cookie/', new CookieHandler($this->server, $this->server)); + $this->addRoute('/phpcon-kagawa-2025/cookie/eat/', new CookieEatHandler($this->server, $this->server)); + + $this->server->run($this); + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + $path = $request->getRequestTarget(); + if ($path !== '/' && ! str_ends_with($path, '/')) { + $path .= '/'; + } + $handler = $this->routes[$path] ?? $this->notFoundHandler; + return $handler->handle($request); + } + + private function addRoute(string $path, RequestHandlerInterface $handler): self + { + $this->routes[$path] = $handler; + return $this; + } +} |
