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