8000 [Messenger] Move Transport/InMemoryTransport to Transport/InMemory/InMemoryTransport by lyrixx · Pull Request #48614 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Move Transport/InMemoryTransport to Transport/InMemory/InMemoryTransport #48614

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

Merged
merged 1 commit into from
Dec 16, 2022
Merged
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
8 changes: 8 additions & 0 deletions UPGRADE-6.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ HttpKernel

* Deprecate parameters `container.dumper.inline_factories` and `container.dumper.inline_class_loader`, use `.container.dumper.inline_factories` and `.container.dumper.inline_class_loader` instead

Messenger
---------

* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`

SecurityBundle
--------------

Expand Down
8 changes: 8 additions & 0 deletions UPGRADE-7.0.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
UPGRADE FROM 6.4 to 7.0
=======================

Messenger
---------

* Remove `Symfony\Component\Messenger\Transport\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`

Workflow
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use Symfony\Component\Messenger\Middleware\ValidationMiddleware;
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy;
use Symfony\Component\Messenger\RoutableMessageBus;
use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory;
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"symfony/http-client": "^5.4|^6.0",
"symfony/lock": "^5.4|^6.0",
"symfony/mailer": "^5.4|^6.0",
"symfony/messenger": "^6.2",
"symfony/messenger": "^6.3",
"symfony/mime": "^6.2",
"symfony/notifier": "^5.4|^6.0",
"symfony/process": "^5.4|^6.0",
Expand Down Expand Up @@ -83,7 +83,7 @@
"symfony/form": "<5.4",
"symfony/lock": "<5.4",
"symfony/mailer": "<5.4",
"symfony/messenger": "<6.2",
"symfony/messenger": "<6.3",
"symfony/mime": "<6.2",
"symfony/property-info": "<5.4",
"symfony/property-access": "<5.4",
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CHANGELOG
=========

6.3
---

* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`

6.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Transport;
namespace Symfony\Component\Messenger\Tests\Transport\InMemory;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\InMemoryTransport;
use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Transport;
namespace Symfony\Component\Messenger\Tests\Transport\InMemory;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Transport\InMemoryTransport;
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;

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

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Transport\InMemory;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* Transport that stays in memory. Useful for testing purpose.
*
* @author Gary PEGEOT <garypegeot@gmail.com>
*/
class InMemoryTransport implements TransportInterface, ResetInterface
{
/**
* @var Envelope[]
*/
private array $sent = [];

/**
* @var Envelope[]
*/
private array $acknowledged = [];

/**
* @var Envelope[]
*/
private array $rejected = [];

/**
* @var Envelope[]
*/
private array $queue = [];

private int $nextId = 1;
private ?SerializerInterface $serializer;

public function __construct(SerializerInterface $serializer = null)
{
$this->serializer = $serializer;
}

public function get(): iterable
{
return array_values($this->decode($this->queue));
}

public function ack(Envelope $envelope): void
{
$this->acknowledged[] = $this->encode($envelope);

if (!$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class)) {
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
}

unset($this->queue[$transportMessageIdStamp->getId()]);
}

public function reject(Envelope $envelope): void
{
$this->rejected[] = $this->encode($envelope);

if (!$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class)) {
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
}

unset($this->queue[$transportMessageIdStamp->getId()]);
}

public function send(Envelope $envelope): Envelope
{
$id = $this->nextId++;
$envelope = $envelope->with(new TransportMessageIdStamp($id));
$encodedEnvelope = $this->encode($envelope);
$this->sent[] = $encodedEnvelope;
$this->queue[$id] = $encodedEnvelope;

return $envelope;
}

public function reset()
{
$this->sent = $this->queue = $this->rejected = $this->acknowledged = [];
}

/**
* @return Envelope[]
*/
public function getAcknowledged(): array
{
return $this->decode($this->acknowledged);
}

/**
* @return Envelope[]
*/
public function getRejected(): array
{
return $this->decode($this->rejected);
}

/**
* @return Envelope[]
*/
public function getSent(): array
{
return $this->decode($this->sent);
}

private function encode(Envelope $envelope): Envelope|array
{
if (null === $this->serializer) {
return $envelope;
}

return $this->serializer->encode($envelope);
}

/**
* @param array<mixed> $messagesEncoded
*
* @return Envelope[]
*/
private function decode(array $messagesEncoded): array
{
if (null === $this->serializer) {
return $messagesEncoded;
}

return array_map($this->serializer->decode(...), $messagesEncoded);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Transport\InMemory;

use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Contracts\Service\ResetInterface;

/**
* @author Gary PEGEOT <garypegeot@gmail.com>
*/
class InMemoryTransportFactory implements TransportFactoryInterface, ResetInterface
{
/**
* @var InMemoryTransport[]
*/
private array $createdTransports = [];

public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
{
['serialize' => $serialize] = $this->parseDsn($dsn);

return $this->createdTransports[] = new InMemoryTransport($serialize ? $serializer : null);
}

public function supports(string $dsn, array $options): bool
{
return str_starts_with($dsn, 'in-memory://');
}

public function reset()
{
foreach ($this->createdTransports as $transport) {
$transport->reset();
}
}

private function parseDsn(string $dsn): array
{
$query = [];
if ($queryAsString = strstr($dsn, '?')) {
parse_str(ltrim($queryAsString, '?'), $query);
}

return [
'serialize' => filter_var($query['serialize'] ?? false, \FILTER_VALIDATE_BOOL),
];
}
}
Loading
0