Ejemplo n.º 1
0
 public function load($routes_file)
 {
     $this->app->coreLogger()->addInfo("Loading routes.");
     $routes = function_exists('get_object_vars') ? get_object_vars(Config::get(Config::get($this->app->cfile)->paths->routing_file)) : $this->readJson(ROOT . "/config/{$routes_file}.json");
     foreach ($routes as $route_name => $route_settings) {
         try {
             $this->routes[$route_name] = new Route($route_name, $route_settings);
         } catch (InvalidArgumentException $e) {
             $this->app->coreLogger()->addError("Router error: {message}\n", array('message' => $e->getMessage()));
         }
     }
 }
Ejemplo n.º 2
0
    /**
     * Displays the 403 page. User should write a 403 template and set it in the config file.
     *
     * @param string  $template
     * @param string $c_type
     * @return bool
     */
    public function display403($template = null, $c_type = self::TYPE_HTML)
    {
        if ($template) {
            $content = $this->environment - $this->render($template);
        } elseif (isset(Config::get($this->app->cfile)->page403) && ($tpl = Config::get($this->app->cfile)->page403)) {
            $content = $this->environment->render($tpl);
        } else {
            $content = <<<EOC
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>Forbidden area</title>
    </head>
    <body>
        <p>You shall not pass!</p>
    </body>
</html>
EOC;
        }
        ob_clean();
        ob_start();
        header("HTTP/1.1 . " . self::STATUS_FORBIDDEN);
        header("Content-Type: " . $c_type);
        echo $content;
        ob_end_flush();
        $this->app->end();
        exit;
    }
Ejemplo n.º 3
0
    public function run()
    {
        try {
            $this->request->proceed();
            $this->start();
            $controller_name = ucfirst($this->request->getController());
            $action_name = $this->request->getAction();
            $class = '\\' . ucfirst(Config::get($this->cfile)->project_name) . '\\' . $controller_name;
            if (!class_exists($class)) {
                $msg = "Controller {$class} does not exist in this application.";
                throw new \Exception($msg);
            }
            $this->controller = new $class($this);
            $this->before();
            if (!method_exists($this->controller, $action_name)) {
                $msg = "Action {$action_name} does not exist in {$controller_name}.";
                throw new \Exception($msg);
            }
            if (($content_type = $this->controller->{$action_name}()) === false) {
                $msg = "Error executing the action. Good luck.";
                throw new \Exception($msg);
            }
            $this->after();
            $this->coreLogger()->debug('Value of $content-type: ' . $content_type);
            if ($content_type != Response::TYPE_EXIT) {
                $this->response->setTemplate($this->controller->getTemplate($action_name));
                $this->response->render($content_type);
            }
            $this->end();
        } catch (\Exception $e) {
            $this->coreLogger()->addCritical("Could not run the application correctly.\nReason: {reason}\nTrace: {trace}", array('reason' => $e->getMessage(), 'trace' => $e->getTraceAsString()));
            header('HTTP/1.1 500 Internal Server Error');
            header('Content-Type: text/html');
            $content = <<<EOT
<!DOCTYPE html>
<html lang="fr">
    <head>
        <title>Internal Server Error</title>
        <meta charset="UTF-8"/>
    </head>
    <body>
        <h1>C’est dommage !</h1>
        <p>C’est con mais ça marche pas. Non, pas du tout. Allez, comme t’es gentil, je te mets l’erreur. Pas de bêtises
        la prochaine fois !</p>
        <pre>
        Tu es un pingouin. Allez, va prévenir l’administrateur !
        </pre>
    </body>
</html>
EOT;
            echo $content;
        }
    }
Ejemplo n.º 4
0
 public function __construct($app)
 {
     $this->app = $app;
     $this->conf = Config::get($this->app->cfile);
 }