8000 [Messenger] Move Transport/InMemoryTransport to Transport/InMemory/In… · symfony/symfony@c5b5bf9 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5b5bf9

Browse files
committed
[Messenger] Move Transport/InMemoryTransport to Transport/InMemory/InMemoryTransport
1 parent 233b9eb commit c5b5bf9

File tree

10 files changed

+249
-171
lines changed

10 files changed

+249
-171
lines changed

UPGRADE-6.3.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ HttpKernel
1111

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

14+
Messenger
15+
---------
16+
17+
* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransport` in favor
18+
of `Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport`
19+
* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in
20+
favor of
21+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
22+
23+
1424
SecurityBundle
1525
--------------
1626

UPGRADE-7.0.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
UPGRADE FROM 6.4 to 7.0
22
=======================
33

4+
Messenger
5+
---------
6+
7+
* Remove `Symfony\Component\Messenger\Transport\InMemoryTransport` in favor
8+
of `Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport`
9+
* Remove `Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in
10+
favor of
11+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
12+
13+
414
Workflow
515
--------
616

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
use Symfony\Component\Messenger\Middleware\ValidationMiddleware;
3636
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy;
3737
use Symfony\Component\Messenger\RoutableMessageBus;
38-
use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
38+
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory;
3939
use Symfony\Component\Messenger\Transport\Sender\SendersLocator;
4040
use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer;
4141
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransport` in favor
8+
of `Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport`
9+
* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in
10+
favor of
11+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
12+
413
6.2
514
---
615

src/Symfony/Component/Messenger/Tests/Transport/InMemoryTransportFactoryTest.php renamed to src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Messenger\Tests\Transport;
12+
namespace Symfony\Component\Messenger\Tests\Transport\InMemory;
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
1717
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
18-
use Symfony\Component\Messenger\Transport\InMemoryTransport;
19-
use Symfony\Component\Messenger\Transport\InMemoryTransportFactory;
18+
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
19+
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory;
2020
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2121

2222
/**

src/Symfony/Component/Messenger/Tests/Transport/InMemoryTransportTest.php renamed to src/Symfony/Component/Messenger/Tests/Transport/InMemory/InMemoryTransportTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Messenger\Tests\Transport;
12+
namespace Symfony\Component\Messenger\Tests\Transport\InMemory;
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
1717
use Symfony\Component\Messenger\Tests\Fixtures\AnEnvelopeStamp;
1818
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
19-
use Symfony\Component\Messenger\Transport\InMemoryTransport;
19+
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
2020
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2121

2222
/**
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Transport\InMemory;
13+
14+
use Symfony\Component\Messenger\Envelope;
15+
use Symfony\Component\Messenger\Exception\LogicException;
16+
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
17+
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
18+
use Symfony\Component\Messenger\Transport\TransportInterface;
19+
use Symfony\Contracts\Service\ResetInterface;
20+
21+
/**
22+
* Transport that stays in memory. Useful for testing purpose.
23+
*
24+
* @author Gary PEGEOT <garypegeot@gmail.com>
25+
*/
26+
class InMemoryTransport implements TransportInterface, ResetInterface
27+
{
28+
/**
29+
* @var Envelope[]
30+
*/
31+
private array $sent = [];
32+
33+
/**
34+
* @var Envelope[]
35+
*/
36+
private array $acknowledged = [];
37+
38+
/**
39+
* @var Envelope[]
40+
*/
41+
private array $rejected = [];
42+
43+
/**
44+
* @var Envelope[]
45+
*/
46+
private array $queue = [];
47+
48+
private int $nextId = 1;
49+
private ?SerializerInterface $serializer;
50+
51+
public function __construct(SerializerInterface $serializer = null)
52+
{
53+
$this->serializer = $serializer;
54+
}
55+
56+
public function get(): iterable
57+
{
58+
return array_values($this->decode($this->queue));
59+
}
60+
61+
public function ack(Envelope $envelope): void
62+
{
63+
$this->acknowledged[] = $this->encode($envelope);
64+
65+
if (!$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class)) {
66+
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
67+
}
68+
69+
unset($this->queue[$transportMessageIdStamp->getId()]);
70+
}
71+
72+
public function reject(Envelope $envelope): void
73+
{
74+
$this->rejected[] = $this->encode($envelope);
75+
76+
if (!$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class)) {
77+
throw new LogicException('No TransportMessageIdStamp found on the Envelope.');
78+
}
79+
80+
unset($this->queue[$transportMessageIdStamp->getId()]);
81+
}
82+
83+
public function send(Envelope $envelope): Envelope
84+
{
85+
$id = $this->nextId++;
86+
$envelope = $envelope->with(new TransportMessageIdStamp($id));
87+
$encodedEnvelope = $this->encode($envelope);
88+
$this->sent[] = $encodedEnvelope;
89+
$this->queue[$id] = $encodedEnvelope;
90+
91+
return $envelope;
92+
}
93+
94+
public function reset()
95+
{
96+
$this->sent = $this->queue = $this->rejected = $this->acknowledged = [];
97+
}
98+
99+
/**
100+
* @return Envelope[]
101+
*/
102+
public function getAcknowledged(): array
103+
{
104+
return $this->decode($this->acknowledged);
105+
}
106+
107+
/**
108+
* @return Envelope[]
109+
*/
110+
public function getRejected(): array
111+
{
112+
return $this->decode($this->rejected);
113+
}
114+
115+
/**
116+
* @return Envelope[]
117+
*/
118+
public function getSent(): array
119+
{
120+
return $this->decode($this->sent);
121+
}
122+
123+
private function encode(Envelope $envelope): Envelope|array
124+
{
125+
if (null === $this->serializer) {
126+
return $envelope;
127+
}
128+
129+
return $this->serializer->encode($envelope);
130+
}
131+
132+
/**
133+
* @param array<mixed> $messagesEncoded
134+
*
135+
* @return Envelope[]
136+
*/
137+
private function decode(array $messagesEncoded): array
138+
{
139+
if (null === $this->serializer) {
140+
return $messagesEncoded;
141+
}
142+
143+
return array_map($this->serializer->decode(...), $messagesEncoded);
144+
}
145+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Transport\InMemory;
13+
14+
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
15+
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
16+
use Symfony\Component\Messenger\Transport\TransportInterface;
17+
use Symfony\Contracts\Service\ResetInterface;
18+
19+
/**
20+
* @author Gary PEGEOT <garypegeot@gmail.com>
21+
*/
22+
class InMemoryTransportFactory implements TransportFactoryInterface, ResetInterface
23+
{
24+
/**
25+
* @var InMemoryTransport[]
26+
*/
27+
private array $createdTransports = [];
28+
29+
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
30+
{
31+
['serialize' => $serialize] = $this->parseDsn($dsn);
32+
33+
return $this->createdTransports[] = new InMemoryTransport($serialize ? $serializer : null);
34+
}
35+
36+
public function supports(string $dsn, array $options): bool
37+
{
38+
return str_starts_with($dsn, 'in-memory://');
39+
}
40+
41+
public function reset()
42+
{
43+
foreach ($this->createdTransports as $transport) {
44+
$transport->reset();
45+
}
46+
}
47+
48+
private function parseDsn(string $dsn): array
49+
{
50+
$query = [];
51+
if ($queryAsString = strstr($dsn, '?')) {
52+
parse_str(ltrim($queryAsString, '?'), $query);
53+
}
54+
55+
return [
56+
'serialize' => filter_var($query['serialize'] ?? false, \FILTER_VALIDATE_BOOL),
57+
];
58+
}
59+
}

0 commit comments

Comments
 (0)
0