Skip to content

Commit

Permalink
Close #11: Add AbstractLoaderFactory
Browse files Browse the repository at this point in the history
All Loaders are now fabricated by an AbstractFactory.
The AbstractFactory considers if ServiceBus configuration contains
configuration for the requested loader and passes config to it.
  • Loading branch information
codeliner committed Jul 5, 2014
1 parent 8118088 commit 11ca278
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 46 deletions.
2 changes: 2 additions & 0 deletions src/Prooph/ServiceBus/Service/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Definition
{
const CONFIG_ROOT = "prooph.service_bus";

const CONFIG_ROOT_ESCAPED = "prooph\.service_bus";

const COMMAND_BUS = "command_bus";

const DEFAULT_COMMAND_BUS = "default_command_bus";
Expand Down
94 changes: 94 additions & 0 deletions src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) Alexander Miertsch <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 05.07.14 - 21:34
*/

namespace Prooph\ServiceBus\Service\Factory;

use Codeliner\ArrayReader\ArrayReader;
use Prooph\ServiceBus\Service\Definition;
use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Class AbstractLoaderFactory
*
* @package Prooph\ServiceBus\Service\Factory
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class AbstractLoaderFactory implements AbstractFactoryInterface
{
/**
* @var array
*/
protected $servicesMap;

/**
* @var ArrayReader
*/
protected $configReader;

/**
* Determine if we can create a service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
return in_array($requestedName, array_keys($this->getServicesMap()));
}

/**
* Create service with name
*
* @param ServiceLocatorInterface $serviceLocator
* @param $name
* @param $requestedName
* @return mixed
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
if (is_null($this->configReader)) {
$this->configReader = new ArrayReader($serviceLocator->get('configuration'));
}

$config = new Config($this->configReader->arrayValue(
Definition::CONFIG_ROOT_ESCAPED . '.' . $requestedName
));

$loaderClass = $this->getServicesMap()[$requestedName];

return new $loaderClass($config);
}

/**
* @return array
*/
protected function getServicesMap()
{
if (is_null($this->servicesMap)) {
$this->servicesMap = array(
Definition::COMMAND_BUS_LOADER => 'Prooph\ServiceBus\Service\CommandBusLoader',
Definition::COMMAND_RECEIVER_LOADER => 'Prooph\ServiceBus\Service\CommandReceiverLoader',
Definition::EVENT_BUS_LOADER => 'Prooph\ServiceBus\Service\EventBusLoader',
Definition::EVENT_RECEIVER_LOADER => 'Prooph\ServiceBus\Service\EventReceiverLoader',
Definition::INVOKE_STRATEGY_LOADER => 'Prooph\ServiceBus\Service\InvokeStrategyLoader',
Definition::MESSAGE_DISPATCHER_LOADER => 'Prooph\ServiceBus\Service\MessageDispatcherLoader',
Definition::QUEUE_LOADER => 'Prooph\ServiceBus\Service\QueueLoader'
);
}

return $this->servicesMap;
}
}

35 changes: 2 additions & 33 deletions src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Prooph\ServiceBus\Service;

use Codeliner\ArrayReader\ArrayReader;
use Prooph\ServiceBus\Command\CommandFactoryInterface;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ConfigInterface;
Expand All @@ -25,6 +26,7 @@ class ServiceBusConfiguration implements ConfigInterface
protected $configuration = array(
Definition::CONFIG_ROOT => array(
Definition::COMMAND_BUS => array(),
Definition::EVENT_BUS => array(),
)
);

Expand All @@ -43,15 +45,6 @@ class ServiceBusConfiguration implements ConfigInterface
*/
protected $commandFactory;

/**
* @var ServiceLocatorInterface
*/
protected $invokeStrategyLoader;

/**
* @var ServiceLocatorInterface
*/
protected $commandReceiverLoader;

