Skip to content

Commit

Permalink
Merge pull request #86 from prooph/develop
Browse files Browse the repository at this point in the history
Merge v4.6 changes
  • Loading branch information
codeliner committed Oct 21, 2015
2 parents e20c9b5 + 3108ebd commit 3bf3fb0
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 137 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
13 changes: 7 additions & 6 deletions docs/factories.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
# Interop\Container + Factories
# Interop + Factories

[Back to documentation](../README.md#documentation)

Instead of providing a module, a bundle, a bridge or similar framework integration prooph/service-bus ships with
`container-aware factories`.
`interop factories`.

## Factory-Driven Message Bus Creation

The concept behind these factories is simple but powerful. It allows us to provide you with bootstrapping logic for
the message buses without the need to rely on a specific framework. However, the factories have two requirements.
the message buses without the need to rely on a specific framework. However, the factories have three requirements.

### Requirements

1. Your Inversion of Control container must implement the [interop-container interface](https://github.com/container-interop/container-interop).
2. The application configuration should be registered with the service id `config` in the container.
2. [interop-config](https://github.com/sandrokeil/interop-config) must be installed
3. The application configuration should be registered with the service id `config` in the container.

*Note: Don't worry, if your environment doesn't provide the requirements. You can
always bootstrap a message bus by hand. Just look at the factories for inspiration in this case.*

## Customizing via Configuration

In the `config` folder you will find a [configuration skeleton](../config/prooph_service_bus.config.php)
and a [another configuration skeleton](../config/services.config.php) which contain the factories for the message guards.
In the `config` folder you will find a [package configuration skeleton](../config/prooph_service_bus.config.php)
and a [services configuration skeleton](../config/services.config.php) which contain the factories for the message guards.
The configuration is a simple PHP array flavored with some comments to help you understand the structure.

Now follow the simple steps below to integrate prooph/service-bus in your framework and/or application.
Expand Down
13 changes: 12 additions & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ does not allow access to the command, an [UnauthorizedException](../src/Prooph/S
If you want to protect the query bus, you can also use the route guard, but in some situations, you want to deny access based on the result
of the query. In this case it's important to make checks on the query results.

The route guard passes the message to the [AuthorizationService](../src/Prooph/ServiceBus/Plugin/Guard/AuthorizationService.php) as context, so you can make assertions on it.

We also provide [service-bus-zfc-rbac-brdige](https://github.com/prooph/service-bus-zfc-rbac-bridge), a bridge to marry these guards with ZFC-Rbac.
You can also find some configuration examples in this repository.

Expand Down Expand Up @@ -168,4 +170,13 @@ $eventBus->utilize($messageProducerPlugin);

//Each event will now be routed to the async message producer
$eventBus->dispatch($domainEvent);
```
```

# StringMessageInitializerPlugin

When you dispatch a string message you likely want to use it also as `message name`. This plugin does exactly that, just utilize the plugin and it will work.

```
$bus->utilize(new StringMessageInitializerPlugin);
$bus->dispatch('my.dispatch.message');
```
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 <[email protected]>
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 <[email protected]>
*/
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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
5 changes: 4 additions & 1 deletion src/Plugin/Guard/RouteGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public function __construct(AuthorizationService $authorizationService)
*/
public function onRoute(ActionEvent $actionEvent)
{
if ($this->authorizationService->isGranted($actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME))) {
if ($this->authorizationService->isGranted(
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME),
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE)
)) {
return;
}

Expand Down
1 change: 0 additions & 1 deletion src/Plugin/InvokeStrategy/HandleCommandStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*/
class HandleCommandStrategy extends AbstractInvokeStrategy
{

/**
* @param mixed $handler
* @param mixed $message
Expand Down
Loading

0 comments on commit 3bf3fb0

Please sign in to comment.