Skip to content

Commit

Permalink
Postpone exception with multiple found LoggerInterfaces to autowire
Browse files Browse the repository at this point in the history
  • Loading branch information
finwe committed Feb 20, 2024
1 parent dd1d4eb commit 5f2c528
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/DI/Pass/LoggerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ public function beforePassCompile(): void
$builder = $this->getContainerBuilder();
$config = $this->getConfig();

$logger = $builder->getByType(LoggerInterface::class);
$multiple = null;
try {
$logger = $builder->getByType(LoggerInterface::class, false);
} catch (\Nette\DI\ServiceCreationException $e) {

Check failure on line 40 in src/DI/Pass/LoggerPass.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Class \Nette\DI\ServiceCreationException should not be referenced via a fully qualified name, but via a use statement.
// multiple loggers
$multiple = $e;
}

// Register or resolve http logger
if ($config->logger->httpLogger !== null) {
$httpLogger = $builder->addDefinition($this->prefix('logger.httpLogger'))
->setFactory($config->logger->httpLogger)
->setAutowired(false);
} elseif ($multiple !== null) {
throw $e;
} elseif ($logger !== null) {
$httpLogger = $builder->addDefinition($this->prefix('logger.httpLogger'))
->setFactory('@' . $logger)
Expand All @@ -56,6 +64,8 @@ public function beforePassCompile(): void
$consoleLogger = $builder->addDefinition($this->prefix('logger.consoleLogger'))
->setFactory($config->logger->consoleLogger)
->setAutowired(false);
} elseif ($multiple !== null) {
throw $e;
} elseif ($logger !== null) {
$consoleLogger = $builder->addDefinition($this->prefix('logger.consoleLogger'))
->setFactory('@' . $logger)
Expand Down

0 comments on commit 5f2c528

Please sign in to comment.