/**
* @param null|array $aConfiguration
Expand Down Expand Up @@ -89,14 +82,6 @@ public function configureServiceManager(ServiceManager $serviceManager)
$serviceManager->setService(Definition::COMMAND_FACTORY, $this->commandFactory);
}

if (!is_null($this->invokeStrategyLoader)) {
$serviceManager->setService(Definition::INVOKE_STRATEGY_LOADER, $this->invokeStrategyLoader);
}

if (!is_null($this->commandReceiverLoader)) {
$serviceManager->setService(Definition::COMMAND_RECEIVER_LOADER, $this->commandReceiverLoader);
}

if (isset($this->configuration[Definition::CONFIG_ROOT][Definition::DEFAULT_COMMAND_BUS])) {
$serviceManager->setDefaultCommandBus(
$this->configuration[Definition::CONFIG_ROOT][Definition::DEFAULT_COMMAND_BUS]
Expand Down Expand Up @@ -187,21 +172,5 @@ public function setCommandFactory(CommandFactoryInterface $commandFactory)
{
$this->commandFactory = $commandFactory;
}

/**
* @param ServiceLocatorInterface $commandReceiverLoader
*/
public function setCommandReceiverLoader(ServiceLocatorInterface $commandReceiverLoader)
{
$this->commandReceiverLoader = $commandReceiverLoader;
}

/**
* @param ServiceLocatorInterface $invokeStrategyLoader
*/
public function setInvokeStrategyLoader(ServiceLocatorInterface $invokeStrategyLoader)
{
$this->invokeStrategyLoader = $invokeStrategyLoader;
}
}

15 changes: 2 additions & 13 deletions src/Prooph/ServiceBus/Service/ServiceBusManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@ class ServiceBusManager extends ServiceManager
*/
protected $mainServiceLocator;

/**
* @var array
*/
protected $invokableClasses = array(
'commandbusloader' => 'Prooph\ServiceBus\Service\CommandBusLoader',
'commandreceiverloader' => 'Prooph\ServiceBus\Service\CommandReceiverLoader',
'invokestrategyloader' => 'Prooph\ServiceBus\Service\InvokeStrategyLoader',
'messagedispatcherloader' => 'Prooph\ServiceBus\Service\MessageDispatcherLoader',
'queueloader' => 'Prooph\ServiceBus\Service\QueueLoader',
'eventreceiverloader' => 'Prooph\ServiceBus\Service\EventReceiverLoader',
'eventbusloader' => 'Prooph\ServiceBus\Service\EventBusLoader',
);

/**
* @param ConfigInterface $config
*/
Expand All @@ -85,6 +72,8 @@ public function __construct(ConfigInterface $config = null)
$instance->setServiceLocator($this->getMainServiceLocator());
}
});

$this->addAbstractFactory('Prooph\ServiceBus\Service\Factory\AbstractLoaderFactory');
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) Alexander Miertsch <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 05.07.14 - 21:05
*/

namespace Prooph\ServiceBusTest\Mock\Configtest;

use Prooph\ServiceBus\Command\CommandBus;

class CustomBus extends CommandBus
{
public $message;

public function __construct($message) {
$this->message = $message;
}
}

36 changes: 36 additions & 0 deletions tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBusFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) Alexander Miertsch <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 05.07.14 - 21:05
*/

namespace Prooph\ServiceBusTest\Mock\Configtest;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Class CustomBusFactory
*
* @package Prooph\ServiceBusTest\Mock\Configtest
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class CustomBusFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new CustomBus("Created via factory");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) Alexander Miertsch <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 05.07.14 - 21:27
*/

namespace Prooph\ServiceBusTest\Mock\Configtest;

use Prooph\ServiceBus\Command\CommandReceiver;

class CustomCommandReceiver extends CommandReceiver
{
public $message;

public function __construct($message) {
$this->message = $message;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the prooph/service-bus.
* (c) Alexander Miertsch <contact@prooph.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 05.07.14 - 21:27
*/

namespace Prooph\ServiceBusTest\Mock\Configtest;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class CustomCommandReceiverFactory implements FactoryInterface
{

/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new CustomCommandReceiver("Created via factory");
}
}

Loading

0 comments on commit 11ca278

Please sign in to comment.