addSchema('site', SiteInformationSchema::getSchema()); $config->addSchema('database', DatabaseConnectionSchema::getSchema()); $config->addSchema('session', SessionSchema::getSchema()); $config->merge([ 'site' => [ 'name' => "Slovocast", 'description' => "A no-bullshit podcast hosting platform.", 'tags' => [ 'podcast', 'hosting', 'indie', 'independent', 'easy' ] ], 'session' => [ 'name' => 'slovocast' ] ]); 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([ 'config' => $config, // more DI stuff 'flash' => function (ContainerInterface $container) { return $container->get(SessionInterface::class)->getFlash(); }, LoggerInterface::class => function() { $logger = new Logger(); $logger->pushHandler( new StreamHandler(__DIR__ . '/var/logs', Level::Warning) ); return $logger; }, 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); } ]); 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 { Routes::init($app); } /** * Taking an instantiated application, sets up all the middleware required * for the application to run. This appends Middleware in a Global sense, * not per-route. Per-route middleware will be set in the `establishRoutes` * method. * * @param App $app */ protected static function establishMiddleware(App $app): void { /** @var ContainerInterface $container */ $container = $app->getContainer(); /** * @var Configuration */ $config = $container->get('config'); // Twig $templatePath = __DIR__ . "/../templates"; $templateCache = false; $twig = Twig::create($templatePath, [ 'cache' => $templateCache ]); // Add the global variables $twig->getEnvironment() ->addGlobal('site_name', $config->get('site.name')); $twig->getEnvironment() ->addGlobal('site_description', $config->get('site.description')); $flash = $container->get(SessionInterface::class)->getFlash(); $twig->getEnvironment() ->addGlobal('flash', $flash); $app->add(TwigMiddleware::create($app, $twig)); // Add the error handling middleware $app->addErrorMiddleware(true, true, true); } /** * 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; } }