2021-07-23 03:18:33 +02:00
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace MailAccountAdmin;
|
|
|
|
|
|
|
|
|
|
use DI\Container;
|
2021-07-27 01:25:03 +02:00
|
|
|
use MailAccountAdmin\Common\UserHelper;
|
2021-07-26 23:52:59 +02:00
|
|
|
use MailAccountAdmin\Frontend\Login\LoginController;
|
|
|
|
|
use MailAccountAdmin\Frontend\Dashboard\DashboardController;
|
2021-07-27 01:25:03 +02:00
|
|
|
use MailAccountAdmin\Repositories\AdminUserRepository;
|
2021-07-26 23:14:14 +02:00
|
|
|
use PDO;
|
2021-07-23 03:18:33 +02:00
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
use Slim\Views\Twig;
|
|
|
|
|
|
|
|
|
|
class Dependencies
|
|
|
|
|
{
|
2021-07-26 23:14:14 +02:00
|
|
|
public const SETTINGS = 'settings';
|
|
|
|
|
public const TWIG = 'view';
|
2021-07-23 03:18:33 +02:00
|
|
|
private const TWIG_TEMPLATE_DIR = __DIR__ . '/../templates';
|
2021-07-26 23:14:14 +02:00
|
|
|
public const DATABASE = 'database';
|
2021-07-23 03:18:33 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
return Twig::create(self::TWIG_TEMPLATE_DIR, $settings->getTwigSettings());
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-26 23:14:14 +02:00
|
|
|
// 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,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-27 01:25:03 +02:00
|
|
|
// Repositories
|
|
|
|
|
$container->set(AdminUserRepository::class, function (ContainerInterface $c) {
|
|
|
|
|
return new AdminUserRepository(
|
|
|
|
|
$c->get(self::DATABASE),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Helper classes
|
|
|
|
|
$container->set(UserHelper::class, function (ContainerInterface $c) {
|
|
|
|
|
return new UserHelper(
|
|
|
|
|
$c->get(AdminUserRepository::class),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-26 23:52:59 +02:00
|
|
|
// Login page
|
2021-07-23 03:18:33 +02:00
|
|
|
$container->set(LoginController::class, function (ContainerInterface $c) {
|
|
|
|
|
return new LoginController(
|
2021-07-27 01:25:03 +02:00
|
|
|
$c->get(self::TWIG),
|
|
|
|
|
$c->get(UserHelper::class),
|
|
|
|
|
$c->get(AdminUserRepository::class),
|
2021-07-23 03:18:33 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-26 23:52:59 +02:00
|
|
|
// Dashboard
|
|
|
|
|
$container->set(DashboardController::class, function (ContainerInterface $c) {
|
|
|
|
|
return new DashboardController(
|
2021-07-27 01:25:03 +02:00
|
|
|
$c->get(self::TWIG),
|
|
|
|
|
$c->get(UserHelper::class),
|
2021-07-26 23:52:59 +02:00
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2021-07-23 03:18:33 +02:00
|
|
|
return $container;
|
|
|
|
|
}
|
|
|
|
|
}
|