-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove some interfaces, make handlers and subscribers a callable
- Loading branch information
1 parent
0c1d054
commit bd6e945
Showing
36 changed files
with
558 additions
and
543 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# How to upgrade from MessageBus `2.x` to `3.0` | ||
|
||
## The `Message`, `Command` and `Event` have been removed | ||
|
||
A message (command, event, etc.) can be any object now, so just remove any type-hint or replace type-hints by more | ||
specific ones. | ||
|
||
The `Message` type-hint has been removed from the following methods: | ||
|
||
- `SimpleBus\Message\Bus\MessageBus::handle` | ||
- `SimpleBus\Message\Bus\Middleware\MessageBusMiddleware::handle` | ||
- `SimpleBus\Message\Handler\Resolver\MessageHandlerResolver::resolve` | ||
- `SimpleBus\Message\Subscriber\Resolver\MessageSubscribersResolver::resolve` | ||
- `SimpleBus\Message\Name\MessageNameResolver::resolve` | ||
|
||
## The `MessageHandler` interface has been removed | ||
|
||
Message handlers can be any callable now, so just remove the ` implements MessageHandler` from your message handler | ||
class definitions. | ||
|
||
## The `MessageSubscriber` interface has been removed | ||
|
||
Message subscribers can be any callable now, so just remove the ` implements MessageSubscriber` from your message | ||
handler class definitions. | ||
|
||
## `MessageHandlerMap` and `MessageSubscriberCollection` have been removed | ||
|
||
Instead, handlers and subscribers can now be any callable you like. Also, they are assumed to be always lazy-loading | ||
(because they should never all be instantiated at the same time). So instead of: | ||
|
||
```php | ||
use SimpleBus\Message\Handler\Map\LazyLoadingMessageHandlerMap; | ||
|
||
$serviceLocator = ...; | ||
|
||
$commandHandlersByCommandName = [ | ||
'Fully\Qualified\Class\Name\Of\Command' => 'command_handler_service_id' | ||
]; | ||
|
||
$commandHandlerMap = new LazyLoadingMessageHandlerMap( | ||
$commandHandlersByCommandName, | ||
$serviceLocator | ||
); | ||
|
||
$commandBus->appendMiddleware( | ||
new DelegatesToMessageHandlerMiddleware( | ||
$commandHandlerResolver | ||
) | ||
); | ||
``` | ||
|
||
you should now define a callable map: | ||
|
||
```php | ||
use SimpleBus\Message\CallableResolver\CallableMap; | ||
use SimpleBus\Message\CallableResolver\ServiceLocatorAwareCallableResolver; | ||
|
||
$serviceLocator = ...; | ||
|
||
$commandHandlersByCommandName = [ | ||
'Fully\Qualified\Class\Name\Of\Command' => ['command_handler_service_id', 'handle'] | ||
]; | ||
|
||
$commandHandlerMap = new CallableMap( | ||
$commandHandlersByCommandName, | ||
new ServiceLocatorAwareCallableResolver($serviceLocator) | ||
); | ||
|
||
$commandBus->appendMiddleware( | ||
new DelegatesToMessageHandlerMiddleware( | ||
$commandHandlerResolver | ||
) | ||
); | ||
``` | ||
|
||
This is the same for |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Message\CallableResolver; | ||
|
||
use Assert\Assertion; | ||
|
||
class CallableCollection | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $callablesByName; | ||
|
||
/** | ||
* @var CallableResolver | ||
*/ | ||
private $callableResolver; | ||
|
||
public function __construct( | ||
array $callablesByName, | ||
CallableResolver $callableResolver | ||
) { | ||
Assertion::allIsArray($callablesByName, 'You need to provide arrays of callables, indexed by name'); | ||
|
||
$this->callablesByName = $callablesByName; | ||
$this->callableResolver = $callableResolver; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return callable[] | ||
*/ | ||
public function filter($name) | ||
{ | ||
if (!array_key_exists($name, $this->callablesByName)) { | ||
return []; | ||
} | ||
|
||
$callables = $this->callablesByName[$name]; | ||
|
||
return array_map([$this->callableResolver, 'resolve'], $callables); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Message\CallableResolver; | ||
|
||
use SimpleBus\Message\CallableResolver\Exception\UndefinedCallable; | ||
|
||
class CallableMap | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $callablesByName; | ||
|
||
/** | ||
* @var CallableResolver | ||
*/ | ||
private $callableResolver; | ||
|
||
public function __construct( | ||
array $callablesByName, | ||
CallableResolver $callableResolver | ||
) { | ||
$this->callablesByName = $callablesByName; | ||
$this->callableResolver = $callableResolver; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @return callable | ||
*/ | ||
public function get($name) | ||
{ | ||
if (!array_key_exists($name, $this->callablesByName)) { | ||
throw new UndefinedCallable( | ||
sprintf( | ||
'Could not find a callable for name "%s"', | ||
$name | ||
) | ||
); | ||
} | ||
|
||
$callable = $this->callablesByName[$name]; | ||
|
||
return $this->callableResolver->resolve($callable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Message\CallableResolver; | ||
|
||
use SimpleBus\Message\CallableResolver\Exception\CouldNotResolveCallable; | ||
|
||
interface CallableResolver | ||
{ | ||
/** | ||
* @param $maybeCallable | ||
* @throws CouldNotResolveCallable | ||
* @return callable | ||
*/ | ||
public function resolve($maybeCallable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Message\CallableResolver\Exception; | ||
|
||
class CouldNotResolveCallable extends \LogicException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Message\CallableResolver\Exception; | ||
|
||
class UndefinedCallable extends \LogicException | ||
{ | ||
} |
48 changes: 48 additions & 0 deletions
48
src/CallableResolver/ServiceLocatorAwareCallableResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace SimpleBus\Message\CallableResolver; | ||
|
||
use SimpleBus\Message\CallableResolver\Exception\CouldNotResolveCallable; | ||
|
||
class ServiceLocatorAwareCallableResolver implements CallableResolver | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $serviceLocator; | ||
|
||
public function __construct(callable $serviceLocator) | ||
{ | ||
$this->serviceLocator = $serviceLocator; | ||
} | ||
|
||
/** | ||
* @param $maybeCallable | ||
* @return callable | ||
*/ | ||
public function resolve($maybeCallable) | ||
{ | ||
if (is_callable($maybeCallable)) { | ||
return $maybeCallable; | ||
} | ||
|
||
if (is_string($maybeCallable)) { | ||
// a string can be converted to an object, which may then be a callable | ||
return $this->resolve($this->loadService($maybeCallable)); | ||
} | ||
|
||
if (is_array($maybeCallable) && count($maybeCallable) === 2) { | ||
list($serviceId, $method) = $maybeCallable; | ||
if (is_string($serviceId)) { | ||
return $this->resolve([$this->loadService($serviceId), $method]); | ||
} | ||
} | ||
|
||
throw new CouldNotResolveCallable(); | ||
} | ||
|
||
private function loadService($serviceId) | ||
{ | ||
return call_user_func($this->serviceLocator, $serviceId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.