XBackBone/bootstrap/app.php

110 lines
3.4 KiB
PHP
Raw Normal View History

2018-04-28 12:20:07 +00:00
<?php
2019-11-12 23:13:23 +00:00
use App\Exception\Handlers\AppErrorHandler;
use App\Exception\Handlers\Renderers\HtmlErrorRenderer;
2019-11-13 12:02:31 +00:00
use App\Factories\ViewFactory;
use App\Middleware\InjectMiddleware;
2019-11-19 12:59:17 +00:00
use App\Middleware\LangMiddleware;
use App\Middleware\RememberMiddleware;
2019-11-19 11:32:58 +00:00
use App\Web\View;
2019-11-12 23:13:23 +00:00
use DI\Bridge\Slim\Bridge;
use DI\ContainerBuilder;
use function DI\factory;
2019-11-19 11:32:58 +00:00
use function DI\get;
2019-11-12 23:13:23 +00:00
use function DI\value;
2019-11-20 17:49:31 +00:00
use Psr\Container\ContainerInterface as Container;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
2018-04-28 12:20:07 +00:00
if (!file_exists('config.php') && is_dir('install/')) {
2019-11-12 23:13:23 +00:00
header('Location: ./install/');
exit();
} else {
if (!file_exists('config.php') && !is_dir('install/')) {
exit('Cannot find the config file.');
}
}
2018-10-13 16:25:48 +00:00
// Load the config
2019-11-21 17:00:47 +00:00
$config = array_replace_recursive([
2019-11-20 17:49:31 +00:00
'app_name' => 'XBackBone',
'base_url' => isset($_SERVER['HTTPS']) ? 'https://'.$_SERVER['HTTP_HOST'] : 'http://'.$_SERVER['HTTP_HOST'],
'debug' => false,
2019-11-12 23:13:23 +00:00
'maintenance' => false,
2019-11-21 17:00:47 +00:00
'db' => [
2019-11-12 23:13:23 +00:00
'connection' => 'sqlite',
2019-11-20 17:49:31 +00:00
'dsn' => BASE_DIR.'resources/database/xbackbone.db',
'username' => null,
'password' => null,
2019-11-21 17:00:47 +00:00
],
'storage' => [
2019-11-12 23:13:23 +00:00
'driver' => 'local',
2019-11-20 17:49:31 +00:00
'path' => realpath(__DIR__.'/').DIRECTORY_SEPARATOR.'storage',
2019-11-21 17:00:47 +00:00
],
], require BASE_DIR.'config.php');
2019-11-12 23:13:23 +00:00
$builder = new ContainerBuilder();
if (!$config['debug']) {
$builder->enableCompilation(BASE_DIR.'/resources/cache/di/');
$builder->writeProxiesToFile(true, BASE_DIR.'/resources/cache/proxies');
}
2019-11-19 11:32:58 +00:00
2019-11-21 17:00:47 +00:00
$builder->addDefinitions([
2019-11-20 17:49:31 +00:00
'config' => value($config),
2019-11-19 11:32:58 +00:00
View::class => factory(function (Container $container) {
2019-11-13 12:02:31 +00:00
return ViewFactory::createAppInstance($container);
2019-11-12 23:13:23 +00:00
}),
2019-11-19 11:32:58 +00:00
'view' => get(View::class),
2019-11-21 17:00:47 +00:00
]);
2019-11-12 23:13:23 +00:00
2019-11-19 11:32:58 +00:00
$builder->addDefinitions(__DIR__.'/container.php');
2019-11-12 23:13:23 +00:00
$app = Bridge::create($builder->build());
2019-11-19 11:32:58 +00:00
$app->setBasePath(parse_url($config['base_url'], PHP_URL_PATH) ?: '');
2019-11-12 23:13:23 +00:00
if (!$config['debug']) {
$app->getRouteCollector()->setCacheFile(BASE_DIR.'resources/cache/routes.cache.php');
2018-11-17 20:54:05 +00:00
}
$app->add(InjectMiddleware::class);
2019-11-19 12:59:17 +00:00
$app->add(LangMiddleware::class);
$app->add(RememberMiddleware::class);
2018-04-28 12:20:07 +00:00
// Permanently redirect paths with a trailing slash to their non-trailing counterpart
2019-11-18 10:42:42 +00:00
$app->add(function (Request $request, RequestHandler $handler) use (&$app, &$config) {
2019-11-12 23:13:23 +00:00
$uri = $request->getUri();
$path = $uri->getPath();
2019-11-18 10:42:42 +00:00
if ($path !== $app->getBasePath().'/' && substr($path, -1) === '/') {
2019-11-12 23:13:23 +00:00
// permanently redirect paths with a trailing slash
// to their non-trailing counterpart
$uri = $uri->withPath(substr($path, 0, -1));
2019-11-18 10:42:42 +00:00
if ($request->getMethod() === 'GET') {
return $app->getResponseFactory()
->createResponse(301)
2019-11-20 17:49:31 +00:00
->withHeader('Location', (string) $uri);
2019-11-12 23:13:23 +00:00
} else {
$request = $request->withUri($uri);
}
}
return $handler->handle($request);
});
$app->addRoutingMiddleware();
2019-11-12 23:13:23 +00:00
// Configure the error handler
$errorHandler = new AppErrorHandler($app->getCallableResolver(), $app->getResponseFactory());
$errorHandler->registerErrorRenderer('text/html', HtmlErrorRenderer::class);
// Add Error Middleware
$errorMiddleware = $app->addErrorMiddleware($config['debug'], true, true);
$errorMiddleware->setDefaultErrorHandler($errorHandler);
// Load the application routes
require BASE_DIR.'app/routes.php';
2019-11-20 17:49:31 +00:00
return $app;