szurubooru/src/Bootstrap.php
2014-03-03 21:46:36 +01:00

70 lines
1.7 KiB
PHP

<?php
use \Chibi\Database as Database;
class Bootstrap
{
public function render($callback = null)
{
if ($callback !== null)
$callback();
else
(new \Chibi\View())->renderFile($this->context->layoutName);
}
public function workWrapper($workCallback)
{
$this->config->chibi->baseUrl = 'http://' . rtrim($_SERVER['HTTP_HOST'], '/') . '/';
$this->context->viewDecorators []= new CustomAssetViewDecorator();
$this->context->viewDecorators []= new \Chibi\PrettyPrintViewDecorator();
CustomAssetViewDecorator::setTitle($this->config->main->title);
$this->context->handleExceptions = false;
$this->context->json = isset($_GET['json']);
$this->context->layoutName = $this->context->json
? 'layout-json'
: 'layout-normal';
$this->context->transport = new StdClass;
StatusHelper::init();
session_start();
AuthController::doLogIn();
if (empty($this->context->route))
{
http_response_code(404);
$this->context->viewName = 'error-404';
$this->render();
return;
}
try
{
$this->render($workCallback);
}
catch (\Chibi\MissingViewFileException $e)
{
$this->context->json = true;
$this->context->layoutName = 'layout-json';
$this->render();
}
catch (SimpleException $e)
{
if ($e instanceof SimpleNotFoundException)
http_response_code(404);
StatusHelper::failure($e->getMessage());
if (!$this->context->handleExceptions)
$this->context->viewName = 'message';
$this->render();
}
catch (Exception $e)
{
StatusHelper::failure($e->getMessage());
$this->context->transport->exception = $e;
$this->context->transport->queries = Database::getLogs();
$this->context->viewName = 'error-exception';
$this->render();
}
AuthController::observeWorkFinish();
}
}