/** * Run the current route * * @param \React\Http\Request $request * @param \React\Restify\Response $response * @param Callable $next * * @return Void */ public function run(Request $request, Response $response, $next) { if (is_string($this->action)) { $this->action = explode(':', $this->action); $this->action[0] = new $action[0](); } if (in_array($this->method, array('PUT', 'POST'))) { $dataResult = ""; $headers = $request->httpRequest->getHeaders(); //Get data chunck by chunk $request->httpRequest->on('data', function ($data) use($headers, $request, &$dataResult) { $dataResult .= $data; if (isset($headers["Content-Length"])) { if (strlen($dataResult) == $headers["Content-Length"]) { $request->httpRequest->close(); } } else { $request->httpRequest->close(); } }); //Wait request end to launch route $request->httpRequest->on('end', function () use($request, $response, $next, &$dataResult) { if ($dataResult !== null) { parse_str($dataResult, $data); $request->setData($data); } call_user_func_array($this->action, array($request, $response, $next)); $this->emit('after', [$request, $response, $this]); }); } else { call_user_func_array($this->action, array($request, $response, $next)); $this->emit('after', [$request, $response, $this]); } }
/** * Run the current route * * @param \React\Http\Request $request * @param \React\Restify\Response $response * @param Callable $next * * @return Void */ public function run(Request $request, Response $response, $next) { if ($this->method === self::ANY_METHOD) { $this->anyMethod = true; if (is_array($this->action)) { $this->action[1] = strtolower($request->httpRequest->getMethod()); } } if (is_string($this->action)) { if ($this->anyMethod) { $instance = new $this->action(); $this->action = []; $this->action[0] = $instance; $this->action[1] = strtolower($request->httpRequest->getMethod()); } else { $this->action = explode(':', $this->action); $this->action[0] = new $action[0](); } } if (in_array($this->method, array('PUT', 'POST')) || $this->anyMethod && in_array($request->httpRequest->getMethod(), ['PUT', 'POST'])) { $dataResult = ""; $headers = $request->httpRequest->getHeaders(); //Get data chunck by chunk $request->httpRequest->on('data', function ($data) use($headers, $request, &$dataResult) { $dataResult .= $data; $request->httpRequest->close(); }); //Wait request end to launch route $request->httpRequest->on('end', function () use($request, $response, $next, &$dataResult) { if ($dataResult !== null) { parse_str($dataResult, $data); $rowData = (array) json_decode($dataResult); $request->setRowBodyData($rowData); $request->setData($data); } call_user_func_array($this->action, array($request, $response, $next)); $this->emit('after', [$request, $response, $this]); }); } else { call_user_func_array($this->action, array($request, $response, $next)); $this->emit('after', [$request, $response, $this]); } }
/** * Try to match the current uri with all routes * * * @param \React\Http\Request $request * @param \React\Restify\Response $response * * @throws \RuntimeException * @return Void */ private function matchRoutes(Request $request, Response $response, $next) { $badMethod = false; foreach ($this->routes as $route) { if (!$route->isParsed()) { $route->parse(); } if (preg_match('#' . $route->parsed . '$#', $request->httpRequest->getPath(), $array)) { if ($route->method != strtoupper($request->httpRequest->getMethod()) && $route->method != Route::ANY_METHOD) { $badMethod = true; continue; } $methodArgs = array(); foreach ($array as $name => $value) { if (!is_int($name)) { $methodArgs[$name] = $value; } } if (count($methodArgs) > 0) { $request->setData($methodArgs); } $route->run($request, $response, $next); return; } } if ($badMethod) { $this->emit('MethodNotAllowed', array($request, $response, $next)); return; } $this->emit('NotFound', array($request, $response, $next)); }