Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Interop Config Library #77

Merged
merged 3 commits into from
Oct 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must not be a class name. it's a service name registered in the container.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default is really a FQCN, see https://github.com/prooph/service-bus/blob/master/src/Container/AbstractBusFactory.php#L151

It is used to be able to create a bus without further dependencies

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


/**
* 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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, this is a service name registered in the container, it's not always a classname

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also in this case the name is correct: https://github.com/prooph/service-bus/blob/master/src/Container/AbstractBusFactory.php#L61

The factory needs to know which bus class should be used to create the bus

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sorry

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np

{
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 @@ -25,17 +26,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 @@ -365,58 +365,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