2013-10-05 17:12:51 +02:00
|
|
|
<?php
|
2014-02-28 20:35:12 +01:00
|
|
|
use \Chibi\Database as Database;
|
|
|
|
|
2013-10-05 17:12:51 +02:00
|
|
|
class Bootstrap
|
|
|
|
{
|
2014-02-05 08:22:26 +01:00
|
|
|
public function render($callback = null)
|
|
|
|
{
|
2014-02-27 15:04:36 +01:00
|
|
|
if ($callback !== null)
|
2014-02-05 08:22:26 +01:00
|
|
|
$callback();
|
|
|
|
else
|
2014-02-27 15:04:36 +01:00
|
|
|
(new \Chibi\View())->renderFile($this->context->layoutName);
|
2014-02-05 08:22:26 +01:00
|
|
|
}
|
|
|
|
|
2013-10-05 17:12:51 +02:00
|
|
|
public function workWrapper($workCallback)
|
|
|
|
{
|
2013-10-05 19:24:08 +02:00
|
|
|
$this->config->chibi->baseUrl = 'http://' . rtrim($_SERVER['HTTP_HOST'], '/') . '/';
|
2014-03-03 21:46:36 +01:00
|
|
|
$this->context->viewDecorators []= new CustomAssetViewDecorator();
|
|
|
|
$this->context->viewDecorators []= new \Chibi\PrettyPrintViewDecorator();
|
2014-02-27 15:04:36 +01:00
|
|
|
CustomAssetViewDecorator::setTitle($this->config->main->title);
|
2013-10-05 22:52:55 +02:00
|
|
|
|
2014-03-03 21:46:36 +01:00
|
|
|
$this->context->handleExceptions = false;
|
2013-10-28 11:19:15 +01:00
|
|
|
$this->context->json = isset($_GET['json']);
|
|
|
|
$this->context->layoutName = $this->context->json
|
2013-10-05 17:12:51 +02:00
|
|
|
? 'layout-json'
|
|
|
|
: 'layout-normal';
|
|
|
|
$this->context->transport = new StdClass;
|
2013-11-16 18:40:26 +01:00
|
|
|
StatusHelper::init();
|
2013-10-05 17:12:51 +02:00
|
|
|
|
2014-03-03 21:46:36 +01:00
|
|
|
session_start();
|
2013-10-27 20:39:32 +01:00
|
|
|
AuthController::doLogIn();
|
2013-10-05 19:24:08 +02:00
|
|
|
|
2013-10-05 17:12:51 +02:00
|
|
|
if (empty($this->context->route))
|
|
|
|
{
|
2014-02-05 08:32:19 +01:00
|
|
|
http_response_code(404);
|
2013-10-05 17:12:51 +02:00
|
|
|
$this->context->viewName = 'error-404';
|
2014-02-05 08:22:26 +01:00
|
|
|
$this->render();
|
2013-10-05 17:12:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-02-05 08:22:26 +01:00
|
|
|
$this->render($workCallback);
|
2013-10-05 17:12:51 +02:00
|
|
|
}
|
2013-11-05 09:27:34 +01:00
|
|
|
catch (\Chibi\MissingViewFileException $e)
|
|
|
|
{
|
|
|
|
$this->context->json = true;
|
|
|
|
$this->context->layoutName = 'layout-json';
|
2014-02-05 08:22:26 +01:00
|
|
|
$this->render();
|
2013-11-05 09:27:34 +01:00
|
|
|
}
|
2013-11-16 18:40:26 +01:00
|
|
|
catch (SimpleException $e)
|
|
|
|
{
|
2014-02-05 08:32:19 +01:00
|
|
|
if ($e instanceof SimpleNotFoundException)
|
|
|
|
http_response_code(404);
|
2014-02-20 17:38:32 +01:00
|
|
|
StatusHelper::failure($e->getMessage());
|
2013-11-16 18:40:26 +01:00
|
|
|
if (!$this->context->handleExceptions)
|
|
|
|
$this->context->viewName = 'message';
|
2014-02-05 08:22:26 +01:00
|
|
|
$this->render();
|
2013-11-16 18:40:26 +01:00
|
|
|
}
|
2013-10-05 17:12:51 +02:00
|
|
|
catch (Exception $e)
|
|
|
|
{
|
2014-02-20 17:38:32 +01:00
|
|
|
StatusHelper::failure($e->getMessage());
|
2013-10-06 19:53:33 +02:00
|
|
|
$this->context->transport->exception = $e;
|
2013-12-18 15:10:53 +01:00
|
|
|
$this->context->transport->queries = Database::getLogs();
|
2013-10-05 17:12:51 +02:00
|
|
|
$this->context->viewName = 'error-exception';
|
2014-02-05 08:22:26 +01:00
|
|
|
$this->render();
|
2013-10-05 17:12:51 +02:00
|
|
|
}
|
2013-10-30 23:24:27 +01:00
|
|
|
|
|
|
|
AuthController::observeWorkFinish();
|
2013-10-05 17:12:51 +02:00
|
|
|
}
|
|
|
|
}
|