public function handleRequest() { ob_start(); $this->startSession(); $routes = RouteCollection::instance(); if ($routes == null || $routes->count() == 0) { throw new \ErrorException('Unable to load routes from configuration directory.'); } $request = Request::createFromGlobals(); $path = $request->getUri()->getPath(); $current_route = null; $matches = array(); $urlMatcher = new UrlMatcher($routes, $request); switch ($urlMatcher->matches($path)) { case UrlMatcher::UNKNOWN_RESOURCE: throw new ResourceNotFoundException('Route not found: ' . $path); return; case UrlMatcher::METHOD_MISMATCH: throw new MethodNotAllowedException("Method not allowed"); return; case UrlMatcher::MATCH_FOUND: $current_route = $urlMatcher->getMatchedRoute(); $matches = $urlMatcher->getMatches(); break; } // we use a controller, so do checks and throw if something is wrong (class or method) if (!$current_route->controller && !$current_route->closure) { throw new IllegalArgumentException('No controller or Closure was set for route ' . $current_route->path); } $params = array(); $userObject = null; // if we use a closure, call them with the request object if ($current_route->closure) { $userObject = $current_route->closure; $ref = new \ReflectionFunction($current_route->closure); if ($ref->getNumberOfParameters() > 0) { $first = $ref->getParameters()[0]; if ($first->getClass() != null && strstr($first->getClass()->getName(), 'Request')) { $params[] = $request; } foreach ($matches as $match) { $params[] = $match; } } } else { // handle controller $parts = explode("@", $current_route->controller); $controller = "App\\Controllers\\" . $parts[0]; $method = $parts[1]; if (!class_exists($controller)) { throw new ResourceNotFoundException("Unable to find controller {$controller}."); } if (!method_exists($controller, $method)) { throw new ResourceNotFoundException("Unable to call {$controller}::{$method}()"); } $instance = new $controller(); $userObject = array($instance, $method); $ref = new \ReflectionClass($controller); $num_params = $ref->getMethod($method)->getNumberOfParameters(); $retval = null; if ($num_params > 0) { $ref_method = $ref->getMethod($method); $first = $ref_method->getParameters()[0]; if ($first->getClass() != null && strstr($first->getClass()->getName(), 'Request')) { $params[] = $request; } foreach ($matches as $match) { $params[] = $match; } } if ($instance instanceof BaseController) { $instance->onBefore(); } } $retval = call_user_func_array($userObject, $params); $this->handleContent($retval); }
private static function output($html, $message) { $req = Request::createFromGlobals(); if ($req->isAjax()) { $json = json_encode(array('error' => true, 'message' => 'Internal Server Error<br>' . $message)); (new Response($json, 500, array('Content-Type' => 'application/json')))->send(); } else { // TODO check for debug. If not, send error page (new Response($html, 500))->send(); } }