Skip to content

Commit 1b7c980

Browse files
author
Nil Portugués Calderó
committed
Adding PSR-11 support plus Symfony 2&3 Containers
1 parent d33bd49 commit 1b7c980

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+579
-37
lines changed

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Build Status](https://travis-ci.org/PHPMessageBus/messagebus.svg?branch=master)](https://travis-ci.org/PHPMessageBus/messagebus) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/PHPMessageBus/messagebus/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/PHPMessageBus/messagebus/?branch=master) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/04e1a12f-e916-4ae6-875f-5e694fb71b6a/mini.png?)](https://insight.sensiolabs.com/projects/04e1a12f-e916-4ae6-875f-5e694fb71b6a) [![Latest Stable Version](https://poser.pugx.org/nilportugues/messagebus/v/stable?)](https://packagist.org/packages/nilportugues/messagebus) [![Total Downloads](https://poser.pugx.org/nilportugues/messagebus/downloads?)](https://packagist.org/packages/nilportugues/messagebus) [![License](https://poser.pugx.org/nilportugues/messagebus/license?)](https://packagist.org/packages/nilportugues/messagebus)
44
[![Donate](https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif)](https://www.paypal.me/nilportugues)
55

6-
Implementation for the CommandBus, the QueryBus and the EventBus in PHP 7.
6+
Implementation for the CommandBus, the QueryBus and the EventBus in PHP 7 and using PSR-11.
77

88
---
99

@@ -157,7 +157,7 @@ final class RegisterUserHandler implements CommandHandler
157157

158158
I'm assuming you're using some kind Service Container. Now it's time to register your CommandHandler.
159159

160-
For instance, in a `Interop\Container` compliant Service Container, we can do this as follows:
160+
For instance, in a `Psr\Container` compliant Service Container, we can do this as follows:
161161

162162
```php
163163
<?php
@@ -186,9 +186,9 @@ For custom strategies, you may write your own implementing the `NilPortugues\Mes
186186

187187
Classes implementing this interface will be resolving the class for the instance required based on the output of the CommandTranslator used.
188188

189-
This package provides an implementation, `NilPortugues\MessageBus\CommandBus\Resolver\InteropContainerResolver`, that expects any Service Container implementing the `Interop\Container` interface.
189+
This package provides an implementation, `NilPortugues\MessageBus\CommandBus\Resolver\PsrContainerResolver`, that expects any Service Container implementing the `Psr\Container` interface.
190190

191-
For custom strategies, such as Symfony Container, you may write your own implementing the `NilPortugues\MessageBus\CommandBus\Contracts\CommandHandlerResolver` interface.
191+
For Symfony 2 and 3 framework users up to version 3.2, you should use Symfony Container: `NilPortugues\MessageBus\CommandBus\Resolver\SymfonyContainerResolver`. For Symfony 3.3 and up use the PSR-11 ContainerResolver class.
192192

193193
#### 1.1.5 - Registering the remaining CommandBus classes
194194

@@ -203,7 +203,7 @@ $container['CommandTranslator'] = function($container) {
203203
};
204204

205205
$container['CommandHandlerResolver'] = function($container) {
206-
return new \NilPortugues\MessageBus\CommandBus\Resolver\InteropContainerResolver($container);
206+
return new \NilPortugues\MessageBus\CommandBus\Resolver\PsrContainerResolver($container);
207207
};
208208

209209
$container['CommandBusMiddleware'] = function($container) {
@@ -354,7 +354,7 @@ class UserQueryResponse implements QueryResponse
354354

355355
I'm assuming you're using some kind Service Container. Now it's time to register your QueryHandler.
356356

357-
For instance, in a `Interop\Container` compliant Service Container, we can do this as follows:
357+
For instance, in a `Psr\Container` compliant Service Container, we can do this as follows:
358358

359359
```php
360360
<?php
@@ -382,10 +382,9 @@ For custom strategies, you may write your own implementing the `NilPortugues\Mes
382382

383383
Classes implementing this interface will be resolving the class for the instance required based on the output of the QueryTranslator used.
384384

385-
This package provides an implementation, `NilPortugues\MessageBus\QueryBus\Resolver\InteropContainerResolver`, that expects any Service Container implementing the `Interop\Container` interface.
386-
387-
For custom strategies, such as Symfony Container, you may write your own implementing the `NilPortugues\MessageBus\QueryBus\Contracts\QueryHandlerResolver` interface.
385+
This package provides an implementation, `NilPortugues\MessageBus\QueryBus\Resolver\PsrContainerResolver`, that expects any Service Container implementing the `Psr\Container` interface.
388386

387+
For Symfony 2 and 3 framework users up to version 3.2, you should use Symfony Container: `NilPortugues\MessageBus\QueryBus\Resolver\SymfonyContainerResolver`. For Symfony 3.3 and up use the PSR-11 ContainerResolver class.
389388

390389
#### 2.1.5 - Registering the remaining QueryBus classes
391390

@@ -400,7 +399,7 @@ $container['QueryTranslator'] = function($container) {
400399
};
401400

402401
$container['QueryHandlerResolver'] = function($container) {
403-
return new \NilPortugues\MessageBus\QueryBus\Resolver\InteropContainerResolver($container);
402+
return new \NilPortugues\MessageBus\QueryBus\Resolver\PsrContainerResolver($container);
404403
};
405404

406405
$container['QueryBusMiddleware'] = function($container) {
@@ -627,7 +626,7 @@ final class SendWelcomeEmailHandler implements EventHandler, EventHandlerPriorit
627626

628627
I'm assuming you're using some kind Service Container. Now it's time to register your Event Handlers.
629628

630-
For instance, in a `Interop\Container` compliant Service Container, we can do this as follows:
629+
For instance, in a `Psr\Container` compliant Service Container, we can do this as follows:
631630

632631
```php
633632
<?php
@@ -671,9 +670,9 @@ Its implementation can be found at: `NilPortugues\MessageBus\EventBus\Translator
671670

672671
Classes implementing this interface will be resolving the class for the instance required based on the output of the EventTranslator used.
673672

674-
This package provides an implementation, `NilPortugues\MessageBus\EventBus\Resolver\InteropContainerResolver`, that expects any Service Container implementing the `Interop\Container` interface.
673+
This package provides an implementation, `NilPortugues\MessageBus\EventBus\Resolver\PsrContainerResolver`, that expects any Service Container implementing the `Psr\Container` interface.
675674

676-
For custom strategies, such as Symfony Container, you may write your own implementing the `NilPortugues\MessageBus\EventBus\Contracts\EventHandlerResolver` interface.
675+
For Symfony 2 and 3 framework users up to version 3.2, you should use Symfony Container: `NilPortugues\MessageBus\EventBus\Resolver\SymfonyContainerResolver`. For Symfony 3.3 and up use the PSR-11 ContainerResolver class.
677676

678677
#### 3.1.6 - Registering the remaining EventBus classes
679678

@@ -694,7 +693,7 @@ $container['EventTranslator'] = function($container) {
694693
};
695694

696695
$container['EventHandlerResolver'] = function($container) {
697-
return new \NilPortugues\MessageBus\EventBus\Resolver\InteropContainerResolver($container);
696+
return new \NilPortugues\MessageBus\EventBus\Resolver\PsrContainerResolver($container);
698697
};
699698

700699
$container['EventBusMiddleware'] = function($container) {

composer.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
],
1313
"require": {
1414
"php": ">=7",
15-
"container-interop/container-interop": "1.1.*",
16-
"nilportugues/assert": "1.0.*",
17-
"nilportugues/serializer": "1.1.*",
18-
"psr/log": "1.0.*",
19-
"psr/cache": "1.0.*"
15+
"container-interop/container-interop": "^1.2",
16+
"nilportugues/assert": "^1.0",
17+
"nilportugues/serializer": "^1.2",
18+
"psr/container": "^1.0",
19+
"psr/log": "^1.0",
20+
"psr/cache": "^1.0"
2021
},
2122
"require-dev": {
2223
"phpunit/phpunit": "5.2.*",
23-
"fabpot/php-cs-fixer": "~1.9"
24+
"fabpot/php-cs-fixer": "~1.9",
25+
"symfony/dependency-injection": "~2.7|~3.0"
2426
},
2527
"prefer-stable": true,
2628
"autoload": {

src/CommandBus/Contracts/Command.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\CommandBus\Contracts;
1112

1213
/**

src/CommandBus/Contracts/CommandBusMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\CommandBus\Contracts;
1112

1213
/**

src/CommandBus/Contracts/CommandHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\CommandBus\Contracts;
1112

1213
/**
1314
* Interface CommandHandler.
1415
*/
1516
interface CommandHandler
1617
{
17-
1818
}

src/CommandBus/Resolver/InteropContainerResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\CommandBus\Resolver;
1112

1213
use Interop\Container\ContainerInterface;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Author: Nil Portugués Calderó <[email protected]>
4+
* Date: 23/03/16
5+
* Time: 22:11.
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace NilPortugues\MessageBus\CommandBus\Resolver;
12+
13+
use Psr\Container\ContainerInterface;
14+
15+
/**
16+
* Class PsrContainerResolver.
17+
*/
18+
class PsrContainerResolver extends InteropContainerResolver
19+
{
20+
/**
21+
* PsrContainerResolver constructor.
22+
*
23+
* @param ContainerInterface $container
24+
*/
25+
public function __construct(ContainerInterface $container)
26+
{
27+
$this->container = $container;
28+
}
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Author: Nil Portugués Calderó <[email protected]>
4+
* Date: 15/02/17
5+
* Time: 11:13.
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace NilPortugues\MessageBus\CommandBus\Resolver;
12+
13+
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use InvalidArgumentException;
15+
use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandler;
16+
use NilPortugues\MessageBus\CommandBus\Contracts\CommandHandlerResolver;
17+
18+
/**
19+
* Class SymfonyContainerResolver.
20+
*/
21+
class SymfonyContainerResolver implements CommandHandlerResolver
22+
{
23+
/** @var ContainerInterface */
24+
protected $container;
25+
26+
/**
27+
* SymfonyContainerResolver constructor.
28+
*
29+
* @param ContainerInterface $container
30+
*/
31+
public function __construct(ContainerInterface $container)
32+
{
33+
$this->container = $container;
34+
}
35+
36+
/**
37+
* Given a string to identify the Command Handler, return the instance.
38+
*
39+
* @param string $handler
40+
*
41+
* @return CommandHandler
42+
*/
43+
public function instantiate(string $handler) : CommandHandler
44+
{
45+
if (false === $this->container->has($handler)) {
46+
throw new InvalidArgumentException(
47+
sprintf('Handler %s could not be found. Did you register it?', $handler)
48+
);
49+
}
50+
51+
return $this->container->get($handler);
52+
}
53+
}

src/EventBus/Contracts/Event.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\EventBus\Contracts;
1112

1213
/**

src/EventBus/Contracts/EventBusMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\EventBus\Contracts;
1112

1213
/**

src/EventBus/Contracts/EventHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\EventBus\Contracts;
1112

1213
/**

src/EventBus/Resolver/InteropContainerResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\EventBus\Resolver;
1112

1213
use Interop\Container\ContainerInterface;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Author: Nil Portugués Calderó <[email protected]>
4+
* Date: 23/03/16
5+
* Time: 22:11.
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace NilPortugues\MessageBus\EventBus\Resolver;
12+
13+
use Psr\Container\ContainerInterface;
14+
15+
/**
16+
* Class PsrContainerResolver.
17+
*/
18+
class PsrContainerResolver extends InteropContainerResolver
19+
{
20+
/**
21+
* PsrContainerResolver constructor.
22+
*
23+
* @param ContainerInterface $container
24+
*/
25+
public function __construct(ContainerInterface $container)
26+
{
27+
$this->container = $container;
28+
}
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Author: Nil Portugués Calderó <[email protected]>
4+
* Date: 23/03/16
5+
* Time: 22:11.
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace NilPortugues\MessageBus\EventBus\Resolver;
12+
13+
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use InvalidArgumentException;
15+
use NilPortugues\MessageBus\EventBus\Contracts\EventHandler;
16+
use NilPortugues\MessageBus\EventBus\Contracts\EventHandlerResolver;
17+
18+
/**
19+
* Class SymfonyContainerResolver.
20+
*/
21+
class SymfonyContainerResolver implements EventHandlerResolver
22+
{
23+
/** @var ContainerInterface */
24+
protected $container;
25+
26+
/**
27+
* SymfonyContainerResolver constructor.
28+
*
29+
* @param ContainerInterface $container
30+
*/
31+
public function __construct(ContainerInterface $container)
32+
{
33+
$this->container = $container;
34+
}
35+
36+
/**
37+
* Given a string to identify the Event Handler, return the instance.
38+
*
39+
* @param string $handler
40+
*
41+
* @return EventHandler
42+
*/
43+
public function instantiate(string $handler) : EventHandler
44+
{
45+
if (false === $this->container->has($handler)) {
46+
throw new InvalidArgumentException(
47+
sprintf('Handler %s could not be found. Did you register it?', $handler)
48+
);
49+
}
50+
51+
return $this->container->get($handler);
52+
}
53+
}

src/QueryBus/Contracts/Query.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\QueryBus\Contracts;
1112

1213
/**

src/QueryBus/Contracts/QueryBusMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\QueryBus\Contracts;
1112

1213
/**

src/QueryBus/Contracts/QueryHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\QueryBus\Contracts;
1112

1213
/**

src/QueryBus/Contracts/QueryResponse.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10+
1011
namespace NilPortugues\MessageBus\QueryBus\Contracts;
1112

1213
/**

0 commit comments

Comments
 (0)