Skip to content

Commit

Permalink
Replace usages of Config::getInstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Dec 11, 2023
1 parent c364256 commit c47e4f0
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 22 deletions.
6 changes: 4 additions & 2 deletions src/Infrastructure/API/LoggerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
use DI;
use HexagonalPlayground\Application\ServiceProviderInterface;
use HexagonalPlayground\Infrastructure\Config;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

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;

Expand Down
5 changes: 2 additions & 3 deletions src/Infrastructure/API/Security/JsonWebTokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Infrastructure/API/Security/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
use DI;
use HexagonalPlayground\Application\Security\TokenServiceInterface;
use HexagonalPlayground\Application\ServiceProviderInterface;
use HexagonalPlayground\Infrastructure\Config;
use Psr\Container\ContainerInterface;

class ServiceProvider implements ServiceProviderInterface
{
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()
];
Expand Down
9 changes: 7 additions & 2 deletions src/Infrastructure/CLI/CreateUserCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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')) {
Expand Down Expand Up @@ -114,4 +114,9 @@ private function getRole(InputInterface $input, OutputInterface $output): ?strin

return null;
}

private function getConfig(): Config
{
return $this->container->get(Config::class);
}
}
8 changes: 4 additions & 4 deletions src/Infrastructure/CLI/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}),
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') ?: '';
Expand Down
10 changes: 7 additions & 3 deletions src/Infrastructure/Email/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
})
];
}
Expand Down
8 changes: 5 additions & 3 deletions src/Infrastructure/Persistence/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
14 changes: 10 additions & 4 deletions src/Infrastructure/Persistence/ORM/DoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))]);
}

Expand All @@ -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",
Expand Down

0 comments on commit c47e4f0

Please sign in to comment.