-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-server.php
63 lines (46 loc) · 1.59 KB
/
http-server.php
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
62
63
<?php
use ThenLabs\EasyMails\Page;
use ThenLabs\EasyMails\Bus;
use ThenLabs\HttpServer\HttpServer;
use ThenLabs\HttpServer\Event\RequestEvent;
use ThenLabs\StratusPHP\StratusRequest;
use ThenLabs\ComposedViews\Event\RenderEvent;
use ThenLabs\Components\Event\FilterDependenciesEvent;
use ThenLabs\ComposedViews\Asset\Script;
use function Opis\Closure\{serialize as s, unserialize as u};
$httpServer = new HttpServer([
'host' => '127.0.0.1',
'port' => 8800,
'blocking' => false,
'document_root' => __DIR__.'/public',
]);
$dispatcher = $httpServer->getDispatcher();
$appSource = null;
$dispatcher->addListener(RequestEvent::class, function ($event) use (&$appSource) {
$request = $event->getRequest();
$response = $event->getResponse();
$uri = $event->getRequestUri();
if ($uri == '/') {
$page = new Page('/controller');
$page->setDebug(true);
$page->on('update', [$page, 'onUpdate']);
$response->setContent($page->render());
$appSource = s($page);
$event->stopPropagation();
} elseif (0 === strpos($uri, '/controller')) {
$page = u($appSource);
$clientSocket = $event->getClientSocket();
$bus = new Bus($clientSocket);
$page->setBus($bus);
$stratusRequest = StratusRequest::createFromJson(
$request->request->get('stratus_request')
);
$stratusResponse = $page->run($stratusRequest);
if ($stratusResponse->isSuccessful()) {
$appSource = s($page);
}
$bus->close();
$event->stopPropagation();
}
});
return $httpServer;