parser = new \Phroute\Phroute\RouteParser(); $this->collector = new RouteCollector( $this->parser ); $this->pool = \Spatie\Async\Pool::create(); $this->uri = parse_url( $uri, PHP_URL_PATH ); if ( in_array( $httpMethod, $allowed_methods ) ) { $this->httpMethod = $httpMethod; } else { throw new \Exception("This request method is not allowed."); } } /** * Sets the route */ public function setRoute() { $this->pool = $this->pool->add(async(function () { $this->collector->get($this->uri, function () { if ($this->uri === "/login" || $this->uri === "/signin") { return $this->getTwig()->render('login.html.twig', [ 'title' => 'login' ]); } }); })->then(function ($output) { return $output; })->catch(function (\Exception $exception) { return $exception; })); return await($this->pool); } /** * @return RouteDataArray */ public function routeCollectorData() : RouteDataArray { $this->pool[] = (async(function () { $this->pool->add(function () { return await($this->collector->getData()); })->then(function ($output) { return $output; })->catch(function (\Exception $exception) { throw new \Exception($exception->getMessage()); })->timeout(function ($output) { return $output; }); })); return await($this->pool); } /** * Renders the route specified by the user * * @return mixed|null */ public function route() { $this->pool = $this->pool->add(async(function () { try { $this->setRoute(); $this->dispatcher = new \Phroute\Phroute\Dispatcher( $this->collector->getData() ); return await($this->dispatcher->dispatch( $this->httpMethod, $this->uri ) ); } catch ( \Exception $e ) { return $this->badRoute(); } })->then(function ($output) { return $output; })->catch(function (\Exception $exception) { throw new \Exception("Could not dispatch route!", $exception->getCode()); })); return await($this->pool); } private function badRoute() { //TODO: Finish this later $async_data[] = $this->pool->add(async(function () { return await("this doesn't work"); })); return $async_data; } }