diff --git a/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php b/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php index 9aabe794a11ab..eeba363cc87c2 100644 --- a/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php +++ b/src/Symfony/Component/Messenger/Asynchronous/Middleware/SendMessageMiddleware.php @@ -13,14 +13,12 @@ use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface; use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; -use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\EnvelopeAwareInterface; use Symfony\Component\Messenger\Middleware\MiddlewareInterface; /** * @author Samuel Roze */ -class SendMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface +class SendMessageMiddleware implements MiddlewareInterface { private $senderLocator; @@ -34,19 +32,17 @@ public function __construct(SenderLocatorInterface $senderLocator) */ public function handle($message, callable $next) { - $envelope = Envelope::wrap($message); - if ($envelope->get(ReceivedMessage::class)) { - // It's a received message. Do not send it back: - return $next($message); + if ($message instanceof ReceivedMessage) { + return $next($message->getMessage()); } - if (!empty($senders = $this->senderLocator->getSendersForMessage($envelope->getMessage()))) { + if (!empty($senders = $this->senderLocator->getSendersForMessage($message))) { foreach ($senders as $sender) { if (null === $sender) { continue; } - $sender->send($envelope); + $sender->send($message); } if (!\in_array(null, $senders, true)) { diff --git a/src/Symfony/Component/Messenger/Asynchronous/Transport/ReceivedMessage.php b/src/Symfony/Component/Messenger/Asynchronous/Transport/ReceivedMessage.php index c713a589ad53c..1b1298da63d32 100644 --- a/src/Symfony/Component/Messenger/Asynchronous/Transport/ReceivedMessage.php +++ b/src/Symfony/Component/Messenger/Asynchronous/Transport/ReceivedMessage.php @@ -12,26 +12,26 @@ namespace Symfony\Component\Messenger\Asynchronous\Transport; use Symfony\Component\Messenger\Asynchronous\Middleware\SendMessageMiddleware; -use Symfony\Component\Messenger\EnvelopeItemInterface; /** - * Marker config for a received message. - * This is mainly used by the `SendMessageMiddleware` middleware to identify + * Wraps a received message. This is mainly used by the `SendMessageMiddleware` middleware to identify * a message should not be sent if it was just received. * * @see SendMessageMiddleware * * @author Samuel Roze */ -final class ReceivedMessage implements EnvelopeItemInterface +final class ReceivedMessage { - public function serialize() + private $message; + + public function __construct($message) { - return ''; + $this->message = $message; } - public function unserialize($serialized) + public function getMessage() { - // noop + return $this->message; } } diff --git a/src/Symfony/Component/Messenger/Asynchronous/Transport/WrapIntoReceivedMessage.php b/src/Symfony/Component/Messenger/Asynchronous/Transport/WrapIntoReceivedMessage.php index 4b10d6445fc5f..8af87a2a4514b 100644 --- a/src/Symfony/Component/Messenger/Asynchronous/Transport/WrapIntoReceivedMessage.php +++ b/src/Symfony/Component/Messenger/Asynchronous/Transport/WrapIntoReceivedMessage.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Messenger\Asynchronous\Transport; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\ReceiverInterface; /** @@ -28,12 +27,12 @@ public function __construct(ReceiverInterface $decoratedConsumer) public function receive(callable $handler): void { - $this->decoratedReceiver->receive(function (?Envelope $envelope) use ($handler) { - if (null !== $envelope) { - $envelope = $envelope->with(new ReceivedMessage()); + $this->decoratedReceiver->receive(function ($message) use ($handler) { + if (null !== $message) { + $message = new ReceivedMessage($message); } - $handler($envelope); + $handler($message); }); } diff --git a/src/Symfony/Component/Messenger/Envelope.php b/src/Symfony/Component/Messenger/Envelope.php deleted file mode 100644 index 14778593c4baf..0000000000000 --- a/src/Symfony/Component/Messenger/Envelope.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger; - -/** - * A message wrapped in an envelope with items (configurations, markers, ...). - * - * @author Maxime Steinhausser - * - * @experimental in 4.1 - */ -final class Envelope -{ - private $items = array(); - private $message; - - /** - * @param object $message - * @param EnvelopeItemInterface[] $items - */ - public function __construct($message, array $items = array()) - { - $this->message = $message; - foreach ($items as $item) { - $this->items[\get_class($item)] = $item; - } - } - - /** - * Wrap a message into an envelope if not already wrapped. - * - * @param Envelope|object $message - */ - public static function wrap($message): self - { - return $message instanceof self ? $message : new self($message); - } - - /** - * @return Envelope a new Envelope instance with additional item - */ - public function with(EnvelopeItemInterface $item): self - { - $cloned = clone $this; - - $cloned->items[\get_class($item)] = $item; - - return $cloned; - } - - public function get(string $itemFqcn): ?EnvelopeItemInterface - { - return $this->items[$itemFqcn] ?? null; - } - - /** - * @return EnvelopeItemInterface[] indexed by fqcn - */ - public function all(): array - { - return $this->items; - } - - /** - * @return object The original message contained in the envelope - */ - public function getMessage() - { - return $this->message; - } -} diff --git a/src/Symfony/Component/Messenger/EnvelopeAwareInterface.php b/src/Symfony/Component/Messenger/EnvelopeAwareInterface.php deleted file mode 100644 index c19bc8436286a..0000000000000 --- a/src/Symfony/Component/Messenger/EnvelopeAwareInterface.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger; - -/** - * A Messenger protagonist aware of the message envelope and its content. - * - * @author Maxime Steinhausser - * - * @experimental in 4.1 - */ -interface EnvelopeAwareInterface -{ -} diff --git a/src/Symfony/Component/Messenger/EnvelopeItemInterface.php b/src/Symfony/Component/Messenger/EnvelopeItemInterface.php deleted file mode 100644 index 2561a127546c0..0000000000000 --- a/src/Symfony/Component/Messenger/EnvelopeItemInterface.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger; - -/** - * An envelope item related to a message. - * This item must be serializable for transport. - * - * @author Maxime Steinhausser - * - * @experimental in 4.1 - */ -interface EnvelopeItemInterface extends \Serializable -{ -} diff --git a/src/Symfony/Component/Messenger/MessageBus.php b/src/Symfony/Component/Messenger/MessageBus.php index 869c9806e4189..904eafcdd94f5 100644 --- a/src/Symfony/Component/Messenger/MessageBus.php +++ b/src/Symfony/Component/Messenger/MessageBus.php @@ -55,12 +55,6 @@ private function callableForNextMiddleware(int $index): callable $middleware = $this->indexedMiddlewareHandlers[$index]; return function ($message) use ($middleware, $index) { - $message = Envelope::wrap($message); - if (!$middleware instanceof EnvelopeAwareInterface) { - // Do not provide the envelope if the middleware cannot read it: - $message = $message->getMessage(); - } - return $middleware->handle($message, $this->callableForNextMiddleware($index + 1)); }; } diff --git a/src/Symfony/Component/Messenger/MessageBusInterface.php b/src/Symfony/Component/Messenger/MessageBusInterface.php index 8c2b91d1af3ae..1d441ea568ff7 100644 --- a/src/Symfony/Component/Messenger/MessageBusInterface.php +++ b/src/Symfony/Component/Messenger/MessageBusInterface.php @@ -23,7 +23,7 @@ interface MessageBusInterface * * The bus can return a value coming from handlers, but is not required to do so. * - * @param object|Envelope $message The message or the message pre-wrapped in an envelope + * @param object $message * * @return mixed */ diff --git a/src/Symfony/Component/Messenger/Middleware/Configuration/ValidationConfiguration.php b/src/Symfony/Component/Messenger/Middleware/Configuration/ValidationConfiguration.php deleted file mode 100644 index 1b6180857b31d..0000000000000 --- a/src/Symfony/Component/Messenger/Middleware/Configuration/ValidationConfiguration.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger\Middleware\Configuration; - -use Symfony\Component\Messenger\EnvelopeItemInterface; -use Symfony\Component\Validator\Constraints\GroupSequence; - -/** - * @author Maxime Steinhausser - * - * @experimental in 4.1 - */ -final class ValidationConfiguration implements EnvelopeItemInterface -{ - private $groups; - - /** - * @param string[]|GroupSequence $groups - */ - public function __construct($groups) - { - $this->groups = $groups; - } - - public function getGroups() - { - return $this->groups; - } - - public function serialize() - { - $isGroupSequence = $this->groups instanceof GroupSequence; - - return serialize(array( - 'groups' => $isGroupSequence ? $this->groups->groups : $this->groups, - 'is_group_sequence' => $isGroupSequence, - )); - } - - public function unserialize($serialized) - { - list( - 'groups' => $groups, - 'is_group_sequence' => $isGroupSequence - ) = unserialize($serialized, array('allowed_classes' => false)); - - $this->__construct($isGroupSequence ? new GroupSequence($groups) : $groups); - } -} diff --git a/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php b/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php index ebaf8525c0407..4de6e42575805 100644 --- a/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php +++ b/src/Symfony/Component/Messenger/Middleware/LoggingMiddleware.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Messenger\Middleware; +use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; use Psr\Log\LoggerInterface; /** @@ -50,6 +51,10 @@ public function handle($message, callable $next) private function createContext($message): array { + if ($message instanceof ReceivedMessage) { + $message = $message->getMessage(); + } + return array( 'message' => $message, 'class' => \get_class($message), diff --git a/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php b/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php index e588d9256bd32..3b168367cdd33 100644 --- a/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php +++ b/src/Symfony/Component/Messenger/Middleware/ValidationMiddleware.php @@ -11,16 +11,13 @@ namespace Symfony\Component\Messenger\Middleware; -use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\EnvelopeAwareInterface; use Symfony\Component\Messenger\Exception\ValidationFailedException; -use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; use Symfony\Component\Validator\Validator\ValidatorInterface; /** * @author Tobias Nyholm */ -class ValidationMiddleware implements MiddlewareInterface, EnvelopeAwareInterface +class ValidationMiddleware implements MiddlewareInterface { private $validator; @@ -31,17 +28,9 @@ public function __construct(ValidatorInterface $validator) public function handle($message, callable $next) { - $envelope = Envelope::wrap($message); - $subject = $envelope->getMessage(); - $groups = null; - /** @var ValidationConfiguration|null $validationConfig */ - if ($validationConfig = $envelope->get(ValidationConfiguration::class)) { - $groups = $validationConfig->getGroups(); - } - - $violations = $this->validator->validate($subject, null, $groups); + $violations = $this->validator->validate($message); if (\count($violations)) { - throw new ValidationFailedException($subject, $violations); + throw new ValidationFailedException($message, $violations); } return $next($message); diff --git a/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php index aa22741c397b6..6398aff361684 100644 --- a/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php @@ -15,7 +15,6 @@ use Symfony\Component\Messenger\Asynchronous\Middleware\SendMessageMiddleware; use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface; use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; use Symfony\Component\Messenger\Transport\SenderInterface; @@ -31,28 +30,12 @@ public function testItSendsTheMessageToAssignedSender() $sender, ))); - $sender->expects($this->once())->method('send')->with(Envelope::wrap($message)); + $sender->expects($this->once())->method('send')->with($message); $next->expects($this->never())->method($this->anything()); $middleware->handle($message, $next); } - public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage() - { - $envelope = Envelope::wrap(new DummyMessage('Hey')); - $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); - $next->expects($this->never())->method($this->anything()); - - $middleware->handle($envelope, $next); - } - public function testItAlsoCallsTheNextMiddlewareIfASenderIsNull() { $message = new DummyMessage('Hey'); @@ -64,7 +47,7 @@ public function testItAlsoCallsTheNextMiddlewareIfASenderIsNull() null, ))); - $sender->expects($this->once())->method('send')->with(Envelope::wrap($message)); + $sender->expects($this->once())->method('send')->with($message); $next->expects($this->once())->method($this->anything()); $middleware->handle($message, $next); @@ -84,7 +67,8 @@ public function testItCallsTheNextMiddlewareWhenNoSenderForThisMessage() public function testItSkipsReceivedMessages() { - $envelope = Envelope::wrap(new DummyMessage('Hey'))->with(new ReceivedMessage()); + $innerMessage = new DummyMessage('Hey'); + $message = new ReceivedMessage($innerMessage); $sender = $this->getMockBuilder(SenderInterface::class)->getMock(); $next = $this->createPartialMock(\stdClass::class, array('__invoke')); @@ -94,9 +78,9 @@ public function testItSkipsReceivedMessages() ))); $sender->expects($this->never())->method('send'); - $next->expects($this->once())->method('__invoke')->with($envelope); + $next->expects($this->once())->method('__invoke')->with($innerMessage); - $middleware->handle($envelope, $next); + $middleware->handle($message, $next); } } diff --git a/src/Symfony/Component/Messenger/Tests/Asynchronous/Transport/Serialization/SerializerConfigurationTest.php b/src/Symfony/Component/Messenger/Tests/Asynchronous/Transport/Serialization/SerializerConfigurationTest.php deleted file mode 100644 index 6ebcc8ddfa0ca..0000000000000 --- a/src/Symfony/Component/Messenger/Tests/Asynchronous/Transport/Serialization/SerializerConfigurationTest.php +++ /dev/null @@ -1,30 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger\Tests\Asynchronous\Serialization; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration; -use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; - -/** - * @author Maxime Steinhausser - */ -class SerializerConfigurationTest extends TestCase -{ - public function testSerialiazable() - { - $config = new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('Default', 'Extra'))); - - $this->assertTrue(is_subclass_of(SerializerConfiguration::class, \Serializable::class, true)); - $this->assertEquals($config, unserialize(serialize($config))); - } -} diff --git a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php index 91e941b82a31b..b230a9c6df60a 100644 --- a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php +++ b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php @@ -16,7 +16,6 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ServiceLocator; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver; use Symfony\Component\Messenger\Transport\AmqpExt\AmqpSender; use Symfony\Component\Messenger\Handler\Locator\ContainerHandlerLocator; @@ -322,7 +321,7 @@ class DummyReceiver implements ReceiverInterface public function receive(callable $handler): void { for ($i = 0; $i < 3; ++$i) { - $handler(Envelope::wrap(new DummyMessage("Dummy $i"))); + $handler(new DummyMessage("Dummy $i")); } } diff --git a/src/Symfony/Component/Messenger/Tests/EnvelopeTest.php b/src/Symfony/Component/Messenger/Tests/EnvelopeTest.php deleted file mode 100644 index 053275b6e714c..0000000000000 --- a/src/Symfony/Component/Messenger/Tests/EnvelopeTest.php +++ /dev/null @@ -1,82 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger\Tests; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; -use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\EnvelopeAwareInterface; -use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; -use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; - -/** - * @author Maxime Steinhausser - */ -class EnvelopeTest extends TestCase -{ - public function testConstruct() - { - $envelope = new Envelope($dummy = new DummyMessage('dummy'), array( - $receivedConfig = new ReceivedMessage(), - )); - - $this->assertSame($dummy, $envelope->getMessage()); - $this->assertArrayHasKey(ReceivedMessage::class, $configs = $envelope->all()); - $this->assertSame($receivedConfig, $configs[ReceivedMessage::class]); - } - - public function testWrap() - { - $first = Envelope::wrap($dummy = new DummyMessage('dummy')); - - $this->assertInstanceOf(Envelope::class, $first); - $this->assertSame($dummy, $first->getMessage()); - - $envelope = Envelope::wrap($first); - $this->assertSame($first, $envelope); - } - - public function testWithReturnsNewInstance() - { - $envelope = Envelope::wrap($dummy = new DummyMessage('dummy')); - - $this->assertNotSame($envelope, $envelope->with(new ReceivedMessage())); - } - - public function testGet() - { - $envelope = Envelope::wrap($dummy = new DummyMessage('dummy')) - ->with($config = new ReceivedMessage()) - ; - - $this->assertSame($config, $envelope->get(ReceivedMessage::class)); - $this->assertNull($envelope->get(ValidationConfiguration::class)); - } - - public function testAll() - { - $envelope = Envelope::wrap($dummy = new DummyMessage('dummy')) - ->with($receivedConfig = new ReceivedMessage()) - ->with($validationConfig = new ValidationConfiguration(array('foo'))) - ; - - $configs = $envelope->all(); - $this->assertArrayHasKey(ReceivedMessage::class, $configs); - $this->assertSame($receivedConfig, $configs[ReceivedMessage::class]); - $this->assertArrayHasKey(ValidationConfiguration::class, $configs); - $this->assertSame($validationConfig, $configs[ValidationConfiguration::class]); - } -} - -class FooConfigurationConsumer implements EnvelopeAwareInterface -{ -} diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/Configuration/ValidationConfigurationTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/Configuration/ValidationConfigurationTest.php deleted file mode 100644 index 6fd6962994f4f..0000000000000 --- a/src/Symfony/Component/Messenger/Tests/Middleware/Configuration/ValidationConfigurationTest.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger\Tests\Middleware\Configuration; - -use PHPUnit\Framework\TestCase; -use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; -use Symfony\Component\Validator\Constraints\GroupSequence; - -/** - * @author Maxime Steinhausser - */ -class ValidationConfigurationTest extends TestCase -{ - public function testConfig() - { - $config = new ValidationConfiguration($groups = array('Default', 'Extra')); - $this->assertSame($groups, $config->getGroups()); - - $config = new ValidationConfiguration($groups = new GroupSequence(array('Default', 'Then'))); - $this->assertSame($groups, $config->getGroups()); - } - - public function testSerialiazable() - { - $this->assertTrue(is_subclass_of(ValidationConfiguration::class, \Serializable::class, true)); - $this->assertEquals($config = new ValidationConfiguration(array('Default', 'Extra')), unserialize(serialize($config))); - $this->assertEquals($config = new ValidationConfiguration(new GroupSequence(array('Default', 'Then'))), unserialize(serialize($config))); - } -} diff --git a/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php b/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php index b2a44b5fa6723..2bd2ba8af22e4 100644 --- a/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php +++ b/src/Symfony/Component/Messenger/Tests/Middleware/ValidationMiddlewareTest.php @@ -12,8 +12,6 @@ namespace Symfony\Component\Messenger\Tests\Middleware; use PHPUnit\Framework\TestCase; -use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; use Symfony\Component\Messenger\Middleware\ValidationMiddleware; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; use Symfony\Component\Validator\ConstraintViolationListInterface; @@ -45,30 +43,6 @@ public function testValidateAndNextMiddleware() $this->assertSame('Hello', $result); } - public function testValidateWithConfigurationAndNextMiddleware() - { - $envelope = Envelope::wrap($message = new DummyMessage('Hey'))->with(new ValidationConfiguration($groups = array('Default', 'Extra'))); - - $validator = $this->createMock(ValidatorInterface::class); - $validator - ->expects($this->once()) - ->method('validate') - ->with($message, null, $groups) - ->willReturn($this->createMock(ConstraintViolationListInterface::class)) - ; - $next = $this->createPartialMock(\stdClass::class, array('__invoke')); - $next - ->expects($this->once()) - ->method('__invoke') - ->with($envelope) - ->willReturn('Hello') - ; - - $result = (new ValidationMiddleware($validator))->handle($envelope, $next); - - $this->assertSame('Hello', $result); - } - /** * @expectedException \Symfony\Component\Messenger\Exception\ValidationFailedException * @expectedExceptionMessage Message of type "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage" failed validation. diff --git a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php index c01ffdadb7f0a..5ce9c457e9841 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpExtIntegrationTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Messenger\Tests\Transport\AmqpExt; use PHPUnit\Framework\TestCase; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver; use Symfony\Component\Messenger\Transport\AmqpExt\AmqpSender; use Symfony\Component\Messenger\Transport\AmqpExt\Connection; @@ -51,12 +50,12 @@ public function testItSendsAndReceivesMessages() $sender = new AmqpSender($serializer, $connection); $receiver = new AmqpReceiver($serializer, $connection); - $sender->send($first = Envelope::wrap(new DummyMessage('First'))); - $sender->send($second = Envelope::wrap(new DummyMessage('Second'))); + $sender->send($firstMessage = new DummyMessage('First')); + $sender->send($secondMessage = new DummyMessage('Second')); $receivedMessages = 0; - $receiver->receive(function (?Envelope $envelope) use ($receiver, &$receivedMessages, $first, $second) { - $this->assertEquals(0 == $receivedMessages ? $first : $second, $envelope); + $receiver->receive(function ($message) use ($receiver, &$receivedMessages, $firstMessage, $secondMessage) { + $this->assertEquals(0 == $receivedMessages ? $firstMessage : $secondMessage, $message); if (2 === ++$receivedMessages) { $receiver->stop(); @@ -75,7 +74,7 @@ public function testItReceivesSignals() $connection->queue()->purge(); $sender = new AmqpSender($serializer, $connection); - $sender->send(Envelope::wrap(new DummyMessage('Hello'))); + $sender->send(new DummyMessage('Hello')); $amqpReadTimeout = 30; $dsn = getenv('MESSENGER_AMQP_DSN').'?read_timeout='.$amqpReadTimeout; @@ -99,15 +98,7 @@ public function testItReceivesSignals() $this->assertFalse($process->isRunning()); $this->assertLessThan($amqpReadTimeout, microtime(true) - $signalTime); - $this->assertSame($expectedOutput.<<<'TXT' -Get envelope with message: Symfony\Component\Messenger\Tests\Fixtures\DummyMessage -with items: [ - "Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage" -] -Done. - -TXT - , $process->getOutput()); + $this->assertEquals($expectedOutput."Get message: Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage\nDone.\n", $process->getOutput()); } /** @@ -123,11 +114,12 @@ public function testItSupportsTimeoutAndTicksNullMessagesToTheHandler() $connection->setup(); $connection->queue()->purge(); + $sender = new AmqpSender($serializer, $connection); $receiver = new AmqpReceiver($serializer, $connection); $receivedMessages = 0; - $receiver->receive(function (?Envelope $envelope) use ($receiver, &$receivedMessages) { - $this->assertNull($envelope); + $receiver->receive(function ($message) use ($receiver, &$receivedMessages) { + $this->assertNull($message); if (2 === ++$receivedMessages) { $receiver->stop(); diff --git a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpReceiverTest.php b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpReceiverTest.php index 4d0b779e3b3f4..2045764216ca3 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpReceiverTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpReceiverTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Messenger\Tests\Transport\AmqpExt; use PHPUnit\Framework\TestCase; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver; use Symfony\Component\Messenger\Transport\AmqpExt\Connection; use Symfony\Component\Messenger\Transport\AmqpExt\Exception\RejectMessageExceptionInterface; @@ -45,8 +44,8 @@ public function testItSendTheDecodedMessageToTheHandlerAndAcknowledgeIt() $connection->expects($this->once())->method('ack')->with($envelope); $receiver = new AmqpReceiver($serializer, $connection); - $receiver->receive(function (?Envelope $envelope) use ($receiver) { - $this->assertEquals(new DummyMessage('Hi'), $envelope->getMessage()); + $receiver->receive(function ($message) use ($receiver) { + $this->assertEquals(new DummyMessage('Hi'), $message); $receiver->stop(); }); } diff --git a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php index 8848b022f6aad..54859a58acf1b 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/AmqpSenderTest.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Messenger\Tests\Transport\AmqpExt; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\AmqpExt\AmqpSender; use Symfony\Component\Messenger\Transport\AmqpExt\Connection; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; @@ -25,16 +24,16 @@ class AmqpSenderTest extends TestCase { public function testItSendsTheEncodedMessage() { - $envelope = Envelope::wrap(new DummyMessage('Oy')); + $message = new DummyMessage('Oy'); $encoded = array('body' => '...', 'headers' => array('type' => DummyMessage::class)); $encoder = $this->getMockBuilder(EncoderInterface::class)->getMock(); - $encoder->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded); + $encoder->method('encode')->with($message)->willReturnOnConsecutiveCalls($encoded); $connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock(); $connection->expects($this->once())->method('publish')->with($encoded['body'], $encoded['headers']); $sender = new AmqpSender($encoder, $connection); - $sender->send($envelope); + $sender->send($message); } } diff --git a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/Fixtures/long_receiver.php b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/Fixtures/long_receiver.php index b7231a5d47fac..2115b1a81dc23 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/Fixtures/long_receiver.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/AmqpExt/Fixtures/long_receiver.php @@ -12,9 +12,10 @@ require_once $autoload; -use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceiver; +use Symfony\Component\Messenger\Transport\AmqpExt\AmqpSender; use Symfony\Component\Messenger\Transport\AmqpExt\Connection; +use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Transport\Serialization\Serializer; use Symfony\Component\Messenger\Worker; use Symfony\Component\Serializer as SerializerComponent; @@ -26,14 +27,13 @@ ); $connection = Connection::fromDsn(getenv('DSN')); +$sender = new AmqpSender($serializer, $connection); $receiver = new AmqpReceiver($serializer, $connection); $worker = new Worker($receiver, new class() implements MessageBusInterface { - public function dispatch($envelope) + public function dispatch($message) { - echo 'Get envelope with message: '.get_class($envelope->getMessage())."\n"; - echo sprintf("with items: %s\n", json_encode(array_keys($envelope->all()), JSON_PRETTY_PRINT)); - + echo 'Get message: '.get_class($message)."\n"; sleep(30); echo "Done.\n"; } diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php index a34be3bfc2ce5..f317c6a5bfe94 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiverTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Tests\Fixtures\CallbackReceiver; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; use Symfony\Component\Messenger\Transport\Enhancers\StopWhenMemoryUsageIsExceededReceiver; @@ -26,7 +25,7 @@ class StopWhenMemoryUsageIsExceededReceiverTest extends TestCase public function testReceiverStopsWhenMemoryLimitExceeded(int $memoryUsage, int $memoryLimit, bool $shouldStop) { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('API'))); + $handler(new DummyMessage('API')); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) @@ -59,7 +58,7 @@ public function memoryProvider() public function testReceiverLogsMemoryExceededWhenLoggerIsGiven() { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('API'))); + $handler(new DummyMessage('API')); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php index e5c51335b3b8e..7a516744e1893 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Enhancers/StopWhenMessageCountIsExceededReceiverTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Tests\Fixtures\CallbackReceiver; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; use Symfony\Component\Messenger\Transport\Enhancers\StopWhenMessageCountIsExceededReceiver; @@ -26,9 +25,9 @@ class StopWhenMessageCountIsExceededReceiverTest extends TestCase public function testReceiverStopsWhenMaximumCountExceeded($max, $shouldStop) { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('First message'))); - $handler(Envelope::wrap(new DummyMessage('Second message'))); - $handler(Envelope::wrap(new DummyMessage('Third message'))); + $handler(new DummyMessage('First message')); + $handler(new DummyMessage('Second message')); + $handler(new DummyMessage('Third message')); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) @@ -79,7 +78,7 @@ public function testReceiverDoesntIncreaseItsCounterWhenReceiveNullMessage() public function testReceiverLogsMaximumCountExceededWhenLoggerIsGiven() { $callable = function ($handler) { - $handler(Envelope::wrap(new DummyMessage('First message'))); + $handler(new DummyMessage('First message')); }; $decoratedReceiver = $this->getMockBuilder(CallbackReceiver::class) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php index 214b773092e8c..2e227c0f2f717 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/Serialization/SerializerTest.php @@ -12,11 +12,8 @@ namespace Symfony\Component\Messenger\Tests\Transport\Serialization; use PHPUnit\Framework\TestCase; -use Symfony\Component\Messenger\Envelope; -use Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; use Symfony\Component\Messenger\Transport\Serialization\Serializer; -use Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration; use Symfony\Component\Serializer as SerializerComponent; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; @@ -29,23 +26,9 @@ public function testEncodedIsDecodable() new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) ); - $envelope = Envelope::wrap(new DummyMessage('Hello')); + $message = new DummyMessage('Hello'); - $this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope))); - } - - public function testEncodedWithConfigurationIsDecodable() - { - $serializer = new Serializer( - new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) - ); - - $envelope = Envelope::wrap(new DummyMessage('Hello')) - ->with(new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo')))) - ->with(new ValidationConfiguration(array('foo', 'bar'))) - ; - - $this->assertEquals($envelope, $serializer->decode($serializer->encode($envelope))); + $this->assertEquals($message, $serializer->decode($serializer->encode($message))); } public function testEncodedIsHavingTheBodyAndTypeHeader() @@ -54,12 +37,11 @@ public function testEncodedIsHavingTheBodyAndTypeHeader() new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) ); - $encoded = $serializer->encode(Envelope::wrap(new DummyMessage('Hello'))); + $encoded = $serializer->encode(new DummyMessage('Hello')); $this->assertArrayHasKey('body', $encoded); $this->assertArrayHasKey('headers', $encoded); $this->assertArrayHasKey('type', $encoded['headers']); - $this->assertArrayNotHasKey('X-Message-Envelope-Items', $encoded['headers']); $this->assertEquals(DummyMessage::class, $encoded['headers']['type']); } @@ -73,31 +55,10 @@ public function testUsesTheCustomFormatAndContext() $encoder = new Serializer($serializer, 'csv', array('foo' => 'bar')); - $encoded = $encoder->encode(Envelope::wrap($message)); + $encoded = $encoder->encode($message); $decoded = $encoder->decode($encoded); $this->assertSame('Yay', $encoded['body']); - $this->assertSame($message, $decoded->getMessage()); - } - - public function testEncodedWithSerializationConfiguration() - { - $serializer = new Serializer( - new SerializerComponent\Serializer(array(new ObjectNormalizer()), array('json' => new JsonEncoder())) - ); - - $envelope = Envelope::wrap(new DummyMessage('Hello')) - ->with(new SerializerConfiguration(array(ObjectNormalizer::GROUPS => array('foo')))) - ->with(new ValidationConfiguration(array('foo', 'bar'))) - ; - - $encoded = $serializer->encode($envelope); - - $this->assertArrayHasKey('body', $encoded); - $this->assertArrayHasKey('headers', $encoded); - $this->assertArrayHasKey('type', $encoded['headers']); - $this->assertEquals(DummyMessage::class, $encoded['headers']['type']); - $this->assertArrayHasKey('X-Message-Envelope-Items', $encoded['headers']); - $this->assertSame('a:2:{s:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration";C:75:"Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration":59:{a:1:{s:7:"context";a:1:{s:6:"groups";a:1:{i:0;s:3:"foo";}}}}s:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration";C:76:"Symfony\Component\Messenger\Middleware\Configuration\ValidationConfiguration":82:{a:2:{s:6:"groups";a:2:{i:0;s:3:"foo";i:1;s:3:"bar";}s:17:"is_group_sequence";b:0;}}}', $encoded['headers']['X-Message-Envelope-Items']); + $this->assertSame($message, $decoded); } } diff --git a/src/Symfony/Component/Messenger/Tests/WorkerTest.php b/src/Symfony/Component/Messenger/Tests/WorkerTest.php index bdfa6fe188d62..7599d8dadc221 100644 --- a/src/Symfony/Component/Messenger/Tests/WorkerTest.php +++ b/src/Symfony/Component/Messenger/Tests/WorkerTest.php @@ -13,7 +13,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Tests\Fixtures\CallbackReceiver; use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; @@ -23,33 +22,29 @@ class WorkerTest extends TestCase { public function testWorkerDispatchTheReceivedMessage() { - $apiMessage = new DummyMessage('API'); - $ipaMessage = new DummyMessage('IPA'); - - $receiver = new CallbackReceiver(function ($handler) use ($apiMessage, $ipaMessage) { - $handler(Envelope::wrap($apiMessage)); - $handler(Envelope::wrap($ipaMessage)); + $receiver = new CallbackReceiver(function ($handler) { + $handler(new DummyMessage('API')); + $handler(new DummyMessage('IPA')); }); $bus = $this->getMockBuilder(MessageBusInterface::class)->getMock(); - $bus->expects($this->at(0))->method('dispatch')->with(Envelope::wrap($apiMessage)->with(new ReceivedMessage())); - $bus->expects($this->at(1))->method('dispatch')->with(Envelope::wrap($ipaMessage)->with(new ReceivedMessage())); + $bus->expects($this->at(0))->method('dispatch')->with(new ReceivedMessage(new DummyMessage('API'))); + $bus->expects($this->at(1))->method('dispatch')->with(new ReceivedMessage(new DummyMessage('IPA'))); $worker = new Worker($receiver, $bus); $worker->run(); } - public function testWorkerDoesNotWrapMessagesAlreadyWrappedWithReceivedMessage() + public function testWorkerDoesNotWrapMessagesAlreadyWrappedInReceivedMessages() { - $envelop = Envelope::wrap(new DummyMessage('API'))->with(new ReceivedMessage()); - $receiver = new CallbackReceiver(function ($handler) use ($envelop) { - $handler($envelop); + $receiver = new CallbackReceiver(function ($handler) { + $handler(new ReceivedMessage(new DummyMessage('API'))); }); $bus = $this->getMockBuilder(MessageBusInterface::class)->getMock(); - $bus->expects($this->at(0))->method('dispatch')->with($envelop); + $bus->expects($this->at(0))->method('dispatch')->with(new ReceivedMessage(new DummyMessage('API'))); $worker = new Worker($receiver, $bus); $worker->run(); @@ -59,7 +54,7 @@ public function testWorkerIsThrowingExceptionsBackToGenerators() { $receiver = new CallbackReceiver(function ($handler) { try { - $handler(Envelope::wrap(new DummyMessage('Hello'))); + $handler(new DummyMessage('Hello')); $this->assertTrue(false, 'This should not be called because the exception is sent back to the generator.'); } catch (\InvalidArgumentException $e) { diff --git a/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceiver.php b/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceiver.php index 0e6fbff8ee340..e61ef0389531e 100644 --- a/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceiver.php +++ b/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpReceiver.php @@ -22,13 +22,13 @@ */ class AmqpReceiver implements ReceiverInterface { - private $decoder; + private $messageDecoder; private $connection; private $shouldStop; - public function __construct(DecoderInterface $decoder, Connection $connection) + public function __construct(DecoderInterface $messageDecoder, Connection $connection) { - $this->decoder = $decoder; + $this->messageDecoder = $messageDecoder; $this->connection = $connection; } @@ -38,8 +38,8 @@ public function __construct(DecoderInterface $decoder, Connection $connection) public function receive(callable $handler): void { while (!$this->shouldStop) { - $AMQPEnvelope = $this->connection->get(); - if (null === $AMQPEnvelope) { + $message = $this->connection->get(); + if (null === $message) { $handler(null); usleep($this->connection->getConnectionCredentials()['loop_sleep'] ?? 200000); @@ -51,18 +51,18 @@ public function receive(callable $handler): void } try { - $handler($this->decoder->decode(array( - 'body' => $AMQPEnvelope->getBody(), - 'headers' => $AMQPEnvelope->getHeaders(), + $handler($this->messageDecoder->decode(array( + 'body' => $message->getBody(), + 'headers' => $message->getHeaders(), ))); - $this->connection->ack($AMQPEnvelope); + $this->connection->ack($message); } catch (RejectMessageExceptionInterface $e) { - $this->connection->reject($AMQPEnvelope); + $this->connection->reject($message); throw $e; } catch (\Throwable $e) { - $this->connection->nack($AMQPEnvelope, AMQP_REQUEUE); + $this->connection->nack($message, AMQP_REQUEUE); throw $e; } finally { diff --git a/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpSender.php b/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpSender.php index 397c52dec2a20..0c4bb18f31ca7 100644 --- a/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpSender.php +++ b/src/Symfony/Component/Messenger/Transport/AmqpExt/AmqpSender.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Messenger\Transport\AmqpExt; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\SenderInterface; use Symfony\Component\Messenger\Transport\Serialization\EncoderInterface; @@ -22,21 +21,21 @@ */ class AmqpSender implements SenderInterface { - private $encoder; + private $messageEncoder; private $connection; - public function __construct(EncoderInterface $encoder, Connection $connection) + public function __construct(EncoderInterface $messageEncoder, Connection $connection) { - $this->encoder = $encoder; + $this->messageEncoder = $messageEncoder; $this->connection = $connection; } /** * {@inheritdoc} */ - public function send(Envelope $envelope) + public function send($message) { - $encodedMessage = $this->encoder->encode($envelope); + $encodedMessage = $this->messageEncoder->encode($message); $this->connection->publish($encodedMessage['body'], $encodedMessage['headers']); } diff --git a/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiver.php b/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiver.php index 37b1978233e9f..4a05afe7707fb 100644 --- a/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiver.php +++ b/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMemoryUsageIsExceededReceiver.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Messenger\Transport\Enhancers; use Psr\Log\LoggerInterface; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\ReceiverInterface; /** @@ -37,8 +36,8 @@ public function __construct(ReceiverInterface $decoratedReceiver, int $memoryLim public function receive(callable $handler): void { - $this->decoratedReceiver->receive(function (?Envelope $envelope) use ($handler) { - $handler($envelope); + $this->decoratedReceiver->receive(function ($message) use ($handler) { + $handler($message); $memoryResolver = $this->memoryResolver; if ($memoryResolver() > $this->memoryLimit) { diff --git a/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMessageCountIsExceededReceiver.php b/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMessageCountIsExceededReceiver.php index 420eb8fa63a40..dc61466bbf401 100644 --- a/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMessageCountIsExceededReceiver.php +++ b/src/Symfony/Component/Messenger/Transport/Enhancers/StopWhenMessageCountIsExceededReceiver.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Messenger\Transport\Enhancers; use Psr\Log\LoggerInterface; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Transport\ReceiverInterface; /** @@ -35,10 +34,10 @@ public function receive(callable $handler): void { $receivedMessages = 0; - $this->decoratedReceiver->receive(function (?Envelope $envelope) use ($handler, &$receivedMessages) { - $handler($envelope); + $this->decoratedReceiver->receive(function ($message) use ($handler, &$receivedMessages) { + $handler($message); - if (null !== $envelope && ++$receivedMessages >= $this->maximumNumberOfMessages) { + if (null !== $message && ++$receivedMessages >= $this->maximumNumberOfMessages) { $this->stop(); if (null !== $this->logger) { $this->logger->info('Receiver stopped due to maximum count of {count} exceeded', array('count' => $this->maximumNumberOfMessages)); diff --git a/src/Symfony/Component/Messenger/Transport/ReceiverInterface.php b/src/Symfony/Component/Messenger/Transport/ReceiverInterface.php index 0f61fe3428d7e..1c29fbe43abe7 100644 --- a/src/Symfony/Component/Messenger/Transport/ReceiverInterface.php +++ b/src/Symfony/Component/Messenger/Transport/ReceiverInterface.php @@ -21,10 +21,10 @@ interface ReceiverInterface /** * Receive some messages to the given handler. * - * The handler will have, as argument, the received {@link \Symfony\Component\Messenger\Envelope} containing the message. - * Note that this envelope can be `null` if the timeout to receive something has expired. + * The handler will have, as argument, the received message. Note that this message + * can be `null` if the timeout to receive something has expired. */ - public function receive(callable $handler): void; + public function receive(callable $handler) : void; /** * Stop receiving some messages. diff --git a/src/Symfony/Component/Messenger/Transport/SenderInterface.php b/src/Symfony/Component/Messenger/Transport/SenderInterface.php index ba36b80fe2af2..a142e1f00995e 100644 --- a/src/Symfony/Component/Messenger/Transport/SenderInterface.php +++ b/src/Symfony/Component/Messenger/Transport/SenderInterface.php @@ -11,8 +11,6 @@ namespace Symfony\Component\Messenger\Transport; -use Symfony\Component\Messenger\Envelope; - /** * @author Samuel Roze * @@ -21,9 +19,9 @@ interface SenderInterface { /** - * Sends the given envelope. + * Sends the given message. * - * @param Envelope $envelope + * @param object $message */ - public function send(Envelope $envelope); + public function send($message); } diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/DecoderInterface.php b/src/Symfony/Component/Messenger/Transport/Serialization/DecoderInterface.php index 026a32103316b..a232dfbf10e30 100644 --- a/src/Symfony/Component/Messenger/Transport/Serialization/DecoderInterface.php +++ b/src/Symfony/Component/Messenger/Transport/Serialization/DecoderInterface.php @@ -11,8 +11,6 @@ namespace Symfony\Component\Messenger\Transport\Serialization; -use Symfony\Component\Messenger\Envelope; - /** * @author Samuel Roze * @@ -21,16 +19,16 @@ interface DecoderInterface { /** - * Decodes an envelope and its message from an encoded-form. + * Decodes the message from an encoded-form. * - * The `$encodedEnvelope` parameter is a key-value array that - * describes the envelope and its content, that will be used by the different transports. + * The `$encodedMessage` parameter is a key-value array that + * describes the message, that will be used by the different transports. * * The most common keys are: * - `body` (string) - the message body * - `headers` (string) - a key/value pair of headers * - * @return Envelope + * @return object */ - public function decode(array $encodedEnvelope): Envelope; + public function decode(array $encodedMessage); } diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/EncoderInterface.php b/src/Symfony/Component/Messenger/Transport/Serialization/EncoderInterface.php index 1dce6fdae33cd..658b9e5b5e681 100644 --- a/src/Symfony/Component/Messenger/Transport/Serialization/EncoderInterface.php +++ b/src/Symfony/Component/Messenger/Transport/Serialization/EncoderInterface.php @@ -10,7 +10,6 @@ */ namespace Symfony\Component\Messenger\Transport\Serialization; -use Symfony\Component\Messenger\Envelope; /** * @author Samuel Roze @@ -20,14 +19,14 @@ interface EncoderInterface { /** - * Encodes an envelope content (message & items) to a common format understandable by transports. - * The encoded array should only contain scalar and arrays. + * Encodes a message to a common format understandable by transports. The encoded array should only + * contain scalar and arrays. * * The most common keys of the encoded array are: * - `body` (string) - the message body * - `headers` (string) - a key/value pair of headers * - * @param Envelope $envelope The envelop containing the message put on the MessageBus by the user + * @param object $message The object that is put on the MessageBus by the user */ - public function encode(Envelope $envelope): array; + public function encode($message): array; } diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php index cfb30090e5dfa..65c2ac55e8886 100644 --- a/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php +++ b/src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Messenger\Transport\Serialization; -use Symfony\Component\Messenger\Envelope; use Symfony\Component\Serializer\SerializerInterface; /** @@ -33,48 +32,27 @@ public function __construct(SerializerInterface $serializer, string $format = 'j /** * {@inheritdoc} */ - public function decode(array $encodedEnvelope): Envelope + public function decode(array $encodedMessage) { - if (empty($encodedEnvelope['body']) || empty($encodedEnvelope['headers'])) { - throw new \InvalidArgumentException('Encoded envelope should have at least a `body` and some `headers`.'); + if (empty($encodedMessage['body']) || empty($encodedMessage['headers'])) { + throw new \InvalidArgumentException('Encoded message should have at least a `body` and some `headers`.'); } - if (empty($encodedEnvelope['headers']['type'])) { - throw new \InvalidArgumentException('Encoded envelope does not have a `type` header.'); + if (empty($encodedMessage['headers']['type'])) { + throw new \InvalidArgumentException('Encoded message does not have a `type` header.'); } - $envelopeItems = isset($encodedEnvelope['headers']['X-Message-Envelope-Items']) ? unserialize($encodedEnvelope['headers']['X-Message-Envelope-Items']) : array(); - - $context = $this->context; - /** @var SerializerConfiguration|null $serializerConfig */ - if ($serializerConfig = $envelopeItems[SerializerConfiguration::class] ?? null) { - $context = $serializerConfig->getContext() + $context; - } - - $message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context); - - return new Envelope($message, $envelopeItems); + return $this->serializer->deserialize($encodedMessage['body'], $encodedMessage['headers']['type'], $this->format, $this->context); } /** * {@inheritdoc} */ - public function encode(Envelope $envelope): array + public function encode($message): array { - $context = $this->context; - /** @var SerializerConfiguration|null $serializerConfig */ - if ($serializerConfig = $envelope->get(SerializerConfiguration::class)) { - $context = $serializerConfig->getContext() + $context; - } - - $headers = array('type' => \get_class($envelope->getMessage())); - if ($configurations = $envelope->all()) { - $headers['X-Message-Envelope-Items'] = serialize($configurations); - } - return array( - 'body' => $this->serializer->serialize($envelope->getMessage(), $this->format, $context), - 'headers' => $headers, + 'body' => $this->serializer->serialize($message, $this->format, $this->context), + 'headers' => array('type' => \get_class($message)), ); } } diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/SerializerConfiguration.php b/src/Symfony/Component/Messenger/Transport/Serialization/SerializerConfiguration.php deleted file mode 100644 index 478e197080adf..0000000000000 --- a/src/Symfony/Component/Messenger/Transport/Serialization/SerializerConfiguration.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Messenger\Transport\Serialization; - -use Symfony\Component\Messenger\EnvelopeItemInterface; - -/** - * @author Maxime Steinhausser - * - * @experimental in 4.1 - */ -final class SerializerConfiguration implements EnvelopeItemInterface -{ - private $context; - - public function __construct(array $context) - { - $this->context = $context; - } - - public function getContext(): array - { - return $this->context; - } - - public function serialize() - { - return serialize(array('context' => $this->context)); - } - - public function unserialize($serialized) - { - list('context' => $context) = unserialize($serialized, array('allowed_classes' => false)); - - $this->__construct($context); - } -} diff --git a/src/Symfony/Component/Messenger/Worker.php b/src/Symfony/Component/Messenger/Worker.php index 918b61a52a18c..2033f3a770fd1 100644 --- a/src/Symfony/Component/Messenger/Worker.php +++ b/src/Symfony/Component/Messenger/Worker.php @@ -41,12 +41,16 @@ public function run() }); } - $this->receiver->receive(function (?Envelope $envelope) { - if (null === $envelope) { + $this->receiver->receive(function ($message) { + if (null === $message) { return; } - $this->bus->dispatch($envelope->with(new ReceivedMessage())); + if (!$message instanceof ReceivedMessage) { + $message = new ReceivedMessage($message); + } + + $this->bus->dispatch($message); }); } }