1 | <?php |
||
9 | class Server |
||
10 | { |
||
11 | /** |
||
12 | * Name of the server |
||
13 | * @var string |
||
14 | */ |
||
15 | public $name = "ReactRestify"; |
||
16 | |||
17 | /** |
||
18 | * Version of the API |
||
19 | * @var null |
||
20 | */ |
||
21 | public $version = null; |
||
22 | |||
23 | /** |
||
24 | * @var Routing\Router |
||
25 | */ |
||
26 | private $router; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $allowOrigin = "*"; |
||
32 | |||
33 | /** |
||
34 | * @param null $name |
||
35 | * @param null $version |
||
36 | */ |
||
37 | public function __construct($name = null, $version = null) |
||
38 | { |
||
39 | if (null !== $name) { |
||
40 | $this->name = $name; |
||
41 | } |
||
42 | |||
43 | if (null !== $version) { |
||
44 | $this->version = $version; |
||
45 | } |
||
46 | |||
47 | $this->router = new Routing\Router(); |
||
48 | |||
49 | $this->initEvents(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Parse request from user |
||
54 | * |
||
55 | * @param \React\Http\Request $httpRequest |
||
56 | * @param \React\Http\Response $httpResponse |
||
57 | */ |
||
58 | public function __invoke(HttpRequest $httpRequest, HttpResponse $httpResponse) |
||
59 | { |
||
60 | $request = new Http\Request($httpRequest); |
||
61 | $response = new Http\Response($httpResponse, $this->name, $this->version); |
||
62 | |||
63 | try { |
||
64 | $this->router->launch($request, $response); |
||
65 | } catch (\Exception $e) { |
||
66 | $response |
||
67 | ->setStatus(500) |
||
68 | ->write($e->getMessage()) |
||
69 | ->end(); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Create a new group of route |
||
75 | * @param String $prefix prefix of the routes |
||
76 | * @param Callable $callback |
||
77 | * |
||
78 | * @return \CapMousse\ReactRestify\Routing\Routes |
||
79 | */ |
||
80 | public function group($prefix, $callback) |
||
81 | { |
||
82 | return $this->router->addGroup($prefix, $callback); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * The the Access-Control-Allow-Origin header |
||
87 | * |
||
88 | * @param string $origin |
||
89 | */ |
||
90 | public function setAccessControlAllowOrigin($origin) |
||
91 | { |
||
92 | $this->allowOrigin = $origin; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Init default event catch |
||
97 | * |
||
98 | * @return void |
||
99 | */ |
||
100 | private function initEvents() |
||
101 | { |
||
102 | $this->router->on('NotFound', function($request, $response) { |
||
103 | $response |
||
104 | ->setStatus(404) |
||
105 | ->write('Not found') |
||
106 | ->end(); |
||
107 | }); |
||
108 | |||
109 | $this->router->on('MethodNotAllowed', function($request, $response) { |
||
110 | $response |
||
111 | ->setStatus(405) |
||
112 | ->write('Method Not Allowed') |
||
113 | ->end(); |
||
114 | }); |
||
115 | |||
116 | $this->router->on('error', function ($request, $response, $error) { |
||
117 | $response |
||
118 | ->reset() |
||
119 | ->setStatus(500) |
||
120 | ->write($error) |
||
121 | ->end(); |
||
122 | }); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Manual router event manager |
||
127 | * @param String $event |
||
128 | * @param Callable|string $callback |
||
129 | */ |
||
130 | public function on($event, $callback) |
||
131 | { |
||
132 | $this->router->removeAllListeners($event); |
||
133 | $this->router->on($event, $callback); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Create runner instance |
||
138 | * @param Int $port |
||
139 | * @param String $host |
||
140 | * @return Server |
||
141 | */ |
||
142 | public function listen($port, $host = "127.0.0.1") |
||
143 | { |
||
144 | $runner = new Runner($this); |
||
145 | $runner->listen($port, $host); |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Server middleware |
||
152 | * @param Callable|string $callback |
||
153 | */ |
||
154 | public function use($callback) |
||
155 | { |
||
156 | return $this->router->addMiddleware($callback); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param string $name method to call |
||
161 | * @param array $arguments |
||
162 | */ |
||
163 | public function __call($name, $arguments) |
||
167 | } |
||
168 |