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, LoggerInterface::class => function() { $logger = new Logger(); $logger->pushHandler( new StreamHandler(APP_LOG_DIR, Level::Warning) ); return $logger; }, /** * Session and Flash classes here */ 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); }, 'flash' => function (ContainerInterface $container) { return $container->get(SessionInterface::class)->getFlash(); }, /** * Application DI */ ResponseFactoryInterface::class => function (ContainerInterface $container) { return new ResponseFactory(); }, /** * Database Connections */ /** * Utility classes */ UserAuthorizationInterface::class => function(ContainerInterface $container) { return new BasicUserAuthorization(); }, /** * Add the Domain Repositories Here */ UserRepositoryInterface::class => function (ContainerInterface $container) { return new UserRepository( $container->get(DatabaseConnectionInterface::class), $container->get(UserAuthorizationInterface::class) ); } ]); 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 $templateCache = false; $twig = Twig::create(APP_LOG_DIR, [ '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; } }