don't over engineer the bootstrapping.

This commit is contained in:
Dave Smith-Hayes 2025-06-20 13:14:51 +00:00
parent f8911d9d14
commit 7c4012c9fd
4 changed files with 35 additions and 85 deletions

View File

@ -33,6 +33,7 @@ use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Psr7\Factory\ResponseFactory;
use Slovocast\Infrastructure\Application\LoggerDefinition;
use Slovocast\Infrastructure\Application\ResponseFactoryDefinition;
use Slovocast\Infrastructure\Application\SessionDefintion;
use Slovocast\Middleware\SessionMiddleware;
use Twig\Error\LoaderError;
@ -120,20 +121,51 @@ class Bootstrap
protected static function initContainer(): Container
{
$containerBuilder = new ContainerBuilder();
$config = self::initConfig();
$containerDefinitions = array_merge(
[ 'config' => $config ],
(new LoggerDefinition())->define(),
(new SessionDefintion())->define(),
(new ResponseFactoryDefinition())->define(),
);
$containerBuilder->addDefinitions([
'config' => $config,
/**
* Global logging
*/
(new LoggerDefinition())->define(),
LoggerInterface::class => function (ContainerInterface $c) {
$logger = new Logger("default");
$logger->pushHandler(new StreamHandler('php://stdout', Level::Info));
return $logger;
},
/**
* Session and Flash classes here
*/
(new SessionDefintion())->define(),
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);
},
'session' => function (ContainerInterface $container) {
return $container->get(SessionInterface::class);
},
SessionMiddleware::class => function (ContainerInterface $container) {
return new SessionMiddleware(
$container->get(SessionManagerInterface::class),
$container->get(SessionInterface::class),
$container->get(LoggerInterface::class)
);
},
UserSessionManagerInterface::class => function (ContainerInterface $container) {
return new UserSessionManager($container->get(SessionInterface::class));
},
/**
* Application DI

View File

@ -1,16 +0,0 @@
<?php
namespace Slovocast\Infrastructure\Application;
/**
* Ideally this would define the Container definition required for setting up
* dependencies within the PHP DI ContainerBuilder class
*/
interface DefinitionInterface
{
/**
* @return array<string, mixed> The definition structure used for setting
* up a Container
*/
public function define(): array;
}

View File

@ -1,24 +0,0 @@
<?php
namespace Slovocast\Infrastructure\Application;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Monolog\Handler\StreamHandler;
use Monolog\Level;
use Monolog\Logger;
use Slovocast\Infrastructure\Application\DefinitionInterface;
class LoggerDefinition implements DefinitionInterface
{
public function define(): array
{
return [
LoggerInterface::class => function (ContainerInterface $c) {
$logger = new Logger("default");
$logger->pushHandler(new StreamHandler('php://stdout', Level::Info));
return $logger;
}
];
}
}

View File

@ -1,42 +0,0 @@
<?php
namespace Slovocast\Infrastructure\Application;
use Slovocast\Infrastructure\Application\DefinitionInterface;
use Slovocast\Infrastructure\Api\User\UserSessionManagerInterface;
use Slovocast\Infrastructure\User\UserSessionManager;
use Slovocast\Middleware\SessionMiddleware;
use Odan\Session\PhpSession;
use Odan\Session\SessionInterface;
use Odan\Session\SessionManagerInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
class SessionDefintion implements DefinitionInterface
{
public function define(): array
{
return [
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);
},
'session' => function (ContainerInterface $container) {
return $container->get(SessionInterface::class);
},
SessionMiddleware::class => function (ContainerInterface $container) {
return new SessionMiddleware(
$container->get(SessionManagerInterface::class),
$container->get(SessionInterface::class),
$container->get(LoggerInterface::class)
);
},
UserSessionManagerInterface::class => function (ContainerInterface $container) {
return new UserSessionManager($container->get(SessionInterface::class));
},
];
}
}