XBackBone/bootstrap/app.php

129 lines
4.5 KiB
PHP
Raw Normal View History

2018-04-28 12:20:07 +00:00
<?php
use App\Database\DB;
2018-11-19 11:19:47 +00:00
use App\Web\Lang;
2018-11-11 16:02:50 +00:00
use App\Web\Session;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Slim\App;
use Slim\Container;
2018-04-28 12:20:07 +00:00
if (!file_exists('config.php') && is_dir('install/')) {
header('Location: ./install/');
exit();
} else if (!file_exists('config.php') && !is_dir('install/')) {
die('Cannot find the config file.');
}
2018-10-13 16:25:48 +00:00
// Load the config
2018-04-28 12:20:07 +00:00
$config = array_replace_recursive([
'app_name' => 'XBackBone',
'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'],
'storage_dir' => 'storage',
2018-11-11 16:02:50 +00:00
'displayErrorDetails' => false,
2018-04-28 12:20:07 +00:00
'db' => [
'connection' => 'sqlite',
'dsn' => __DIR__ . '/../resources/database/xbackbone.db',
2018-04-28 12:20:07 +00:00
'username' => null,
'password' => null,
],
], require __DIR__ . '/../config.php');
2018-04-28 12:20:07 +00:00
2018-11-17 20:54:05 +00:00
if (!$config['displayErrorDetails']) {
$config['routerCacheFile'] = __DIR__ . '/../resources/cache/routes.cache.php';
}
2018-11-11 16:02:50 +00:00
$container = new Container(['settings' => $config]);
$container['logger'] = function ($container) {
$logger = new Logger('app');
$streamHandler = new RotatingFileHandler(__DIR__ . '/../logs/log.txt', 10, Logger::DEBUG);
$streamHandler->setFormatter(new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", "Y-m-d H:i:s", true));
$logger->pushHandler($streamHandler);
return $logger;
};
2018-11-11 18:18:27 +00:00
// Session init
Session::init('xbackbone_session', __DIR__ . '/../resources/sessions');
2018-04-28 12:20:07 +00:00
2018-10-13 16:25:48 +00:00
// Set the database dsn
$dsn = $config['db']['connection'] === 'sqlite' ? __DIR__ . '/../' . $config['db']['dsn'] : $config['db']['dsn'];
DB::setDsn($config['db']['connection'] . ':' . $dsn, $config['db']['username'], $config['db']['password']);
2018-04-28 12:20:07 +00:00
2018-11-11 16:02:50 +00:00
$container['database'] = function ($container) use (&$config) {
return DB::getInstance();
};
2018-11-19 18:04:43 +00:00
Lang::build(substr(@$_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2), __DIR__. '/../resources/lang/');
2018-11-19 11:19:47 +00:00
2018-11-11 16:02:50 +00:00
$container['view'] = function ($container) use (&$config) {
$view = new \Slim\Views\Twig(__DIR__ . '/../resources/templates', [
'cache' => __DIR__ . '/../resources/cache',
2018-11-11 16:02:50 +00:00
'autoescape' => 'html',
'debug' => $config['displayErrorDetails'],
'auto_reload' => $config['displayErrorDetails'],
]);
// Instantiate and add Slim specific extension
$router = $container->get('router');
$uri = \Slim\Http\Uri::createFromEnvironment(new \Slim\Http\Environment($_SERVER));
$view->addExtension(new Slim\Views\TwigExtension($router, $uri));
$view->getEnvironment()->addGlobal('config', $config);
$view->getEnvironment()->addGlobal('request', $container->get('request'));
$view->getEnvironment()->addGlobal('alerts', Session::getAlert());
$view->getEnvironment()->addGlobal('session', Session::all());
2018-11-19 18:45:52 +00:00
$view->getEnvironment()->addGlobal('current_lang', Lang::getInstance()->getLang());
2018-11-11 16:02:50 +00:00
$view->getEnvironment()->addGlobal('PLATFORM_VERSION', PLATFORM_VERSION);
2018-11-19 18:04:43 +00:00
$view->getEnvironment()->addFunction(new Twig_Function('route', 'route'));
$view->getEnvironment()->addFunction(new Twig_Function('lang', 'lang'));
$view->getEnvironment()->addFunction(new Twig_Function('urlFor', 'urlFor'));
2018-11-11 16:02:50 +00:00
return $view;
};
$container['errorHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response, $exception) use (&$container) {
if ($exception instanceof \App\Exceptions\UnauthorizedException) {
return $container->view->render($response->withStatus(403), 'errors/403.twig');
}
$container->logger->critical('Fatal error during app execution', [$exception, $exception->getTraceAsString()]);
return $container->view->render($response->withStatus(500), 'errors/500.twig', ['exception' => $exception]);
};
};
2018-11-11 18:18:27 +00:00
2018-11-11 16:02:50 +00:00
$container['notFoundHandler'] = function ($container) {
return function (\Slim\Http\Request $request, \Slim\Http\Response $response) use (&$container) {
$response->withStatus(404)->withHeader('Content-Type', 'text/html');
return $container->view->render($response, 'errors/404.twig');
};
};
$app = new App($container);
2018-04-28 12:20:07 +00:00
// Permanently redirect paths with a trailing slash to their non-trailing counterpart
$app->add(function (\Slim\Http\Request $request, \Slim\Http\Response $response, callable $next) {
$uri = $request->getUri();
$path = $uri->getPath();
if ($path !== '/' && substr($path, -1) === '/') {
$uri = $uri->withPath(substr($path, 0, -1));
if ($request->getMethod() === 'GET') {
return $response->withRedirect((string)$uri, 301);
} else {
return $next($request->withUri($uri), $response);
}
}
return $next($request, $response);
});
2018-10-13 16:25:48 +00:00
// Load the application routes
2018-04-28 12:20:07 +00:00
require 'app/routes.php';