Skip to content

Commit

Permalink
Refactor the way drivers et injected in the messageConsumer
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Sep 3, 2015
1 parent 0ce8c89 commit 2335557
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Adapter/DriverManager.php
Expand Up @@ -76,4 +76,17 @@ public function listDrivers()
{
return array_keys($this->aliases);
}

/**
* returns all drivers
* @return array key is alias, value is the service
*/
public function getDrivers()
{
$drivers = array();
foreach($this->aliases as $alias => $service) {
$drivers[$alias] = $this->container->get($service);
}
return $drivers;
}
}
4 changes: 2 additions & 2 deletions Resources/config/services.yml
Expand Up @@ -103,13 +103,13 @@ services:
### Consumers ###

# The base service - mapped to an abstract class
# NB: this is where we map the consumer to a list of drivers
# All the services which are tagged 'kaliop_queueing.driver' get registered automatically.
kaliop_queueing.message_consumer:
class: %kaliop_queueing.message_consumer.class%
calls:
- [ setLogger, [ @?logger ] ]
- [ setDispatcher, [ @kaliop_queueing.event_dispatcher ] ]
- [ registerDriver, [ @kaliop_queueing.driver.rabbitmq ] ]
- [ setDriverManager, [ @kaliop_queueing.drivermanager ] ]
tags:
- { name: monolog.logger, channel: kaliop_queueing }

Expand Down
20 changes: 16 additions & 4 deletions Service/MessageConsumer.php
Expand Up @@ -31,7 +31,8 @@ abstract class MessageConsumer implements ConsumerInterface, MessageConsumerInte
protected $logger;
protected $dispatcher;
/** @var DriverInterface[] */
protected $drivers = array();
protected $drivers = null;
protected $driverManager = null;

/**
* The method to be implemented by subclasses, executed upon reception of a message.
Expand All @@ -54,11 +55,21 @@ public function setDispatcher(EventDispatcherInterface $dispatcher = null)
}

/**
* @param DriverInterface $driver
* @param string $driverManager
*/
public function registerDriver(DriverInterface $driver)
public function setDriverManager($driverManager)
{
$this->drivers[] = $driver;
$this->driverManager = $driverManager;
}

/**
* Lazy-loads all the driver services which have been registered
*/
protected function loadRegisteredDrivers()
{
if ($this->drivers === null) {
$this->drivers = $this->driverManager->getDrivers();
}
}

/**
Expand Down Expand Up @@ -120,6 +131,7 @@ public function receive($msg)
*/
protected function getDriver($message)
{
$this->loadRegisteredDrivers();
foreach ($this->drivers as $driver) {
if ($driver->acceptMessage($message))
return $driver;
Expand Down

0 comments on commit 2335557

Please sign in to comment.