|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CapMousse\ReactRestify; |
|
4
|
|
|
|
|
5
|
|
|
use React\Http\Request as HttpRequest; |
|
6
|
|
|
use React\Http\Response as HttpResponse; |
|
7
|
|
|
use CapMousse\ReactRestify\Container\Container; |
|
8
|
|
|
|
|
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('error', function ($request, $response, $error) { |
|
110
|
|
|
$response |
|
111
|
|
|
->reset() |
|
112
|
|
|
->setStatus(500) |
|
113
|
|
|
->write($error) |
|
114
|
|
|
->end(); |
|
115
|
|
|
}); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Manual router event manager |
|
120
|
|
|
* @param String $event |
|
121
|
|
|
* @param Callable|string $callback |
|
122
|
|
|
*/ |
|
123
|
|
|
public function on($event, $callback) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->router->removeAllListeners($event); |
|
126
|
|
|
$this->router->on($event, $callback); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Create runner instance |
|
131
|
|
|
* @param Int $port |
|
132
|
|
|
* @param String $host |
|
133
|
|
|
* @return Server |
|
134
|
|
|
*/ |
|
135
|
|
|
public function listen($port, $host = "127.0.0.1") |
|
136
|
|
|
{ |
|
137
|
|
|
$runner = new Runner($this); |
|
138
|
|
|
$runner->listen($port, $host); |
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Server middleware |
|
145
|
|
|
* @param Callable|string $callback |
|
146
|
|
|
*/ |
|
147
|
|
|
public function use($callback) |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->router->addMiddleware($callback); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Add item to the container |
|
154
|
|
|
* @param string $alias |
|
155
|
|
|
* @param string|null $concrete |
|
156
|
|
|
* @return void |
|
157
|
|
|
*/ |
|
158
|
|
|
public function add($alias, $concrete = null) |
|
159
|
|
|
{ |
|
160
|
|
|
$container = Container::getInstance(); |
|
161
|
|
|
$container->add($alias, $concrete); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $name method to call |
|
166
|
|
|
* @param array $arguments |
|
167
|
|
|
*/ |
|
168
|
|
|
public function __call($name, $arguments) |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->router->addRoute($name, $arguments[0], $arguments[1]); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|