mercureact/src/Http/Server.php

98 lines
2.6 KiB
PHP
Raw Normal View History

2024-03-10 03:06:19 +01:00
<?php
namespace NoccyLabs\Mercureact\Http;
use NoccyLabs\Mercureact\Broker\TopicManager;
use NoccyLabs\Mercureact\Configuration;
2024-03-10 20:22:28 +01:00
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;
2024-03-10 03:06:19 +01:00
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;
2024-03-10 20:22:28 +01:00
private ResponseMiddleware $responseMiddleware;
private SecurityMiddleware $securityMiddleware;
private WebSocketHandler $webSocketHandler;
private MercureHandler $mercureHandler;
private ApiHandler $apiRequestHandler;
private NotFoundHandler $notFoundHandler;
2024-03-10 03:06:19 +01:00
/**
*
*
*/
2024-03-11 00:50:15 +01:00
public function __construct(Configuration $config, ?LoopInterface $loop=null)
2024-03-10 03:06:19 +01:00
{
$this->loop = $loop??Loop::get();
$this->config = $config;
$this->topicManager = new TopicManager();
$this->webSocketClients = new SplObjectStorage();
2024-03-10 20:22:28 +01:00
2024-03-11 00:50:15 +01:00
$this->server = $this->createHttpServer();
2024-03-10 03:06:19 +01:00
}
/**
*
*
* @return void
*/
public function listen(ServerInterface $socket): void
{
$this->server->listen($socket);
}
/**
*
* @return HttpServer
*/
2024-03-11 00:50:15 +01:00
private function createHttpServer(): HttpServer
2024-03-10 03:06:19 +01:00
{
return new HttpServer(
2024-03-10 20:22:28 +01:00
$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(
2024-03-11 00:50:15 +01:00
config: $this->config,
2024-03-10 20:22:28 +01:00
topicManager: $this->topicManager
),
$this->apiRequestHandler = new ApiHandler(
config: $this->config,
topicManager: $this->topicManager
),
$this->notFoundHandler = new NotFoundHandler()
2024-03-10 03:06:19 +01:00
);
}
}