diff --git a/src/Prooph/ServiceBus/Command/AbstractCommandFactoryFactory.php b/src/Prooph/ServiceBus/Command/AbstractCommandFactoryFactory.php new file mode 100644 index 0000000..49daa88 --- /dev/null +++ b/src/Prooph/ServiceBus/Command/AbstractCommandFactoryFactory.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 05.07.14 - 22:51 + */ + +namespace Prooph\ServiceBus\Command; + +use Prooph\ServiceBus\Service\CommandFactoryLoader; +use Zend\ServiceManager\AbstractFactoryInterface; +use Zend\ServiceManager\ServiceLocatorInterface; + +/** + * Class AbstractCommandFactoryFactory + * + * @package Prooph\ServiceBus\Command + * @author Alexander Miertsch + */ +class AbstractCommandFactoryFactory implements AbstractFactoryInterface +{ + protected $commandFactory; + + /** + * 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) + { + if ($serviceLocator instanceof CommandFactoryLoader) { + return true; + } + } + + /** + * Create service with name + * + * @param ServiceLocatorInterface $serviceLocator + * @param $name + * @param $requestedName + * @return mixed + */ + public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) + { + if (is_null($this->commandFactory)) { + $this->commandFactory = new CommandFactory(); + } + return $this->commandFactory; + } +} + \ No newline at end of file diff --git a/src/Prooph/ServiceBus/Command/CommandReceiver.php b/src/Prooph/ServiceBus/Command/CommandReceiver.php index 9de4637..cdf66e9 100644 --- a/src/Prooph/ServiceBus/Command/CommandReceiver.php +++ b/src/Prooph/ServiceBus/Command/CommandReceiver.php @@ -13,6 +13,7 @@ use Prooph\ServiceBus\Exception\RuntimeException; use Prooph\ServiceBus\Message\MessageInterface; +use Prooph\ServiceBus\Service\CommandFactoryLoader; use Prooph\ServiceBus\Service\Definition; use Prooph\ServiceBus\Service\InvokeStrategyLoader; use Zend\EventManager\EventManager; @@ -33,9 +34,9 @@ class CommandReceiver implements CommandReceiverInterface protected $commandMap = array(); /** - * @var CommandFactoryInterface + * @var CommandFactoryLoader */ - protected $commandFactory; + protected $commandFactoryLoader; /** * @var ServiceLocatorInterface @@ -85,7 +86,7 @@ public function handle(MessageInterface $aMessage) return; } - $command = $this->getCommandFactory()->fromMessage($aMessage); + $command = $this->getCommandFactoryLoader()->get($aMessage->name())->fromMessage($aMessage); $handler = $this->commandHandlerLocator->get($this->commandMap[$aMessage->name()]); @@ -128,23 +129,19 @@ public function handle(MessageInterface $aMessage) } /** - * @param CommandFactoryInterface $aCommandFactory + * @param CommandFactoryLoader $aCommandFactoryLoader */ - public function setCommandFactory(CommandFactoryInterface $aCommandFactory) + public function setCommandFactoryLoader(CommandFactoryLoader $aCommandFactoryLoader) { - $this->commandFactory = $aCommandFactory; + $this->commandFactoryLoader = $aCommandFactoryLoader; } /** - * @return CommandFactoryInterface + * @return CommandFactoryLoader */ - public function getCommandFactory() + public function getCommandFactoryLoader() { - if (is_null($this->commandFactory)) { - $this->commandFactory = new CommandFactory(); - } - - return $this->commandFactory; + return $this->commandFactoryLoader; } /** diff --git a/src/Prooph/ServiceBus/Command/DefaultCommandReceiverFactory.php b/src/Prooph/ServiceBus/Command/DefaultCommandReceiverFactory.php index 3805857..29da5a6 100644 --- a/src/Prooph/ServiceBus/Command/DefaultCommandReceiverFactory.php +++ b/src/Prooph/ServiceBus/Command/DefaultCommandReceiverFactory.php @@ -126,9 +126,7 @@ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $ ); } - if ($mainServiceLocator->has(Definition::COMMAND_FACTORY)) { - $commandReceiver->setCommandFactory($mainServiceLocator->get(Definition::COMMAND_FACTORY)); - } + $commandReceiver->setCommandFactoryLoader($mainServiceLocator->get(Definition::COMMAND_FACTORY_LOADER)); return $commandReceiver; } diff --git a/src/Prooph/ServiceBus/Service/CommandFactoryLoader.php b/src/Prooph/ServiceBus/Service/CommandFactoryLoader.php new file mode 100644 index 0000000..594c3f3 --- /dev/null +++ b/src/Prooph/ServiceBus/Service/CommandFactoryLoader.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + * + * Date: 05.07.14 - 22:30 + */ + +namespace Prooph\ServiceBus\Service; + +use Prooph\ServiceBus\Command\AbstractCommandFactoryFactory; +use Prooph\ServiceBus\Command\CommandFactoryInterface; +use Zend\ServiceManager\AbstractPluginManager; +use Zend\ServiceManager\ConfigInterface; +use Zend\ServiceManager\Exception; + +/** + * Class CommandFactoryLoader + * + * @package Prooph\ServiceBus\Service + * @author Alexander Miertsch + */ +class CommandFactoryLoader extends AbstractPluginManager +{ + /** + * @param ConfigInterface $aConfig + */ + public function __construct(ConfigInterface $aConfig = null) + { + parent::__construct($aConfig); + + $this->abstractFactories[] = new AbstractCommandFactoryFactory(); + } + + /** + * Validate the plugin + * + * Checks that the filter loaded is either a valid callback or an instance + * of FilterInterface. + * + * @param mixed $plugin + * @throws Exception\RuntimeException + * @return void + */ + public function validatePlugin($plugin) + { + if (! $plugin instanceof CommandFactoryInterface) { + throw new Exception\RuntimeException(sprintf( + 'CommandFactory must be instance of Prooph\ServiceBus\Command\CommandFactoryInterface,' + . 'instance of type %s given', + ((is_object($plugin)? get_class($plugin) : gettype($plugin))) + )); + } + } +} + \ No newline at end of file diff --git a/src/Prooph/ServiceBus/Service/Definition.php b/src/Prooph/ServiceBus/Service/Definition.php index a23d8ef..1339dae 100644 --- a/src/Prooph/ServiceBus/Service/Definition.php +++ b/src/Prooph/ServiceBus/Service/Definition.php @@ -33,6 +33,8 @@ class Definition const COMMAND_FACTORY = "command_factory"; + const COMMAND_FACTORY_LOADER = "command_factory_loader"; + const EVENT_BUS = "event_bus"; const DEFAULT_EVENT_BUS = "default_event_bus"; @@ -43,6 +45,8 @@ class Definition const EVENT_FACTORY = "event_factory"; + const EVENT_FACTORY_LOADER = "event_factory_loader"; + const QUEUE = "queue"; const MESSAGE_DISPATCHER = "message_dispatcher"; @@ -51,6 +55,8 @@ class Definition const MESSAGE_FACTORY = "message_factory"; + const MESSAGE_FACTORY_LOADER = "message_factory_loader"; + const COMMAND_BUS_LOADER = "command_bus_loader"; const COMMAND_RECEIVER_LOADER = "command_receiver_loader"; diff --git a/src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php b/src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php index de34947..8931da6 100644 --- a/src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php +++ b/src/Prooph/ServiceBus/Service/Factory/AbstractLoaderFactory.php @@ -84,7 +84,8 @@ protected function getServicesMap() 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' + Definition::QUEUE_LOADER => 'Prooph\ServiceBus\Service\QueueLoader', + Definition::COMMAND_FACTORY_LOADER => 'Prooph\ServiceBus\Service\CommandFactoryLoader', ); } diff --git a/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php b/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php index d3c1b75..07c16b9 100644 --- a/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php +++ b/src/Prooph/ServiceBus/Service/ServiceBusConfiguration.php @@ -40,12 +40,6 @@ class ServiceBusConfiguration implements ConfigInterface */ protected $eventHandlers = array(); - /** - * @var CommandFactoryInterface - */ - protected $commandFactory; - - /** * @param null|array $aConfiguration */ @@ -78,10 +72,6 @@ public function configureServiceManager(ServiceManager $serviceManager) $serviceBusManagerServicesConfig->configureServiceManager($serviceManager); - if (!is_null($this->commandFactory)) { - $serviceManager->setService(Definition::COMMAND_FACTORY, $this->commandFactory); - } - if (isset($this->configuration[Definition::CONFIG_ROOT][Definition::DEFAULT_COMMAND_BUS])) { $serviceManager->setDefaultCommandBus( $this->configuration[Definition::CONFIG_ROOT][Definition::DEFAULT_COMMAND_BUS] diff --git a/tests/Prooph/ServiceBusTest/Command/CommandBusTest.php b/tests/Prooph/ServiceBusTest/Command/CommandBusTest.php index 25f1d4e..3551098 100644 --- a/tests/Prooph/ServiceBusTest/Command/CommandBusTest.php +++ b/tests/Prooph/ServiceBusTest/Command/CommandBusTest.php @@ -16,6 +16,7 @@ use Prooph\ServiceBus\Message\InMemoryMessageDispatcher; use Prooph\ServiceBus\Message\MessageFactory; use Prooph\ServiceBus\Message\Queue; +use Prooph\ServiceBus\Service\CommandFactoryLoader; use Prooph\ServiceBus\Service\CommandReceiverLoader; use Prooph\ServiceBus\Service\ServiceBusManager; use Prooph\ServiceBusTest\Mock\DoSomething; @@ -61,6 +62,8 @@ protected function setUp() $serviceBusManager ); + $commandReceiver->setCommandFactoryLoader(new CommandFactoryLoader()); + $commandReceiverLoader = new CommandReceiverLoader(); $commandReceiverLoader->setService('test-case-bus', $commandReceiver); diff --git a/tests/Prooph/ServiceBusTest/Command/CommandReceiverTest.php b/tests/Prooph/ServiceBusTest/Command/CommandReceiverTest.php index ff64255..9e241b1 100644 --- a/tests/Prooph/ServiceBusTest/Command/CommandReceiverTest.php +++ b/tests/Prooph/ServiceBusTest/Command/CommandReceiverTest.php @@ -15,6 +15,7 @@ use Prooph\ServiceBus\Command\CommandReceiver; use Prooph\ServiceBus\Message\MessageHeader; use Prooph\ServiceBus\Message\StandardMessage; +use Prooph\ServiceBus\Service\CommandFactoryLoader; use Prooph\ServiceBus\Service\ServiceBusManager; use Prooph\ServiceBusTest\Mock\DoSomething; use Prooph\ServiceBusTest\TestCase; @@ -67,6 +68,8 @@ protected function setUp() ), $commandHandlerLocator ); + + $this->commandReceiver->setCommandFactoryLoader(new CommandFactoryLoader()); } /** diff --git a/tests/Prooph/ServiceBusTest/Command/DefaultCommandReceiverFactoryTest.php b/tests/Prooph/ServiceBusTest/Command/DefaultCommandReceiverFactoryTest.php index a95b0d3..bcfcd9d 100644 --- a/tests/Prooph/ServiceBusTest/Command/DefaultCommandReceiverFactoryTest.php +++ b/tests/Prooph/ServiceBusTest/Command/DefaultCommandReceiverFactoryTest.php @@ -88,9 +88,6 @@ protected function setUp() //Register InvokeStrategyLoader as Service $this->serviceBusManager->setService(Definition::INVOKE_STRATEGY_LOADER, $invokeStrategyLoader); - //Register CommandFactory as Service, this is not necessary but we do it for testing purposes - $this->serviceBusManager->setService(Definition::COMMAND_FACTORY, new CommandFactory()); - $this->commandReceiverLoader = new CommandReceiverLoader(); //Set MainServiceManager as ServiceLocator for the CommandReceiverLoader @@ -120,11 +117,6 @@ public function it_creates_a_fully_configured_command_receiver() $commandReceiver = $defaultCommandReceiverFactory ->createServiceWithName($this->commandReceiverLoader, 'testcasebus', 'test-case-bus'); - $this->assertSame( - $this->serviceBusManager->get(Definition::COMMAND_FACTORY), - $commandReceiver->getCommandFactory() - ); - $this->assertSame( $this->serviceBusManager->get(Definition::INVOKE_STRATEGY_LOADER), $commandReceiver->getInvokeStrategyLoader()