162 lines
5.6 KiB
PHP
162 lines
5.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace MailAccountAdmin;
|
|
|
|
use DI\Container;
|
|
use MailAccountAdmin\Common\PasswordHelper;
|
|
use MailAccountAdmin\Common\SessionHelper;
|
|
use MailAccountAdmin\Common\UserHelper;
|
|
use MailAccountAdmin\Frontend\Accounts\AccountController;
|
|
use MailAccountAdmin\Frontend\Accounts\AccountHandler;
|
|
use MailAccountAdmin\Frontend\Domains\DomainController;
|
|
use MailAccountAdmin\Frontend\Login\LoginController;
|
|
use MailAccountAdmin\Frontend\Dashboard\DashboardController;
|
|
use MailAccountAdmin\Repositories\AccountRepository;
|
|
use MailAccountAdmin\Repositories\AdminUserRepository;
|
|
use MailAccountAdmin\Repositories\AliasRepository;
|
|
use MailAccountAdmin\Repositories\DomainRepository;
|
|
use PDO;
|
|
use Psr\Container\ContainerInterface;
|
|
use Slim\Views\Twig;
|
|
use Twig\Extension\CoreExtension as TwigCoreExtension;
|
|
|
|
class Dependencies
|
|
{
|
|
public const SETTINGS = 'settings';
|
|
public const TWIG = 'view';
|
|
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
|
|
public const DATABASE = 'database';
|
|
|
|
public static function createContainer(Settings $settings): Container
|
|
{
|
|
$container = new Container();
|
|
|
|
// App settings
|
|
$container->set(self::SETTINGS, $settings);
|
|
|
|
// Twig template engine
|
|
$container->set(self::TWIG, function (ContainerInterface $c) {
|
|
/** @var Settings $settings */
|
|
$settings = $c->get(self::SETTINGS);
|
|
|
|
// Create Twig view
|
|
$twig = Twig::create(self::TWIG_TEMPLATE_DIR, $settings->getTwigSettings());
|
|
|
|
// Set default date format
|
|
/** @var TwigCoreExtension $coreExtension */
|
|
$coreExtension = $twig->getEnvironment()->getExtension(TwigCoreExtension::class);
|
|
$coreExtension->setDateFormat($settings->getDateFormat());
|
|
$coreExtension->setTimezone($settings->getTimezone());
|
|
|
|
// Return Twig view
|
|
return $twig;
|
|
});
|
|
|
|
// Database connection
|
|
$container->set(self::DATABASE, function (ContainerInterface $c) {
|
|
/** @var Settings $settings */
|
|
$settings = $c->get(self::SETTINGS);
|
|
$dbSettings = $settings->getDatabaseSettings();
|
|
|
|
return new PDO(
|
|
"mysql:dbname={$dbSettings['dbname']};host={$dbSettings['host']};port={$dbSettings['port']}",
|
|
$dbSettings['username'],
|
|
$dbSettings['password'],
|
|
[
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
],
|
|
);
|
|
});
|
|
|
|
// Repositories
|
|
$container->set(AdminUserRepository::class, function (ContainerInterface $c) {
|
|
return new AdminUserRepository(
|
|
$c->get(self::DATABASE),
|
|
);
|
|
});
|
|
$container->set(DomainRepository::class, function (ContainerInterface $c) {
|
|
return new DomainRepository(
|
|
$c->get(self::DATABASE),
|
|
);
|
|
});
|
|
$container->set(AccountRepository::class, function (ContainerInterface $c) {
|
|
return new AccountRepository(
|
|
$c->get(self::DATABASE),
|
|
);
|
|
});
|
|
$container->set(AliasRepository::class, function (ContainerInterface $c) {
|
|
return new AliasRepository(
|
|
$c->get(self::DATABASE),
|
|
);
|
|
});
|
|
|
|
// Helper classes
|
|
$container->set(SessionHelper::class, function (ContainerInterface $c) {
|
|
return new SessionHelper();
|
|
});
|
|
|
|
$container->set(UserHelper::class, function (ContainerInterface $c) {
|
|
return new UserHelper(
|
|
$c->get(SessionHelper::class),
|
|
$c->get(AdminUserRepository::class),
|
|
);
|
|
});
|
|
|
|
$container->set(PasswordHelper::class, function (ContainerInterface $c) {
|
|
return new PasswordHelper();
|
|
});
|
|
|
|
// Frontend controllers and handlers
|
|
// -> Login page
|
|
$container->set(LoginController::class, function (ContainerInterface $c) {
|
|
return new LoginController(
|
|
$c->get(self::TWIG),
|
|
$c->get(SessionHelper::class),
|
|
$c->get(UserHelper::class),
|
|
$c->get(AdminUserRepository::class),
|
|
$c->get(PasswordHelper::class),
|
|
);
|
|
});
|
|
|
|
// -> Dashboard
|
|
$container->set(DashboardController::class, function (ContainerInterface $c) {
|
|
return new DashboardController(
|
|
$c->get(self::TWIG),
|
|
$c->get(SessionHelper::class),
|
|
$c->get(UserHelper::class),
|
|
);
|
|
});
|
|
|
|
// -> Domains
|
|
$container->set(DomainController::class, function (ContainerInterface $c) {
|
|
return new DomainController(
|
|
$c->get(self::TWIG),
|
|
$c->get(SessionHelper::class),
|
|
$c->get(UserHelper::class),
|
|
$c->get(DomainRepository::class),
|
|
);
|
|
});
|
|
|
|
// -> Accounts
|
|
$container->set(AccountController::class, function (ContainerInterface $c) {
|
|
return new AccountController(
|
|
$c->get(self::TWIG),
|
|
$c->get(SessionHelper::class),
|
|
$c->get(UserHelper::class),
|
|
$c->get(AccountHandler::class),
|
|
);
|
|
});
|
|
$container->set(AccountHandler::class, function (ContainerInterface $c) {
|
|
return new AccountHandler(
|
|
$c->get(AccountRepository::class),
|
|
$c->get(AliasRepository::class),
|
|
$c->get(DomainRepository::class),
|
|
$c->get(PasswordHelper::class),
|
|
);
|
|
});
|
|
|
|
return $container;
|
|
}
|
|
}
|