2024-05-14 02:06:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Slovocast;
|
|
|
|
|
2024-06-27 01:10:48 +00:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2024-05-14 02:06:01 +00:00
|
|
|
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-06-17 00:44:12 +00:00
|
|
|
use Psr\Http\Message\ResponseFactoryInterface;
|
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-06-17 00:44:12 +00:00
|
|
|
use Slim\Psr7\Factory\ResponseFactory;
|
2024-05-26 02:47:52 +00:00
|
|
|
|
2024-05-16 02:03:49 +00:00
|
|
|
use Slovocast\Routes;
|
2024-06-18 01:49:09 +00:00
|
|
|
use Slovocast\Configuration\{
|
|
|
|
SiteInformationSchema,
|
|
|
|
DatabaseConnectionSchema,
|
|
|
|
SessionSchema
|
|
|
|
};
|
2024-06-27 01:10:48 +00:00
|
|
|
use Twig\Error\LoaderError;
|
2024-06-18 01:49:09 +00:00
|
|
|
use Slovocast\Domain\Repository\{
|
|
|
|
UserRepositoryInterface,
|
|
|
|
UserRepository
|
|
|
|
};
|
|
|
|
use Slovocast\Infrastructure\{
|
|
|
|
DatabaseConnectionInterface,
|
2024-06-27 01:10:48 +00:00
|
|
|
PdoDatabaseConnection,
|
2024-06-18 01:49:09 +00:00
|
|
|
User\UserAuthorizationInterface,
|
|
|
|
User\BasicUserAuthorization
|
|
|
|
};
|
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;
|
2024-06-27 01:10:48 +00:00
|
|
|
use Odan\Session\SessionManagerInterface;
|
2024-05-26 02:47:52 +00:00
|
|
|
|
2024-06-18 01:49:09 +00:00
|
|
|
/**
|
|
|
|
* Defines here are used globally
|
|
|
|
*/
|
|
|
|
define('APP_ROOT_DIR', __DIR__ . '/..');
|
|
|
|
define('APP_SRC_DIR', __DIR__);
|
|
|
|
define('APP_PUBLIC_DIR', __DIR__ . '/../public');
|
|
|
|
define('APP_TEMPLATES_DIR', __DIR__ . '/../templates');
|
|
|
|
define('APP_LOGS_DIR', __DIR__ . '/../var/logs');
|
|
|
|
define('APP_TEMP_DIR', __DIR__ . '/../var/temp');
|
|
|
|
|
2024-05-14 02:06:01 +00:00
|
|
|
class Bootstrap
|
|
|
|
{
|
|
|
|
/**
|
2024-06-27 01:10:48 +00:00
|
|
|
* Pulls out all the configuration schemas and configuration values.
|
2024-05-14 02:06:01 +00:00
|
|
|
*
|
|
|
|
* @return Configuration
|
|
|
|
*/
|
|
|
|
protected static function initConfig(): Configuration
|
|
|
|
{
|
|
|
|
$config = new Configuration();
|
2024-06-18 01:49:09 +00:00
|
|
|
$dotenv = \DotenvVault\DotenvVault::createImmutable(APP_ROOT_DIR);
|
2024-05-14 02:06:01 +00:00
|
|
|
|
|
|
|
// 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-06-27 01:10:48 +00:00
|
|
|
],
|
|
|
|
'database' => [
|
|
|
|
'host' => '127.0.0.1',
|
|
|
|
'database' => 'slovocast',
|
|
|
|
'username' => 'slovocast',
|
|
|
|
'password' => 'Password01',
|
2024-06-03 02:11:24 +00:00
|
|
|
]
|
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
|
2024-06-27 01:10:48 +00:00
|
|
|
* @throws \Exception
|
2024-05-14 02:06:01 +00:00
|
|
|
*/
|
2024-06-27 01:10:48 +00:00
|
|
|
protected static function initContainer(): Container
|
2024-05-14 02:06:01 +00:00
|
|
|
{
|
|
|
|
$containerBuilder = new ContainerBuilder();
|
|
|
|
|
|
|
|
$config = self::initConfig();
|
|
|
|
$containerBuilder->addDefinitions([
|
2024-05-24 02:39:38 +00:00
|
|
|
'config' => $config,
|
2024-05-26 02:47:52 +00:00
|
|
|
LoggerInterface::class => function() {
|
|
|
|
$logger = new Logger();
|
|
|
|
$logger->pushHandler(
|
2024-06-18 01:49:09 +00:00
|
|
|
new StreamHandler(APP_LOG_DIR, Level::Warning)
|
2024-05-26 02:47:52 +00:00
|
|
|
);
|
|
|
|
return $logger;
|
2024-06-03 02:11:24 +00:00
|
|
|
},
|
2024-06-18 01:49:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Session and Flash classes here
|
|
|
|
*/
|
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-06-17 00:44:12 +00:00
|
|
|
},
|
2024-06-18 01:49:09 +00:00
|
|
|
'flash' => function (ContainerInterface $container) {
|
|
|
|
return $container->get(SessionInterface::class)->getFlash();
|
|
|
|
},
|
2024-06-27 01:10:48 +00:00
|
|
|
|
2024-06-18 01:49:09 +00:00
|
|
|
/**
|
|
|
|
* Application DI
|
|
|
|
*/
|
2024-06-17 00:44:12 +00:00
|
|
|
ResponseFactoryInterface::class => function (ContainerInterface $container) {
|
|
|
|
return new ResponseFactory();
|
2024-06-17 01:55:44 +00:00
|
|
|
},
|
2024-06-18 01:49:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Database Connections
|
|
|
|
*/
|
2024-06-27 01:10:48 +00:00
|
|
|
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;
|
|
|
|
},
|
2024-06-18 01:49:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility classes
|
|
|
|
*/
|
|
|
|
UserAuthorizationInterface::class => function(ContainerInterface $container) {
|
|
|
|
return new BasicUserAuthorization();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the Domain Repositories Here
|
|
|
|
*/
|
2024-06-17 01:55:44 +00:00
|
|
|
UserRepositoryInterface::class => function (ContainerInterface $container) {
|
|
|
|
return new UserRepository(
|
|
|
|
$container->get(DatabaseConnectionInterface::class),
|
|
|
|
$container->get(UserAuthorizationInterface::class)
|
|
|
|
);
|
2024-05-26 02:23:53 +00:00
|
|
|
}
|
2024-05-14 02:06:01 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
return $containerBuilder->build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-27 01:10:48 +00:00
|
|
|
* Tasking the instantiated Application, sets up all the routes for the
|
2024-05-14 02:06:01 +00:00
|
|
|
* 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
|
2024-06-16 01:50:40 +00:00
|
|
|
* 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.
|
2024-05-14 02:06:01 +00:00
|
|
|
*
|
|
|
|
* @param App $app
|
2024-06-27 01:10:48 +00:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
* @throws LoaderError
|
2024-05-14 02:06:01 +00:00
|
|
|
*/
|
|
|
|
protected static function establishMiddleware(App $app): void
|
|
|
|
{
|
2024-06-05 01:16:59 +00:00
|
|
|
/** @var ContainerInterface $container */
|
|
|
|
$container = $app->getContainer();
|
|
|
|
|
2024-05-24 02:39:38 +00:00
|
|
|
/**
|
2024-06-27 01:10:48 +00:00
|
|
|
* @var Configuration $config
|
2024-05-24 02:39:38 +00:00
|
|
|
*/
|
2024-06-05 01:16:59 +00:00
|
|
|
$config = $container->get('config');
|
2024-05-16 02:03:49 +00:00
|
|
|
|
|
|
|
// Twig
|
2024-05-23 00:39:33 +00:00
|
|
|
$templateCache = false;
|
2024-06-18 01:57:21 +00:00
|
|
|
$twig = Twig::create(APP_TEMPLATES_DIR, [ '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-06-05 01:16:59 +00:00
|
|
|
$flash = $container->get(SessionInterface::class)->getFlash();
|
|
|
|
$twig->getEnvironment()
|
|
|
|
->addGlobal('flash', $flash);
|
2024-05-23 00:41:44 +00:00
|
|
|
$app->add(TwigMiddleware::create($app, $twig));
|
2024-06-17 00:09:09 +00:00
|
|
|
|
|
|
|
// Add the error handling middleware
|
|
|
|
$app->addErrorMiddleware(true, true, true);
|
2024-05-14 02:06:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates the application.
|
|
|
|
*
|
|
|
|
* @return App
|
2024-06-27 01:10:48 +00:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws LoaderError
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
* @throws \Exception
|
2024-05-14 02:06:01 +00:00
|
|
|
*/
|
|
|
|
public static function init(): App
|
|
|
|
{
|
|
|
|
$container = self::initContainer();
|
|
|
|
$app = AppFactory::createFromContainer($container);
|
|
|
|
self::establishMiddleware($app);
|
|
|
|
self::establishRoutes($app);
|
|
|
|
return $app;
|
|
|
|
}
|
|
|
|
}
|