Skip to content

Commit

Permalink
Merge pull request #77 from sandrokeil/interop-config
Browse files Browse the repository at this point in the history
Implemented Interop Config Library
  • Loading branch information
prolic committed Oct 18, 2015
2 parents b881862 + 712f07e commit a1b5f0b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 126 deletions.
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@
"php": ">=5.5",
"beberlei/assert": "^2.0",
"prooph/common" : "^3.3",
"react/promise" : "^2.2",
"container-interop/container-interop" : "^1.1"
"react/promise" : "^2.2"
},
"require-dev": {
"phpunit/phpunit": "~4.8",
"fabpot/php-cs-fixer": "1.7.*",
"satooshi/php-coveralls": "dev-master"
"satooshi/php-coveralls": "dev-master",
"container-interop/container-interop" : "^1.1",
"sandrokeil/interop-config": "^0.3"
},
"suggest": {
"prooph/event-store": "Let the EventBus dispatch persisted DomainEvents",
"zendframework/zend-servicemanager": "Use Zf2 ServiceManager to lazy load your handlers and listeners",
"prooph/service-bus-zfc-rbac-bridge": "Use ZfcRbac as authorization service for route and finalize guard"
"prooph/service-bus-zfc-rbac-bridge": "Use ZfcRbac as authorization service for route and finalize guard",
"container-interop/container-interop": "For usage of provided factories",
"sandrokeil/interop-config": "For usage of provided factories"
},
"autoload": {
"psr-4": {
Expand Down
103 changes: 47 additions & 56 deletions src/Container/AbstractBusFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
Expand All @@ -11,6 +12,9 @@

namespace Prooph\ServiceBus\Container;

use Interop\Config\ConfigurationTrait;
use Interop\Config\RequiresContainerId;
use Interop\Config\ProvidesDefaultOptions;
use Interop\Container\ContainerInterface;
use Prooph\Common\Messaging\MessageFactory;
use Prooph\ServiceBus\Exception\RuntimeException;
Expand All @@ -24,8 +28,10 @@
* @package Prooph\ServiceBus\Container
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
abstract class AbstractBusFactory
abstract class AbstractBusFactory implements RequiresContainerId, ProvidesDefaultOptions
{
use ConfigurationTrait;

/**
* Returns the FQCN of a bus extending Prooph\ServiceBus\MessageBus
*
Expand All @@ -34,29 +40,55 @@ abstract class AbstractBusFactory
abstract protected function getBusClass();

/**
* Returns config key used within the prooph.service_bus config namespace to identify the bus.
* Returns the default router class to use if no one was specified in the config
*
* @return string
*/
abstract protected function getBusConfigKey();
abstract protected function getDefaultRouterClass();

/**
* Returns the default router class to use if no one was specified in the config
*
* @return string
* @inheritdoc
*/
abstract protected function getDefaultRouterClass();
public function vendorName()
{
return 'prooph';
}

/**
* Create service
* @inheritdoc
*/
public function packageName()
{
return 'service_bus';
}

/**
* @inheritdoc
*/
public function defaultOptions()
{
return [
'enable_handler_location' => true,
'message_factory' => MessageFactory::class,
];
}

/**
* Create service.
*
* @param ContainerInterface $container
* @throws RuntimeException
* @return MessageBus
*/
public function __invoke(ContainerInterface $container)
{
$busConfig = $this->getBusConfig($container);
$config = [];

if ($container->has('config')) {
$config = $container->get('config');
}

$busConfig = $this->optionsWithFallback($config);

$busClass = $this->getBusClass();

Expand All @@ -70,58 +102,17 @@ public function __invoke(ContainerInterface $container)
$this->attachRouter($bus, $busConfig['router']);
}

if (!isset($busConfig['enable_handler_location']) || (bool)$busConfig['enable_handler_location']) {
if ((bool) $busConfig['enable_handler_location']) {
$bus->utilize(new ServiceLocatorPlugin($container));
}

$messageFactoryServiceId = isset($busConfig['message_factory'])
? $busConfig['message_factory']
: MessageFactory::class;

if ($container->has($messageFactoryServiceId)) {
$bus->utilize(new MessageFactoryPlugin($container->get($messageFactoryServiceId)));
if ($container->has($busConfig['message_factory'])) {
$bus->utilize(new MessageFactoryPlugin($container->get($busConfig['message_factory'])));
}

return $bus;
}

/**
* @param ContainerInterface $container
* @return array
* @throws RuntimeException
*/
private function getBusConfig(ContainerInterface $container)
{
if (! $container->has('config')) {
return [];
}

$config = $container->get('config');

if (!is_array($config) && !$config instanceof \ArrayAccess) {
throw new RuntimeException(sprintf(
'Application config registered in the container %s must be either of type array or implement \ArrayAccess. Otherwise it is not compatible with %s',
get_class($container),
get_called_class()
));
}

if (!isset($config['prooph']['service_bus'][$this->getBusConfigKey()])) {
return [];
}

$busConfig = $config['prooph']['service_bus'][$this->getBusConfigKey()];

if (!is_array($busConfig) && !$busConfig instanceof \ArrayAccess) {
throw new RuntimeException(sprintf(
'Config prooph.service_bus.%s must either be of type array or implement \ArrayAccess.',
$this->getBusConfigKey()
));
}

return $busConfig;
}

/**
* @param MessageBus $bus
* @param array $utils
Expand All @@ -134,7 +125,7 @@ private function attachPlugins(MessageBus $bus, array &$utils, ContainerInterfac
if (! is_string($util) || ! $container->has($util)) {
throw new RuntimeException(sprintf(
'Wrong message bus utility configured at %s. Either it is not a string or unknown by the container.',
'prooph.service_bus.' . $this->getBusConfigKey() . '.' . $index
'prooph.service_bus.' . $this->containerId() . '.' . $index
));
}

Expand All @@ -148,9 +139,9 @@ private function attachPlugins(MessageBus $bus, array &$utils, ContainerInterfac
*/
private function attachRouter(MessageBus $bus, array &$routerConfig)
{
$routerClass = isset($routerConfig['type'])? (string)$routerConfig['type'] : $this->getDefaultRouterClass();
$routerClass = isset($routerConfig['type']) ? (string)$routerConfig['type'] : $this->getDefaultRouterClass();

$routes = isset($routerConfig['routes'])? $routerConfig['routes'] : [];
$routes = isset($routerConfig['routes']) ? $routerConfig['routes'] : [];

$router = new $routerClass($routes);

Expand Down
10 changes: 5 additions & 5 deletions src/Container/CommandBusFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
Expand All @@ -22,21 +23,20 @@
*/
class CommandBusFactory extends AbstractBusFactory
{

/**
* @inheritdoc
*/
protected function getBusClass()
public function containerId()
{
return CommandBus::class;
return 'command_bus';
}

/**
* @inheritdoc
*/
protected function getBusConfigKey()
protected function getBusClass()
{
return 'command_bus';
return CommandBus::class;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Container/EventBusFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
Expand All @@ -22,21 +23,20 @@
*/
class EventBusFactory extends AbstractBusFactory
{

/**
* @inheritdoc
*/
protected function getBusClass()
public function containerId()
{
return EventBus::class;
return 'event_bus';
}

/**
* @inheritdoc
*/
protected function getBusConfigKey()
protected function getBusClass()
{
return 'event_bus';
return EventBus::class;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/Container/QueryBusFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the prooph/service-bus.
* (c) 2014-2015 prooph software GmbH <contact@prooph.de>
Expand All @@ -24,17 +25,17 @@ class QueryBusFactory extends AbstractBusFactory
/**
* @inheritdoc
*/
protected function getBusClass()
public function containerId()
{
return QueryBus::class;
return 'query_bus';
}

/**
* @inheritdoc
*/
protected function getBusConfigKey()
protected function getBusClass()
{
return 'query_bus';
return QueryBus::class;
}

/**
Expand Down
52 changes: 0 additions & 52 deletions tests/Container/BusFactoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,58 +364,6 @@ public function it_can_handle_application_config_being_of_type_array_access($bus
$this->assertInstanceOf($busClass, $bus);
}

/**
* @test
* @dataProvider provideBuses
* @expectedException \Prooph\ServiceBus\Exception\RuntimeException
*/
public function it_throws_an_exception_if_application_config_is_neither_array_nor_array_access($busClass, $busConfigKey, $busFactory)
{
$container = $this->prophesize(ContainerInterface::class);

$configObject = new \stdClass();

$configObject->prooph = [
'service_bus' => [
$busConfigKey => [
'plugins' => [
'first_plugin_service_id',
]
]
]
];

$container->has('config')->willReturn(true);
$container->get('config')->willReturn($configObject);

$bus = $busFactory($container->reveal());
}

/**
* @test
* @dataProvider provideBuses
* @expectedException \Prooph\ServiceBus\Exception\RuntimeException
*/
public function it_throws_an_exception_if_bus_config_is_neither_array_nor_array_access($busClass, $busConfigKey, $busFactory)
{
$container = $this->prophesize(ContainerInterface::class);

$busConfig = new \stdClass();

$busConfig->plugins = [];

$container->has('config')->willReturn(true);
$container->get('config')->willReturn(new \ArrayObject([
'prooph' => [
'service_bus' => [
$busConfigKey => $busConfig
]
]
]));

$bus = $busFactory($container->reveal());
}


public function provideBuses()
{
Expand Down

0 comments on commit a1b5f0b

Please sign in to comment.