Skip to content

Commit f3f388b

Browse files
committed
Memorise metadata in envelopes
1 parent 4370fa0 commit f3f388b

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/Message/Envelope.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
abstract class Envelope implements EnvelopeInterface
88
{
9+
private ?array $metadata = null;
10+
911
public function __construct(protected MessageInterface $message)
1012
{
1113
}
@@ -34,16 +36,20 @@ public function getData(): mixed
3436

3537
public function getMetadata(): array
3638
{
37-
return array_merge(
38-
$this->message->getMetadata(),
39-
[
40-
EnvelopeInterface::ENVELOPE_STACK_KEY => array_merge(
41-
$this->message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY] ?? [],
42-
[static::class],
43-
),
44-
],
45-
$this->getEnvelopeMetadata(),
46-
);
39+
if ($this->metadata === null) {
40+
$this->metadata = array_merge(
41+
$this->message->getMetadata(),
42+
[
43+
EnvelopeInterface::ENVELOPE_STACK_KEY => array_merge(
44+
$this->message->getMetadata()[EnvelopeInterface::ENVELOPE_STACK_KEY] ?? [],
45+
[static::class],
46+
),
47+
],
48+
$this->getEnvelopeMetadata(),
49+
);
50+
}
51+
52+
return $this->metadata;
4753
}
4854

4955
abstract protected function getEnvelopeMetadata(): array;

0 commit comments

Comments
 (0)