mercureact/src/Http/Server.php

98 lines
2.6 KiB
PHP

<?php
namespace NoccyLabs\Mercureact\Http;
use NoccyLabs\Mercureact\Broker\TopicManager;
use NoccyLabs\Mercureact\Configuration;
use NoccyLabs\Mercureact\Http\Middleware\ApiHandler;
use NoccyLabs\Mercureact\Http\Middleware\MercureHandler;
use NoccyLabs\Mercureact\Http\Middleware\NotFoundHandler;
use NoccyLabs\Mercureact\Http\Middleware\ResponseMiddleware;
use NoccyLabs\Mercureact\Http\Middleware\SecurityMiddleware;
use NoccyLabs\Mercureact\Http\Middleware\WebSocketHandler;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Http\HttpServer;
use React\Socket\ServerInterface;
use SplObjectStorage;
class Server
{
private Configuration $config;
private LoopInterface $loop;
private HttpServer $server;
private SplObjectStorage $webSocketClients;
private TopicManager $topicManager;
private ResponseMiddleware $responseMiddleware;
private SecurityMiddleware $securityMiddleware;
private WebSocketHandler $webSocketHandler;
private MercureHandler $mercureHandler;
private ApiHandler $apiRequestHandler;
private NotFoundHandler $notFoundHandler;
/**
*
*
*/
public function __construct(Configuration $config, ?LoopInterface $loop=null)
{
$this->loop = $loop??Loop::get();
$this->config = $config;
$this->topicManager = new TopicManager();
$this->webSocketClients = new SplObjectStorage();
$this->server = $this->createHttpServer();
}
/**
*
*
* @return void
*/
public function listen(ServerInterface $socket): void
{
$this->server->listen($socket);
}
/**
*
* @return HttpServer
*/
private function createHttpServer(): HttpServer
{
return new HttpServer(
$this->responseMiddleware = new ResponseMiddleware(
config: $this->config
),
$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()
);
}
}