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' ], 'database' => [ 'host' => '127.0.0.1', 'database' => 'slovocast', 'username' => 'slovocast', 'password' => 'Password01', ] ]); return $config; } /** * Set up the Container with all of its definitions, as well as * initialization of configuration. * * @return Container * @throws \Exception */ 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 */ DatabaseConnectionInterface::class => function(ContainerInterface $container) { $databaseConfig = $container->get('config')->get('database'); $dsn = sprintf( "%s:dbname=%s;host=%s", $databaseConfig['driver'], $databaseConfig['database'], $databaseConfig['host'] ); $username = $databaseConfig['username']; $password = $databaseConfig['password']; $connection = new PdoDatabaseConnection($dsn, $username, $password); return $connection; }, /** * 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 instantiated 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 * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws LoaderError */ protected static function establishMiddleware(App $app): void { /** @var ContainerInterface $container */ $container = $app->getContainer(); /** * @var Configuration $config */ $config = $container->get('config'); // Twig $templateCache = false; $twig = Twig::create(APP_TEMPLATES_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 * @throws ContainerExceptionInterface * @throws LoaderError * @throws NotFoundExceptionInterface * @throws \Exception */ public static function init(): App { $container = self::initContainer(); $app = AppFactory::createFromContainer($container); self::establishMiddleware($app); self::establishRoutes($app); return $app; } }