Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postpone LoggerPass exception with multiple found LoggerInterfaces to autowire #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DI/MessengerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Contributte\Messenger\DI\Pass\SerializerPass;
use Contributte\Messenger\DI\Pass\TransportFactoryPass;
use Contributte\Messenger\DI\Pass\TransportPass;
use Nette\DI\Definitions\Statement;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\ClassType;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
Expand Down
13 changes: 12 additions & 1 deletion src/DI/Pass/LoggerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Contributte\Messenger\Logger\MessengerLogger;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\DI\ServiceCreationException;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Console\Logger\ConsoleLogger;
Expand Down Expand Up @@ -34,13 +35,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 (ServiceCreationException $e) {
// 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 +65,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
4 changes: 2 additions & 2 deletions src/DI/Utils/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public static function getMessageHandlerMessage(string $class, array $options):
throw new LogicalException(sprintf('Handler must have "%s::%s()" method.', $class, $options['method']));
}

if ($rcMethod->getNumberOfParameters() !== 1 && !$rc->implementsInterface( BatchHandlerInterface::class)) {
if ($rcMethod->getNumberOfParameters() !== 1 && !$rc->implementsInterface(BatchHandlerInterface::class)) {
throw new LogicalException(sprintf('Only one parameter is allowed in "%s::%s()."', $class, $options['method']));
}

if ($rc->implementsInterface( BatchHandlerInterface::class)) {
if ($rc->implementsInterface(BatchHandlerInterface::class)) {
if ($rcMethod->getNumberOfParameters() !== 2) {
throw new LogicalException(sprintf('Exactly two parameters are required in "%s::%s()."', $class, $options['method']));
}
Expand Down
Loading