container = $container ?? $this->buildContainer(); $this->dispatcher = $dispatcher ?? $this->buildDispatcher(); } public function container(): Container { return $this->container; } public function dispatcher(): Dispatcher { return $this->dispatcher; } public function handle(ServerRequest $request): ResponseInterface { $psr17 = new Psr17Factory(); $middleware = [ new Middleware\RoutingMiddleware($this->dispatcher, $psr17), new Middleware\DispatchMiddleware($this->container, $psr17), ]; $relay = new Relay($middleware); return $relay->handle($request); } private function buildContainer(): Container { $builder = new ContainerBuilder(); // Add definitions/bindings here as needed. return $builder->build(); } private function buildDispatcher(): Dispatcher { $routesPath = dirname(__DIR__, 2) . '/routes'; $collector = static function (RouteCollector $r) use ($routesPath): void { // Load user-defined routes if present $router = new Router($r); foreach (['web.php', 'api.php'] as $file) { $path = $routesPath . '/' . $file; if (is_file($path)) { /** @noinspection PhpIncludeInspection */ (static function ($router) use ($path) { require $path; })($router); } } // Ensure a default health route exists for acceptance/demo $r->addRoute('GET', '/_phred/health', [Controllers\HealthController::class, '__invoke']); }; return simpleDispatcher($collector); } }