-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Messenger] collect all stamps added on Envelope as collections #29159
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ public function __construct($message, StampInterface ...$stamps) | |
$this->message = $message; | ||
|
||
foreach ($stamps as $stamp) { | ||
$this->stamps[\get_class($stamp)] = $stamp; | ||
$this->stamps[\get_class($stamp)][] = $stamp; | ||
} | ||
} | ||
|
||
|
@@ -48,22 +48,26 @@ public function with(StampInterface ...$stamps): self | |
$cloned = clone $this; | ||
|
||
foreach ($stamps as $stamp) { | ||
$cloned->stamps[\get_class($stamp)] = $stamp; | ||
$cloned->stamps[\get_class($stamp)][] = $stamp; | ||
} | ||
|
||
return $cloned; | ||
} | ||
|
||
public function get(string $stampFqcn): ?StampInterface | ||
public function last(string $stampFqcn): ?StampInterface | ||
{ | ||
return $this->stamps[$stampFqcn] ?? null; | ||
return isset($this->stamps[$stampFqcn]) ? end($this->stamps[$stampFqcn]) : null; | ||
} | ||
|
||
/** | ||
* @return StampInterface[] indexed by fqcn | ||
* @return StampInterface[]|StampInterface[][] The stamps for the specified FQCN, or all stamps by their class name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be better to have 2 different methods instead of two kind of returned value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure personally: when reading, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you're both right. |
||
*/ | ||
public function all(): array | ||
public function all(string $stampFqcn = null): array | ||
{ | ||
if (null !== $stampFqcn) { | ||
return $this->stamps[$stampFqcn] ?? array(); | ||
} | ||
|
||
return $this->stamps; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
use Symfony\Component\Messenger\Stamp\SerializerStamp; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Encoder\XmlEncoder; | ||
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; | ||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
use Symfony\Component\Serializer\Serializer as SymfonySerializer; | ||
use Symfony\Component\Serializer\SerializerInterface as SymfonySerializerInterface; | ||
|
@@ -34,9 +35,9 @@ class Serializer implements SerializerInterface | |
private $format; | ||
private $context; | ||
|
||
public function __construct(SymfonySerializerInterface $serializer, string $format = 'json', array $context = array()) | ||
public function __construct(SymfonySerializerInterface $serializer = null, string $format = 'json', array $context = array()) | ||
{ | ||
$this->serializer = $serializer; | ||
$this->serializer = $serializer ?? self::create()->serializer; | ||
$this->format = $format; | ||
$this->context = $context; | ||
} | ||
|
@@ -48,7 +49,7 @@ public static function create(): self | |
} | ||
|
||
$encoders = array(new XmlEncoder(), new JsonEncoder()); | ||
$normalizers = array(new ObjectNormalizer()); | ||
$normalizers = array(new ArrayDenormalizer(), new ObjectNormalizer()); | ||
$serializer = new SymfonySerializer($normalizers, $encoders); | ||
|
||
return new self($serializer); | ||
|
@@ -70,9 +71,8 @@ public function decode(array $encodedEnvelope): Envelope | |
$stamps = $this->decodeStamps($encodedEnvelope); | ||
|
||
$context = $this->context; | ||
/** @var SerializerStamp|null $serializerStamp */ | ||
if ($serializerStamp = $stamps[SerializerStamp::class] ?? null) { | ||
$context = $serializerStamp->getContext() + $context; | ||
if (isset($stamps[SerializerStamp::class])) { | ||
$context = end($stamps[SerializerStamp::class])->getContext() + $context; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can actually support multiple stamps no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. like merging all stamps together? I don't know, so I wouldn't change that in this PR :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think deciding on end() vs reset() vs merging kinda belongs to this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. about reset() vs end(): the previous behavior returned the latest stamp, so it should be end() - in need of any reason to change it I'd keep the behavior here. |
||
} | ||
|
||
$message = $this->serializer->deserialize($encodedEnvelope['body'], $encodedEnvelope['headers']['type'], $this->format, $context); | ||
|
@@ -87,7 +87,7 @@ public function encode(Envelope $envelope): array | |
{ | ||
$context = $this->context; | ||
/** @var SerializerStamp|null $serializerStamp */ | ||
if ($serializerStamp = $envelope->get(SerializerStamp::class)) { | ||
if ($serializerStamp = $envelope->last(SerializerStamp::class)) { | ||
$context = $serializerStamp->getContext() + $context; | ||
} | ||
|
||
|
@@ -107,21 +107,24 @@ private function decodeStamps(array $encodedEnvelope): array | |
continue; | ||
} | ||
|
||
$stamps[] = $this->serializer->deserialize($value, substr($name, \strlen(self::STAMP_HEADER_PREFIX)), $this->format, $this->context); | ||
$stamps[] = $this->serializer->deserialize($value, substr($name, \strlen(self::STAMP_HEADER_PREFIX)).'[]', $this->format, $this->context); | ||
} | ||
if ($stamps) { | ||
$stamps = array_merge(...$stamps); | ||
} | ||
|
||
return $stamps; | ||
} | ||
|
||
private function encodeStamps(Envelope $envelope): array | ||
{ | ||
if (!$stamps = $envelope->all()) { | ||
if (!$allStamps = $envelope->all()) { | ||
return array(); | ||
} | ||
|
||
$headers = array(); | ||
foreach ($stamps as $stamp) { | ||
$headers[self::STAMP_HEADER_PREFIX.\get_class($stamp)] = $this->serializer->serialize($stamp, $this->format, $this->context); | ||
foreach ($allStamps as $class => $stamps) { | ||
$headers[self::STAMP_HEADER_PREFIX.$class] = $this->serializer->serialize($stamps, $this->format, $this->context); | ||
} | ||
|
||
return $headers; | ||
|
Uh oh!
There was an error while loading. Please reload this page.