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) |
||
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) |
||
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) |
||
84 | |||
85 | /** |
||
86 | * The the Access-Control-Allow-Origin header |
||
87 | * |
||
88 | * @param string $origin |
||
89 | */ |
||
90 | public function setAccessControlAllowOrigin($origin) |
||
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) |
||
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") |
||
142 | |||
143 | /** |
||
144 | * Server middleware |
||
145 | * @param Callable|string $callback |
||
146 | */ |
||
147 | public function use($callback) |
||
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) |
||
172 | } |
||
173 |