diff --git a/src/Prooph/ServiceBus/Service/Definition.php b/src/Prooph/ServiceBus/Service/Definition.php index 779e3e0..a23d8ef 100644 --- a/src/Prooph/ServiceBus/Service/Definition.php +++ b/src/Prooph/ServiceBus/Service/Definition.php @@ -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"; diff --git a/src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php b/src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php new file mode 100644 index 0000000..de34947 --- /dev/null +++ b/src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php @@ -0,0 +1,94 @@ + + * + * 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 + */ +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; + } +} + \ No newline at end of file diff --git a/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php b/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php index a399565..d3c1b75 100644 --- a/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php +++ b/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php @@ -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; @@ -25,6 +26,7 @@ class ServiceBusConfiguration implements ConfigInterface protected $configuration = array( Definition::CONFIG_ROOT => array( Definition::COMMAND_BUS => array(), + Definition::EVENT_BUS => array(), ) ); @@ -43,15 +45,6 @@ class ServiceBusConfiguration implements ConfigInterface */ protected $commandFactory; - /** - * @var ServiceLocatorInterface - */ - protected $invokeStrategyLoader; - - /** - * @var ServiceLocatorInterface - */ - protected $commandReceiverLoader; /** * @param null|array $aConfiguration @@ -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] @@ -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; - } } \ No newline at end of file diff --git a/src/Prooph/ServiceBus/Service/ServiceBusManager.php b/src/Prooph/ServiceBus/Service/ServiceBusManager.php index 564758b..86669b2 100644 --- a/src/Prooph/ServiceBus/Service/ServiceBusManager.php +++ b/src/Prooph/ServiceBus/Service/ServiceBusManager.php @@ -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 */ @@ -85,6 +72,8 @@ public function __construct(ConfigInterface $config = null) $instance->setServiceLocator($this->getMainServiceLocator()); } }); + + $this->addAbstractFactory('Prooph\ServiceBus\Service\Factory\AbstractLoaderFactory'); } /** diff --git a/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBus.php b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBus.php new file mode 100644 index 0000000..dfbf0bb --- /dev/null +++ b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBus.php @@ -0,0 +1,24 @@ + + * + * 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; + } +} + \ No newline at end of file diff --git a/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBusFactory.php b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBusFactory.php new file mode 100644 index 0000000..79ac4b1 --- /dev/null +++ b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomBusFactory.php @@ -0,0 +1,36 @@ + + * + * 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 + */ +class CustomBusFactory implements FactoryInterface +{ + /** + * Create service + * + * @param ServiceLocatorInterface $serviceLocator + * @return mixed + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + return new CustomBus("Created via factory"); + } +} + \ No newline at end of file diff --git a/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiver.php b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiver.php new file mode 100644 index 0000000..0de01ad --- /dev/null +++ b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiver.php @@ -0,0 +1,24 @@ + + * + * 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; + } +} + \ No newline at end of file diff --git a/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiverFactory.php b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiverFactory.php new file mode 100644 index 0000000..b01c901 --- /dev/null +++ b/tests/Prooph/ServiceBusTest/Mock/Configtest/CustomCommandReceiverFactory.php @@ -0,0 +1,31 @@ + + * + * 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"); + } +} + \ No newline at end of file diff --git a/tests/Prooph/ServiceBusTest/Service/ServiceBusConfigurationTest.php b/tests/Prooph/ServiceBusTest/Service/ServiceBusConfigurationTest.php new file mode 100644 index 0000000..8328437 --- /dev/null +++ b/tests/Prooph/ServiceBusTest/Service/ServiceBusConfigurationTest.php @@ -0,0 +1,96 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 05.07.14 - 19:01 + */ + +namespace Prooph\ServiceBusTest\Service; + +use Prooph\ServiceBus\Service\Definition; +use Prooph\ServiceBus\Service\ServiceBusConfiguration; +use Prooph\ServiceBus\Service\ServiceBusManager; +use Prooph\ServiceBusTest\TestCase; + +/** + * Class ServiceBusConfigurationTest + * + * @package Prooph\ServiceBusTest\Service + * @author Alexander Miertsch + */ +class ServiceBusConfigurationTest extends TestCase +{ + /** + * @test + */ + public function it_sets_up_invoke_strategy_manager_with_configured_invoke_strategy() + { + $serviceBusConfiguration = new ServiceBusConfiguration(array( + Definition::CONFIG_ROOT => array( + Definition::COMMAND_HANDLER_INVOKE_STRATEGIES => array( + 'do_something_invoke_strategy' + ), + Definition::INVOKE_STRATEGY_LOADER => array( + 'invokables' => array( + 'do_something_invoke_strategy' => 'Prooph\ServiceBusTest\Mock\DoSomethingInvokeStrategy' + ) + ) + ) + )); + + $sbm = new ServiceBusManager($serviceBusConfiguration); + + $invokeStrategy = $sbm->get(Definition::INVOKE_STRATEGY_LOADER)->get('do_something_invoke_strategy'); + + $this->assertInstanceOf('Prooph\ServiceBusTest\Mock\DoSomethingInvokeStrategy', $invokeStrategy); + } + + /** + * @test + */ + public function it_sets_up_command_bus_loader_with_configured_factory() + { + $serviceBusConfiguration = new ServiceBusConfiguration(array( + Definition::CONFIG_ROOT => array( + Definition::COMMAND_BUS_LOADER => array( + 'factories' => array( + 'mock-custom-bus' => 'Prooph\ServiceBusTest\Mock\Configtest\CustomBusFactory', + ) + ) + ) + )); + + $sbm = new ServiceBusManager($serviceBusConfiguration); + + $commandBus = $sbm->get(Definition::COMMAND_BUS_LOADER)->get('mock-custom-bus'); + + $this->assertEquals("Created via factory", $commandBus->message); + } + + /** + * @test + */ + public function it_sets_up_command_receiver_loader_with_configured_factory() + { + $serviceBusConfiguration = new ServiceBusConfiguration(array( + Definition::CONFIG_ROOT => array( + Definition::COMMAND_RECEIVER_LOADER => array( + 'factories' => array( + 'mock-custom-receiver' => 'Prooph\ServiceBusTest\Mock\Configtest\CustomCommandReceiverFactory', + ) + ) + ) + )); + + $sbm = new ServiceBusManager($serviceBusConfiguration); + + $customReceiver = $sbm->get(Definition::COMMAND_RECEIVER_LOADER)->get('mock-custom-receiver'); + + $this->assertEquals("Created via factory", $customReceiver->message); + } +} + \ No newline at end of file