/** * Create a new response rendered with the error code, and stops page * execution. * * ## Usage * * throw new \Http\Exception(500); * * @param integer The HTTP error code, defaults to 404 * @param Response The current response object, if one is not defined, one will be generated * @return void No value is returned */ public function __construct($code = 404, Response $response = null) { if ($code < 400) { throw new \Exception('An invalid call to Nerd\\Http\\Exception was made. This exception is meant to handle errors for Http, and was called with a ' . $code); } if ($response === null) { $response = new Response(); } $response->setStatus($code)->setBody(call_user_func(Config::get('error.http_error_handler', null, false), $code))->send(); exit; }
public static function __initialize() { $app = static::instance(); $uri = Url::current()->uri(); $css = ['css/bootstrap.css', 'css/bootstrap-responsive.css']; $js = ['js/jquery.js', 'js/bootstrap.js']; $app->response = Response::instance(); $app->session = Session::instance(); $app->cache = Datastore::instance(); $app->css = Asset::collection($css); $app->js = Asset::collection($js); // If there is no url, then we're on the home page. trim($uri, '/') == '' and $uri = '@@HOME'; if ($page = Model\Page::findOneByUri($uri)) { $app->response->setBody($page->title); return; } try { Controller::instance()->dispatch($uri, $app->response); return; } catch (HttpException $e) { if (!($page = Model\Page::findOneByUri($uri = '@@404'))) { // Fallback to system handling. throw new HttpException(404, 'Page Not Found'); } $app->response->setStatus(404); $app->response->setBody($page->title); return; } }
/** * The dispatcher extracts the active URI and routes to a namespaced * Controller for further handling. * * [!!] This method could use some serious optimization, and modularity. * * ## Usage * * $request = FrontController::dispatch(); * * Once you've dispatched your request, you can handle everything after * as you would a \Http\Response. * * @param string URI to parse * @return Response Returns the response instance for further execution */ public function dispatch($uri, \Nerd\Http\Response &$response) { $default = Config::get('routes._default_'); $default = \explode('/', $default); if (($controller = (isset($default[0]) and !empty($default[0])) ? \ucfirst($default[0]) : false) === false) { throw new \Exception('Your application does not appear to have a value default route configured. Please specify one in your routes configuration file.'); } $action = (isset($default[1]) and !empty($default[1])) ? 'action' . ucfirst($default[1]) : 'actionIndex'; unset($default); $directory = \LIBRARY_PATH . \DS; $namespace = \APPLICATION_NS; $segments = array_merge(array_filter(explode('/', ltrim($uri, '/'))), []); // Determine if we're attempting to load a package or the application if (isset($segments[0]) and \strtolower($segments[0]) !== \strtolower($namespace) and \is_dir($directory . $segments[0])) { $namespace = $segments[0]; $directory .= $segments[0] . \DS; $segments = array_slice($segments, 1); } else { $directory .= $namespace . \DS; } $directory .= 'classes' . \DS . 'controller' . \DS; //$response = \Nerd\Http\Response::instance(); if (count($segments)) { $possibility = []; while (count($segments) > 0 and is_dir($directory . $segments[0])) { $directory .= $segments[0] . \DS; $possibility[] = $segments[0]; $segments = array_slice($segments, 1); } if (count($segments) > 0) { if (!file_exists($directory . $segments[0] . '.php')) { throw new \Nerd\Http\Exception(404, $response); } $possibility[] = $segments[0]; $segments = array_slice($segments, 1); } $controller = ''; foreach ($possibility as $value) { $controller .= ucfirst($value) . '\\'; } $controller = rtrim($controller, '\\'); if (count($segments) > 0) { $action = 'action' . ucfirst(array_shift($segments)); } } $controller = '\\' . ucfirst($namespace) . '\\Controller\\' . $controller; if (\class_exists($controller)) { $controller = new $controller($response); } if (!\is_object($controller) or !\method_exists($controller, $action)) { throw new \Nerd\Http\Exception(404, $response); } if (!$controller instanceof \Nerd\Design\Architectural\MVC\Controller) { throw new \Exception('Corrupt application controller. Controller does not implement the MVC specification.'); } $controller->before(); if (($body = call_user_func_array(array($controller, $action), $segments)) != null) { $response->setBody($body); } $controller->after(); }
public static function findMVC($uri) { $segments = explode('/', trim($uri, '/')); $package = array_shift($segments); $path = LIBRARY_PATH . '/' . (isset(static::$packages[$package]) ? $package : strtolower(APPLICATION_NS)) . '/classes/controller/'; $class = '\\' . (isset(static::$packages[$package]) ? ucfirst($package) : ucfirst(APPLICATION_NS)) . '\\Controller\\'; if (!isset(static::$packages[$package])) { array_unshift($segments, $package); $package = APPLICATION_NS; } $current = array_shift($segments); while (is_dir($path . $current)) { $path .= strtolower($current) . '/'; $class .= ucfirst($current) . '\\'; $current = array_shift($segments); } $response = \Nerd\Http\Response::make(); $action = array_shift($segments); if (file_exists($path . $current . '.php')) { $class .= ucfirst($current); try { $controller = new $class(); } catch (\Exception $e) { throw new \Nerd\Http\Exception(500, $response); } $action = 'action' . ucfirst($action ?: 'index'); if (!method_exists($controller, $action)) { throw new \Nerd\Http\Exception(404); } $controller->before(); $response->setBody(call_user_func_array(array($controller, $action), $segments)); $controller->after(); return $response; } throw new \Nerd\Http\Exception(404, $response); }