loop = $loop??Loop::get(); $this->config = $config; $this->logger = $this->createLogger(); $this->topicManager = new TopicManager(); $this->loop->addPeriodicTimer(30, function () { $this->topicManager->garbageCollect(); }); $this->webSocketClients = new SplObjectStorage(); $this->server = $this->createHttpServer(); } /** * * * @return void */ public function listen(ServerInterface $socket): void { $this->server->listen($socket); } private function createLogger(): Logger { $handlers = [ new StreamHandler(STDOUT) ]; $logger = new Logger("main", $handlers); return $logger; } /** * * @return HttpServer */ private function createHttpServer(): HttpServer { return new HttpServer( $this->responseMiddleware = new ResponseMiddleware( config: $this->config, logger: $this->logger->withName("http"), ), $this->securityMiddleware = new SecurityMiddleware( config: $this->config ), $this->webSocketHandler = new WebSocketHandler( config: $this->config, webSocketClients: $this->webSocketClients, topicManager: $this->topicManager ), $this->mercureHandler = new MercureHandler( config: $this->config, topicManager: $this->topicManager ), $this->apiRequestHandler = new ApiHandler( config: $this->config, topicManager: $this->topicManager ), $this->notFoundHandler = new NotFoundHandler() ); } }