-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Happyr/sfevent
Make sure you can extend the consumer by using symfony event dispatcher
- Loading branch information
Showing
8 changed files
with
188 additions
and
22 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,60 @@ | ||
<?php | ||
|
||
namespace Happyr\Mq2phpBundle\Consumer; | ||
|
||
use Happyr\Mq2phpBundle\Event\PreHandleMessage; | ||
use SimpleBus\Serialization\Envelope\Serializer\MessageInEnvelopSerializer; | ||
use SimpleBus\Message\Bus\MessageBus; | ||
use SimpleBus\Asynchronous\Consumer\SerializedEnvelopeConsumer as SimpleBusSerializedEnvelopeConsumer; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
/** | ||
* Use this consumer to easily implement an asynchronous message consumer. | ||
*/ | ||
class ExtendableEnvelopeConsumer implements SimpleBusSerializedEnvelopeConsumer | ||
{ | ||
/** | ||
* @var MessageInEnvelopSerializer | ||
*/ | ||
private $messageInEnvelopeSerializer; | ||
|
||
/** | ||
* @var MessageBus | ||
*/ | ||
private $messageBus; | ||
|
||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
private $dispathcer; | ||
|
||
/** | ||
* @param MessageInEnvelopSerializer $messageInEnvelopeSerializer | ||
* @param MessageBus $messageBus | ||
* @param EventDispatcherInterface $dispathcer | ||
*/ | ||
public function __construct( | ||
MessageInEnvelopSerializer $messageInEnvelopeSerializer, | ||
MessageBus $messageBus, | ||
EventDispatcherInterface $dispathcer | ||
) { | ||
$this->messageInEnvelopeSerializer = $messageInEnvelopeSerializer; | ||
$this->messageBus = $messageBus; | ||
$this->dispathcer = $dispathcer; | ||
} | ||
|
||
/** | ||
* @param string $serializedEnvelope | ||
*/ | ||
public function consume($serializedEnvelope) | ||
{ | ||
// Unserialize | ||
$envelope = $this->messageInEnvelopeSerializer->unwrapAndDeserialize($serializedEnvelope); | ||
|
||
// Tell the world | ||
$this->dispathcer->dispatch(PreHandleMessage::NAME, new PreHandleMessage($envelope)); | ||
|
||
// Handle the message | ||
$this->messageBus->handle($envelope->message()); | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace Happyr\Mq2phpBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Register the message bus argument to our consumer. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class RegisterConsumers implements CompilerPassInterface | ||
{ | ||
/** | ||
* @param ContainerBuilder $container | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$this->replaceArgumentWithReference($container, 'happyr.mq2php.extendable_command_envelope_consumer', 'simple_bus.asynchronous.command_bus'); | ||
$this->replaceArgumentWithReference($container, 'happyr.mq2php.extendable_event_envelope_consumer', 'simple_bus.asynchronous.event_bus'); | ||
} | ||
|
||
/** | ||
* @param ContainerBuilder $container | ||
* @param string $serviceId | ||
* @param string $referenceId | ||
*/ | ||
private function replaceArgumentWithReference(ContainerBuilder $container, $serviceId, $referenceId) | ||
{ | ||
if (!$container->hasDefinition($serviceId)) { | ||
return; | ||
} | ||
|
||
if (!$container->hasDefinition($referenceId)) { | ||
$container->removeDefinition($serviceId); | ||
} | ||
|
||
$container->getDefinition($serviceId)->replaceArgument(1, new Reference($referenceId)); | ||
} | ||
} |
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 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,32 @@ | ||
<?php | ||
|
||
namespace Happyr\Mq2phpBundle\Event; | ||
|
||
use SimpleBus\Serialization\Envelope\Envelope; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class PreHandleMessage extends Event | ||
{ | ||
const NAME = 'happyr.mq2php.pre_handle_message'; | ||
|
||
/** | ||
* @var Envelope | ||
*/ | ||
private $envelope; | ||
|
||
/** | ||
* @param Envelope $envelope | ||
*/ | ||
public function __construct(Envelope $envelope) | ||
{ | ||
$this->envelope = $envelope; | ||
} | ||
|
||
/** | ||
* @return Envelope | ||
*/ | ||
public function getEnvelope() | ||
{ | ||
return $this->envelope; | ||
} | ||
} |
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 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 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 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