Skip to content

Commit

Permalink
Add support for logger injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Thibaud Fabre committed Aug 1, 2016
1 parent 8e27034 commit def6b72
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"phpunit/phpunit": "^5.4"
},
"require": {
"alchemy/queue-component": "^0.1",
"alchemy/queue-component": "^0.1.1",
"php": ">=5.5"
},
"extra": {
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/QueueConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public function getConfigTreeBuilder()
{
$builder = new TreeBuilder();

$builder->root('alchemy_queues')
$builder->root('alchemy_queue')
->children()
->scalarNode('logger')->defaultNull()->end()
->arrayNode('queues')
->useAttributeAsKey('name', true)
->prototype('array')
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/QueueExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Alchemy\QueueBundle\Queue\QueueRegistry;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;

class QueueExtension extends ConfigurableExtension
Expand All @@ -39,6 +40,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
$registry = new Definition(QueueRegistry::class);
$queueConfigurations = $mergedConfig['queues'];

if ($mergedConfig['logger'] != '') {
$registry->addMethodCall('setLogger', [ new Reference($mergedConfig['logger']) ]);
}

foreach ($queueConfigurations as $name => $configuration) {
$registry->addMethodCall('bindConfiguration', [ $name, $configuration ]);
}
Expand Down
33 changes: 30 additions & 3 deletions src/Queue/QueueRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

use Alchemy\Queue\Amqp\AmqpMessageQueueFactory;
use Alchemy\Queue\MessageQueue;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class QueueRegistry
class QueueRegistry implements LoggerAwareInterface
{
/**
* @var array
Expand All @@ -26,6 +29,16 @@ class QueueRegistry
*/
private $queues = [];

/**
* @var LoggerInterface
*/
private $logger;

public function __construct()
{
$this->logger = new NullLogger();
}

/**
* @param string $queueName
* @param array $configuration
Expand All @@ -46,11 +59,25 @@ public function getQueue($queueName)
}

if (isset($this->configurations[$queueName])) {
return $this->queues[$queueName] =
AmqpMessageQueueFactory::create($this->configurations[$queueName])->getNamedQueue($queueName);
$queue = AmqpMessageQueueFactory::create(
$this->configurations[$queueName],
$this->logger
)->getNamedQueue($queueName);

return $this->queues[$queueName] = $queue;
}

throw new \RuntimeException('Queue is not registered: ' . $queueName);
}

/**
* Sets a logger instance on the object
*
* @param LoggerInterface $logger
* @return null
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
}

0 comments on commit def6b72

Please sign in to comment.