public function handleError(Request $request, RouterEntry $router = null, \Exception $error)
 {
     if ($request->getMethod() !== 'get') {
         response()->json(array('error' => $error->getMessage(), 'code' => $error->getCode()));
     }
     if ($error instanceof OrganisationDisabledException) {
         $this->showDisabledImage();
     }
     if ($error->getCode() === 404) {
         $this->showMissingImage($request);
     }
     $this->showErrorImage($request);
     // Might be optimised.
     die($error->getMessage());
 }
 /**
  * Get all get/post items
  * @param array|null $filter Only take items in filter
  * @return array
  */
 public function all(array $filter = null)
 {
     $output = $_POST;
     if ($this->request->getMethod() === 'post') {
         $contents = file_get_contents('php://input');
         if (strpos(trim($contents), '{') === 0) {
             $output = json_decode($contents, true);
             if ($output === false) {
                 $output = [];
             }
         }
     }
     $output = array_merge($_GET, $output);
     if ($filter !== null) {
         $output = array_filter($output, function ($key) use($filter) {
             return in_array($key, $filter) === true;
         }, ARRAY_FILTER_USE_KEY);
     }
     return $output;
 }
 public function routeRequest($rewrite = false)
 {
     $this->loadedRoute = null;
     $routeNotAllowed = false;
     try {
         /* Initialize boot-managers */
         if (count($this->bootManagers) > 0) {
             $max = count($this->bootManagers) - 1;
             /* @var $manager IRouterBootManager */
             for ($i = $max; $i >= 0; $i--) {
                 $manager = $this->bootManagers[$i];
                 $this->request = $manager->boot($this->request);
                 if (!$this->request instanceof Request) {
                     throw new HttpException('Bootmanager "' . get_class($manager) . '" must return instance of ' . Request::class, 500);
                 }
             }
         }
         if ($rewrite === false) {
             /* Loop through each route-request */
             $this->processRoutes($this->routes);
             if ($this->csrfVerifier !== null) {
                 /* Verify csrf token for request */
                 $this->csrfVerifier->handle($this->request);
             }
             $this->originalUrl = $this->request->getUri();
         }
         $max = count($this->processedRoutes) - 1;
         /* @var $route IRoute */
         for ($i = $max; $i >= 0; $i--) {
             $route = $this->processedRoutes[$i];
             /* If the route matches */
             if ($route->matchRoute($this->request) === true) {
                 /* Check if request method matches */
                 if (count($route->getRequestMethods()) > 0 && in_array($this->request->getMethod(), $route->getRequestMethods(), false) === false) {
                     $routeNotAllowed = true;
                     continue;
                 }
                 $this->loadedRoute = $route;
                 $this->loadedRoute->loadMiddleware($this->request, $this->loadedRoute);
                 /* If the request has changed, we reinitialize the router */
                 if ($this->request->getUri() !== $this->originalUrl && in_array($this->request->getUri(), $this->routeRewrites) === false) {
                     $this->routeRewrites[] = $this->request->getUri();
                     $this->routeRequest(true);
                     return;
                 }
                 /* Render route */
                 $routeNotAllowed = false;
                 $this->request->setUri($this->originalUrl);
                 $this->loadedRoute->renderRoute($this->request);
                 break;
             }
         }
     } catch (\Exception $e) {
         $this->handleException($e);
     }
     if ($routeNotAllowed === true) {
         $this->handleException(new HttpException('Route or method not allowed', 403));
     }
     if ($this->loadedRoute === null) {
         $this->handleException(new NotFoundHttpException('Route not found: ' . $this->request->getUri(), 404));
     }
 }