1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}
}
|