2024-05-14 02:06:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Slovocast;
|
|
|
|
|
|
|
|
use Slim\App;
|
|
|
|
use Slim\Factory\AppFactory;
|
2024-05-26 02:47:52 +00:00
|
|
|
|
2024-05-14 02:06:01 +00:00
|
|
|
use League\Config\Configuration;
|
2024-05-26 02:47:52 +00:00
|
|
|
|
2024-06-03 02:11:24 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2024-05-14 02:06:01 +00:00
|
|
|
use DI\Container;
|
|
|
|
use DI\ContainerBuilder;
|
2024-05-26 02:47:52 +00:00
|
|
|
|
2024-05-16 02:03:49 +00:00
|
|
|
use Slim\Views\Twig;
|
|
|
|
use Slim\Views\TwigMiddleware;
|
2024-05-26 02:47:52 +00:00
|
|
|
|
2024-05-16 02:03:49 +00:00
|
|
|
use Slovocast\Routes;
|
2024-05-26 01:44:57 +00:00
|
|
|
use Slovocast\Configuration\SiteInformationSchema;
|
|
|
|
use Slovocast\Configuration\DatabaseConnectionSchema;
|
2024-06-03 02:11:24 +00:00
|
|
|
use Slovocast\Configuration\SessionSchema;
|
2024-05-14 02:06:01 +00:00
|
|
|
|
2024-05-26 02:47:52 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Monolog\Logger;
|
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Level;
|
2024-06-03 02:11:24 +00:00
|
|
|
use Odan\Session\PhpSession;
|
|
|
|
use Odan\Session\SessionInterface;
|
|
|
|
use Odan\Session\SessionManagerInterface;
|
2024-05-26 02:47:52 +00:00
|
|
|
|
2024-05-14 02:06:01 +00:00
|
|
|
class Bootstrap
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Pulls out all the configuration schemas and configuration values.
|
|
|
|
*
|
|
|
|
* @return Configuration
|
|
|
|
*/
|
|
|
|
protected static function initConfig(): Configuration
|
|
|
|
{
|
|
|
|
$config = new Configuration();
|
|
|
|
|
|
|
|
// set all configuration details
|
2024-05-26 01:44:57 +00:00
|
|
|
$config->addSchema('site', SiteInformationSchema::getSchema());
|
|
|
|
$config->addSchema('database', DatabaseConnectionSchema::getSchema());
|
2024-06-03 02:11:24 +00:00
|
|
|
$config->addSchema('session', SessionSchema::getSchema());
|
2024-05-14 02:06:01 +00:00
|
|
|
|
2024-05-24 02:39:38 +00:00
|
|
|
$config->merge([
|
|
|
|
'site' => [
|
|
|
|
'name' => "Slovocast",
|
2024-05-26 01:52:49 +00:00
|
|
|
'description' => "A no-bullshit podcast hosting platform.",
|
|
|
|
'tags' => [
|
|
|
|
'podcast', 'hosting', 'indie', 'independent', 'easy'
|
|
|
|
]
|
2024-05-24 02:39:38 +00:00
|
|
|
],
|
2024-06-03 02:11:24 +00:00
|
|
|
'session' => [
|
|
|
|
'name' => 'slovocast'
|
|
|
|
]
|
2024-05-24 02:39:38 +00:00
|
|
|
]);
|
|
|
|
|
2024-05-14 02:06:01 +00:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set up the Container with all of its definitions, as well as
|
|
|
|
* initialization of configuration.
|
|
|
|
*
|
|
|
|
* @return Container
|
|
|
|
*/
|
|
|
|
protected static function initContainer(): Container
|
|
|
|
{
|
|
|
|
$containerBuilder = new ContainerBuilder();
|
|
|
|
|
|
|
|
$config = self::initConfig();
|
|
|
|
$containerBuilder->addDefinitions([
|
2024-05-24 02:39:38 +00:00
|
|
|
'config' => $config,
|
2024-05-14 02:06:01 +00:00
|
|
|
// more DI stuff
|
2024-05-26 02:23:53 +00:00
|
|
|
'flash' => function () {
|
|
|
|
$messages = [];
|
|
|
|
return new \Slim\Flash\Messages($messages);
|
2024-05-26 02:47:52 +00:00
|
|
|
},
|
|
|
|
LoggerInterface::class => function() {
|
|
|
|
$logger = new Logger();
|
|
|
|
$logger->pushHandler(
|
|
|
|
new StreamHandler(__DIR__ . '/var/logs', Level::Warning)
|
|
|
|
);
|
|
|
|
return $logger;
|
2024-06-03 02:11:24 +00:00
|
|
|
},
|
|
|
|
SessionManagerInterface::class => function (ContainerInterface $container) {
|
|
|
|
return $container->get(SessionInterface::class);
|
|
|
|
},
|
|
|
|
SessionInterface::class => function (ContainerInterface $container) {
|
|
|
|
$options = $container->get('config')->get('session');
|
|
|
|
return new PhpSession($options);
|
2024-05-26 02:23:53 +00:00
|
|
|
}
|
2024-05-14 02:06:01 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return $containerBuilder->build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tasking the instaniated Application, sets up all the routes for the
|
|
|
|
* application.
|
|
|
|
*
|
|
|
|
* @param App $app
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function establishRoutes(App $app): void
|
|
|
|
{
|
2024-05-16 02:03:49 +00:00
|
|
|
Routes::init($app);
|
2024-05-14 02:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Taking an instantiated application, sets up all the middleware required
|
|
|
|
* for the application to run.
|
|
|
|
*
|
|
|
|
* @param App $app
|
|
|
|
*/
|
|
|
|
protected static function establishMiddleware(App $app): void
|
|
|
|
{
|
2024-05-24 02:39:38 +00:00
|
|
|
/**
|
|
|
|
* @var Configuration
|
|
|
|
*/
|
2024-05-26 01:44:57 +00:00
|
|
|
$config = $app->getContainer()->get('config');
|
2024-05-14 02:06:01 +00:00
|
|
|
$app->addErrorMiddleware(true, true, true);
|
2024-05-16 02:03:49 +00:00
|
|
|
|
|
|
|
// Twig
|
2024-05-16 02:15:24 +00:00
|
|
|
$templatePath = __DIR__ . "/../templates";
|
2024-05-23 00:39:33 +00:00
|
|
|
$templateCache = false;
|
|
|
|
$twig = Twig::create($templatePath, [ 'cache' => $templateCache ]);
|
2024-05-25 16:02:18 +00:00
|
|
|
|
2024-05-23 02:47:20 +00:00
|
|
|
// Add the global variables
|
2024-05-24 02:39:38 +00:00
|
|
|
$twig->getEnvironment()
|
|
|
|
->addGlobal('site_name', $config->get('site.name'));
|
|
|
|
$twig->getEnvironment()
|
|
|
|
->addGlobal('site_description', $config->get('site.description'));
|
2024-05-23 00:41:44 +00:00
|
|
|
$app->add(TwigMiddleware::create($app, $twig));
|
2024-05-26 01:44:57 +00:00
|
|
|
|
2024-05-26 02:23:53 +00:00
|
|
|
// Flash Messages
|
|
|
|
$app->add(function ($req, $next) {
|
|
|
|
if (session_status() !== PHP_SESSION_ACTIVE) {
|
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
|
2024-05-26 02:47:52 +00:00
|
|
|
/** @var $this ContainerInterface */
|
2024-05-26 02:23:53 +00:00
|
|
|
$this->get('flash')->__construct($_SESSION);
|
|
|
|
|
|
|
|
return $next->handle($req);
|
|
|
|
});
|
2024-05-14 02:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates the application.
|
|
|
|
*
|
|
|
|
* @return App
|
|
|
|
*/
|
|
|
|
public static function init(): App
|
|
|
|
{
|
|
|
|
$container = self::initContainer();
|
|
|
|
$app = AppFactory::createFromContainer($container);
|
|
|
|
self::establishMiddleware($app);
|
|
|
|
self::establishRoutes($app);
|
|
|
|
return $app;
|
|
|
|
}
|
|
|
|
}
|