Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3a2301

Browse files
feature #48614 [Messenger] Move Transport/InMemoryTransport to Transport/InMemory/InMemoryTransport (lyrixx)
This PR was merged into the 6.3 branch. Discussion ---------- [Messenger] Move Transport/InMemoryTransport to Transport/InMemory/InMemoryTransport | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | | License | MIT | Doc PR | --- For consistency with `SyncTransport` Commits ------- a7926b2 [Messenger] Move Transport/InMemoryTransport to Transport/InMemory/InMemoryTransport
2 parents 03930d3 + a7926b2 commit b3a2301

File tree

11 files changed

+246
-173
lines changed

11 files changed

+246
-173
lines changed

UPGRADE-6.3.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ 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` and
18+
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
19+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
20+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
21+
1422
SecurityBundle
1523
--------------
1624

UPGRADE-7.0.md

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

4+
Messenger
5+
---------
6+
7+
* Remove `Symfony\Component\Messenger\Transport\InMemoryTransport` and
8+
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
9+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
10+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
11+
412
Workflow
513
--------
614

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/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"symfony/http-client": "^5.4|^6.0",
4949
"symfony/lock": "^5.4|^6.0",
5050
"symfony/mailer": "^5.4|^6.0",
51-
"symfony/messenger": "^6.2",
51+
"symfony/messenger": "^6.3",
5252
"symfony/mime": "^6.2",
5353
"symfony/notifier": "^5.4|^6.0",
5454
"symfony/process": "^5.4|^6.0",
@@ -83,7 +83,7 @@
8383
"symfony/form": "<5.4",
8484
"symfony/lock": "<5.4",
8585
"symfony/mailer": "<5.4",
86-
"symfony/messenger": "<6.2",
86+
"symfony/messenger": "<6.3",
8787
"symfony/mime": "<6.2",
8888
"symfony/property-info": "<5.4",
8989
"symfony/property-access": "<5.4",

src/Symfony/Component/Messenger/CHANGELOG.md

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

4+
6.3
5+
---
6+
7+
* Deprecate `Symfony\Component\Messenger\Transport\InMemoryTransport` and
8+
`Symfony\Component\Messenger\Transport\InMemoryTransportFactory` in favor of
9+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport` and
10+
`Symfony\Component\Messenger\Transport\InMemory\InMemoryTransportFactory`
11+
412
6.2
513
---
614

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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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)