1+ <?php
2+
3+ namespace PHPServer \BuiltIn ;
4+
5+ use Closure ;
6+ use JetBrains \PhpStorm \ArrayShape ;
7+ use JetBrains \PhpStorm \Pure ;
8+
9+ class Server
10+ {
11+ protected string |null $ documentRoot = null ;
12+ protected string |null $ routerScript = null ;
13+ protected Closure |null $ requestCallback = null ;
14+
15+
16+ #[Pure] public static function create (float |string $ host , int $ port ): Server
17+ {
18+ return new Server ($ host , $ port );
19+ }
20+
21+ public function __construct (
22+ protected float |string $ host ,
23+ protected int $ port
24+ )
25+ {
26+ }
27+
28+ public function setDocumentRoot (string $ path ): Server
29+ {
30+ $ this ->documentRoot = $ path ;
31+ return $ this ;
32+ }
33+
34+ public function setRouterScript (string $ filePath ): Server
35+ {
36+ $ this ->routerScript = $ filePath ;
37+ return $ this ;
38+ }
39+
40+ public function onRequest (callable $ callback ): Server
41+ {
42+ $ this ->requestCallback = $ callback ;
43+ return $ this ;
44+ }
45+
46+ public function start (): StartedServer
47+ {
48+ return new StartedServer ($ this , ServerProcess::create ($ this ));
49+ }
50+
51+ #[ArrayShape(['host ' => "int|string " , 'port ' => "int " , 'document_root ' => "null|string " , 'router_script ' => "null|string " , 'request_callback ' => "\Closure|null " ])]
52+ public function getInfo (): array
53+ {
54+ return [
55+ 'host ' => $ this ->host ,
56+ 'port ' => $ this ->port ,
57+ 'document_root ' => $ this ->documentRoot ,
58+ 'router_script ' => $ this ->routerScript ,
59+ 'request_callback ' => $ this ->requestCallback ,
60+ ];
61+ }
62+ }
0 commit comments