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); |
66
|
|
|
} catch (\Exception $e) { |
67
|
|
|
$response |
68
|
|
|
->setStatus(500) |
69
|
|
|
->write($e->getMessage()) |
70
|
|
|
->end(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create a new group of route |
76
|
|
|
* @param String $prefix prefix of the routes |
77
|
|
|
* @param Callable $callback |
78
|
|
|
* |
79
|
|
|
* @return \CapMousse\ReactRestify\Routing\Routes |
80
|
|
|
*/ |
81
|
|
|
public function group($prefix, $callback) |
82
|
|
|
{ |
83
|
|
|
return $this->router->addGroup($prefix, $callback); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* The the Access-Control-Allow-Origin header |
88
|
|
|
* |
89
|
|
|
* @param string $origin |
90
|
|
|
*/ |
91
|
|
|
public function setAccessControlAllowOrigin($origin) |
92
|
|
|
{ |
93
|
|
|
$this->allowOrigin = $origin; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Init default event catch |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
private function initEvents() |
102
|
|
|
{ |
103
|
|
|
$this->router->on('NotFound', function($request, $response) { |
104
|
|
|
$response |
105
|
|
|
->setStatus(404) |
106
|
|
|
->write('Not found') |
107
|
|
|
->end(); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
$this->router->on('MethodNotAllowed', function($request, $response) { |
111
|
|
|
$response |
112
|
|
|
->setStatus(405) |
113
|
|
|
->write('Method Not Allowed') |
114
|
|
|
->end(); |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
$this->router->on('error', function ($request, $response, $error) { |
118
|
|
|
$response |
119
|
|
|
->reset() |
120
|
|
|
->setStatus(500) |
121
|
|
|
->write($error) |
122
|
|
|
->end(); |
123
|
|
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Manual router event manager |
128
|
|
|
* @param String $event |
129
|
|
|
* @param Callable $callback |
130
|
|
|
*/ |
131
|
|
|
public function on($event, $callback) |
132
|
|
|
{ |
133
|
|
|
$this->router->removeAllListeners($event); |
134
|
|
|
$this->router->on($event, $callback); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create runner instance |
139
|
|
|
* @param Int $port |
140
|
|
|
* @param String $host |
141
|
|
|
* @return Server |
142
|
|
|
*/ |
143
|
|
|
public function listen($port, $host = "127.0.0.1") |
144
|
|
|
{ |
145
|
|
|
$runner = new Runner($this); |
146
|
|
|
$runner->listen($port, $host); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $name method to call |
153
|
|
|
* @param array $arguments |
154
|
|
|
*/ |
155
|
|
|
public function __call($name, $arguments) |
156
|
|
|
{ |
157
|
|
|
return $this->router->addRoute($name, $arguments[0], $arguments[1]); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
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..