Skip to content

[Messenger] Non-transportable envelope items #27245

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\EnvelopeItemInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\TransportableEnvelopeItemInterface;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
Expand All @@ -40,6 +42,11 @@ public function handle($message, callable $next)
return $next($message);
}

$transportableItems = array_filter($envelope->all(), function (EnvelopeItemInterface $item): bool {
return $item instanceof TransportableEnvelopeItemInterface;
});
$envelope = new Envelope($envelope->getMessage(), $transportableItems);

if (!empty($senders = $this->senderLocator->getSendersForMessage($envelope->getMessage()))) {
foreach ($senders as $sender) {
if (null === $sender) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,4 @@
*/
final class ReceivedMessage implements EnvelopeItemInterface
{
public function serialize()
{
return '';
}

public function unserialize($serialized)
{
// noop
}
}
5 changes: 3 additions & 2 deletions src/Symfony/Component/Messenger/EnvelopeItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

/**
* An envelope item related to a message.
* This item must be serializable for transport.
*
* @see TransportableEnvelopeItemInterface for an envelope item that could be transported.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @experimental in 4.1
*/
interface EnvelopeItemInterface extends \Serializable
interface EnvelopeItemInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

namespace Symfony\Component\Messenger\Middleware\Configuration;

use Symfony\Component\Messenger\EnvelopeItemInterface;
use Symfony\Component\Messenger\TransportableEnvelopeItemInterface;
use Symfony\Component\Validator\Constraints\GroupSequence;

/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @experimental in 4.1
*/
final class ValidationConfiguration implements EnvelopeItemInterface
final class ValidationConfiguration implements TransportableEnvelopeItemInterface
{
private $groups;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeItemInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\SenderInterface;
use Symfony\Component\Messenger\TransportableEnvelopeItemInterface;

class SendMessageMiddlewareTest extends TestCase
{
Expand All @@ -37,6 +39,27 @@ public function testItSendsTheMessageToAssignedSender()
$middleware->handle($message, $next);
}

public function testItSendsEnvelopeWithTransportableItemsOnly()
{
$envelope = Envelope::wrap(new DummyMessage('Hey'))
->with(new TransportableItem())
->with(new NonTransportableItem())
;
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));

$middleware = new SendMessageMiddleware(new InMemorySenderLocator(array(
$sender,
)));

$sender->expects($this->once())->method('send')->with(Envelope::wrap(new DummyMessage('Hey'))
->with(new TransportableItem())
);
$next->expects($this->never())->method($this->anything());

$middleware->handle($envelope, $next);
}

public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage()
{
$envelope = Envelope::wrap(new DummyMessage('Hey'));
Expand Down Expand Up @@ -114,3 +137,20 @@ public function getSendersForMessage($message): array
return $this->senders;
}
}

class TransportableItem implements TransportableEnvelopeItemInterface
{
public function serialize()
{
// no op
}

public function unserialize($serialized)
{
// no op
}
}

class NonTransportableItem implements EnvelopeItemInterface
{
}
15 changes: 0 additions & 15 deletions src/Symfony/Component/Messenger/Tests/Fixtures/AnEnvelopeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,4 @@

class AnEnvelopeItem implements EnvelopeItemInterface
{
/**
* {@inheritdoc}
*/
public function serialize()
{
return '';
}

/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
// noop
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public function encode(Envelope $envelope): array
}

$headers = array('type' => \get_class($envelope->getMessage()));
if ($configurations = $envelope->all()) {
$headers['X-Message-Envelope-Items'] = serialize($configurations);
if ($items = $envelope->all()) {
$headers['X-Message-Envelope-Items'] = serialize($items);
}

return array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

namespace Symfony\Component\Messenger\Transport\Serialization;

use Symfony\Component\Messenger\EnvelopeItemInterface;
use Symfony\Component\Messenger\TransportableEnvelopeItemInterface;

/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @experimental in 4.1
*/
final class SerializerConfiguration implements EnvelopeItemInterface
final class SerializerConfiguration implements TransportableEnvelopeItemInterface
{
private $context;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\Messenger;

/**
* An envelope item that could be transported.
*
* @author Konstantin Myakshin <molodchick@gmail.com>
*
* @experimental in 4.1
*/
interface TransportableEnvelopeItemInterface extends EnvelopeItemInterface, \Serializable
{
}