From c47e4f027723729a2437aaf5c93d2a3cdb1317d7 Mon Sep 17 00:00:00 2001 From: Marius Klocke Date: Mon, 11 Dec 2023 19:09:35 +0100 Subject: [PATCH] Replace usages of Config::getInstance() --- src/Infrastructure/API/LoggerProvider.php | 6 ++++-- .../API/Security/JsonWebTokenService.php | 5 ++--- .../API/Security/ServiceProvider.php | 8 ++++++++ src/Infrastructure/CLI/CreateUserCommand.php | 9 +++++++-- src/Infrastructure/CLI/ServiceProvider.php | 8 ++++---- src/Infrastructure/Config.php | 2 +- src/Infrastructure/Email/MailServiceProvider.php | 10 +++++++--- .../Persistence/EventServiceProvider.php | 8 +++++--- .../Persistence/ORM/DoctrineServiceProvider.php | 14 ++++++++++---- 9 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/Infrastructure/API/LoggerProvider.php b/src/Infrastructure/API/LoggerProvider.php index f7bb26d4..e2a9582b 100644 --- a/src/Infrastructure/API/LoggerProvider.php +++ b/src/Infrastructure/API/LoggerProvider.php @@ -6,6 +6,7 @@ use DI; use HexagonalPlayground\Application\ServiceProviderInterface; use HexagonalPlayground\Infrastructure\Config; +use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; class LoggerProvider implements ServiceProviderInterface @@ -13,8 +14,9 @@ class LoggerProvider implements ServiceProviderInterface public function getDefinitions(): array { return [ - LoggerInterface::class => DI\factory(function () { - $config = Config::getInstance(); + LoggerInterface::class => DI\factory(function (ContainerInterface $container) { + /** @var Config $config */ + $config = $container->get(Config::class); $stream = fopen($config->logPath, 'w'); $logLevel = $config->logLevel; diff --git a/src/Infrastructure/API/Security/JsonWebTokenService.php b/src/Infrastructure/API/Security/JsonWebTokenService.php index 55972440..8db6d335 100644 --- a/src/Infrastructure/API/Security/JsonWebTokenService.php +++ b/src/Infrastructure/API/Security/JsonWebTokenService.php @@ -10,7 +10,6 @@ use HexagonalPlayground\Application\Security\TokenInterface; use HexagonalPlayground\Application\Security\TokenServiceInterface; use HexagonalPlayground\Domain\User; -use HexagonalPlayground\Infrastructure\Config; class JsonWebTokenService implements TokenServiceInterface { @@ -19,9 +18,9 @@ class JsonWebTokenService implements TokenServiceInterface private Key $privateKey; - public function __construct() + public function __construct(string $jwtSecret) { - $this->privateKey = new Key(hex2bin(Config::getInstance()->jwtSecret), self::ALGORITHM); + $this->privateKey = new Key(hex2bin($jwtSecret), self::ALGORITHM); } /** diff --git a/src/Infrastructure/API/Security/ServiceProvider.php b/src/Infrastructure/API/Security/ServiceProvider.php index 58ed7d13..2f04f4f9 100644 --- a/src/Infrastructure/API/Security/ServiceProvider.php +++ b/src/Infrastructure/API/Security/ServiceProvider.php @@ -6,6 +6,8 @@ use DI; use HexagonalPlayground\Application\Security\TokenServiceInterface; use HexagonalPlayground\Application\ServiceProviderInterface; +use HexagonalPlayground\Infrastructure\Config; +use Psr\Container\ContainerInterface; class ServiceProvider implements ServiceProviderInterface { @@ -13,6 +15,12 @@ public function getDefinitions(): array { return [ TokenServiceInterface::class => DI\get(JsonWebTokenService::class), + JsonWebTokenService::class => DI\factory(function (ContainerInterface $container) { + /** @var Config $config */ + $config = $container->get(Config::class); + + return new JsonWebTokenService($config->jwtSecret); + }), PasswordAuthenticator::class => DI\autowire(), TokenAuthenticator::class => DI\autowire() ]; diff --git a/src/Infrastructure/CLI/CreateUserCommand.php b/src/Infrastructure/CLI/CreateUserCommand.php index 7f39c21c..027ceb47 100644 --- a/src/Infrastructure/CLI/CreateUserCommand.php +++ b/src/Infrastructure/CLI/CreateUserCommand.php @@ -49,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int private function getEmail(InputInterface $input, OutputInterface $output): ?string { if ($input->getOption('default')) { - return Config::getInstance()->adminEmail; + return $this->getConfig()->adminEmail; } if ($input->getOption('email')) { @@ -62,7 +62,7 @@ private function getEmail(InputInterface $input, OutputInterface $output): ?stri private function getPassword(InputInterface $input, OutputInterface $output): ?string { if ($input->getOption('default')) { - return Config::getInstance()->adminPassword; + return $this->getConfig()->adminPassword; } if ($input->getOption('password')) { @@ -114,4 +114,9 @@ private function getRole(InputInterface $input, OutputInterface $output): ?strin return null; } + + private function getConfig(): Config + { + return $this->container->get(Config::class); + } } diff --git a/src/Infrastructure/CLI/ServiceProvider.php b/src/Infrastructure/CLI/ServiceProvider.php index 5ce928ea..3595b35a 100644 --- a/src/Infrastructure/CLI/ServiceProvider.php +++ b/src/Infrastructure/CLI/ServiceProvider.php @@ -27,12 +27,12 @@ class ServiceProvider implements ServiceProviderInterface public function getDefinitions(): array { return [ - Configuration::class => DI\factory(function () { - $namespace = 'Migrations'; - $path = Config::getInstance()->appHome . '/migrations'; + Configuration::class => DI\factory(function (ContainerInterface $container) { + /** @var Config $config */ + $config = $container->get(Config::class); $migrationsConfig = new Configuration(); - $migrationsConfig->addMigrationsDirectory($namespace, $path); + $migrationsConfig->addMigrationsDirectory('Migrations', $config->appHome . '/migrations'); return $migrationsConfig; }), diff --git a/src/Infrastructure/Config.php b/src/Infrastructure/Config.php index 190a49c5..c32c660c 100644 --- a/src/Infrastructure/Config.php +++ b/src/Infrastructure/Config.php @@ -22,7 +22,7 @@ class Config public string $mysqlUser; public string $redisHost; - private function __construct() + public function __construct() { $this->adminEmail = getenv('ADMIN_EMAIL') ?: ''; $this->adminPassword = getenv('ADMIN_PASSWORD') ?: ''; diff --git a/src/Infrastructure/Email/MailServiceProvider.php b/src/Infrastructure/Email/MailServiceProvider.php index 4e48e6c4..d507358f 100644 --- a/src/Infrastructure/Email/MailServiceProvider.php +++ b/src/Infrastructure/Email/MailServiceProvider.php @@ -21,7 +21,8 @@ public function getDefinitions(): array return [ MailerInterface::class => DI\get(SymfonyMailer::class), SymfonyMailer::class => DI\factory(function (ContainerInterface $container) { - $config = Config::getInstance(); + /** @var Config $config */ + $config = $container->get(Config::class); $transport = Transport::fromDsn( $config->emailUrl, @@ -35,8 +36,11 @@ public function getDefinitions(): array ); }), TemplateRendererInterface::class => DI\get(TemplateRenderer::class), - TemplateRenderer::class => DI\factory(function () { - return new TemplateRenderer(Config::getInstance()->appHome . '/templates'); + TemplateRenderer::class => DI\factory(function (ContainerInterface $container) { + /** @var Config $config */ + $config = $container->get(Config::class); + + return new TemplateRenderer($config->appHome . '/templates'); }) ]; } diff --git a/src/Infrastructure/Persistence/EventServiceProvider.php b/src/Infrastructure/Persistence/EventServiceProvider.php index 544105c2..47551b4f 100644 --- a/src/Infrastructure/Persistence/EventServiceProvider.php +++ b/src/Infrastructure/Persistence/EventServiceProvider.php @@ -37,11 +37,13 @@ public function getDefinitions(): array HealthCheckInterface::class => DI\add(DI\get(RedisHealthCheck::class)), Redis::class => DI\factory(function (ContainerInterface $container) { - $retry = new Retry($container->get(LoggerInterface::class), 60, 5); + /** @var Config $config */ + $config = $container->get(Config::class); + $retry = new Retry($container->get(LoggerInterface::class), 60, 5); - return $retry(function () { + return $retry(function () use ($config) { $redis = new Redis(); - @$redis->connect(Config::getInstance()->redisHost); + @$redis->connect($config->redisHost); return $redis; }); diff --git a/src/Infrastructure/Persistence/ORM/DoctrineServiceProvider.php b/src/Infrastructure/Persistence/ORM/DoctrineServiceProvider.php index 3936b300..17198e50 100644 --- a/src/Infrastructure/Persistence/ORM/DoctrineServiceProvider.php +++ b/src/Infrastructure/Persistence/ORM/DoctrineServiceProvider.php @@ -66,7 +66,8 @@ public function getDefinitions(): array }), Connection::class => DI\factory(function (ContainerInterface $container) { - $config = Config::getInstance(); + /** @var Config $config */ + $config = $container->get(Config::class); $params = [ 'dbname' => $config->mysqlDatabase, @@ -100,13 +101,16 @@ public function getDefinitions(): array }), Configuration::class => DI\factory(function (ContainerInterface $container) { + /** @var Config $appConfig */ + $appConfig = $container->get(Config::class); + $config = new Configuration(); $config->setProxyDir(sys_get_temp_dir()); $config->setProxyNamespace('DoctrineProxies'); $config->setMetadataDriverImpl($container->get(SimplifiedXmlDriver::class)); $config->setAutoGenerateProxyClasses(AbstractProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); - if (Config::getInstance()->logLevel === 'debug') { + if ($appConfig->logLevel === 'debug') { $config->setMiddlewares([new LoggingMiddleware($container->get(LoggerInterface::class))]); } @@ -115,8 +119,10 @@ public function getDefinitions(): array ObjectManager::class => DI\get(EntityManagerInterface::class), - SimplifiedXmlDriver::class => DI\factory(function () { - $basePath = Config::getInstance()->appHome; + SimplifiedXmlDriver::class => DI\factory(function (ContainerInterface $container) { + /** @var Config $config */ + $config = $container->get(Config::class); + $basePath = $config->appHome; $driver = new SimplifiedXmlDriver([ $basePath . "/config/doctrine/Infrastructure/API/Security/WebAuthn" => "HexagonalPlayground\\Infrastructure\\API\\Security\\WebAuthn",