1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Queue\Message; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
|
9
|
|
|
abstract class AbstractEnvelope implements EnvelopeInterface |
10
|
|
|
{ |
11
|
|
|
protected array $metadata = []; |
12
|
|
|
private MessageInterface $message; |
13
|
|
|
|
14
|
|
|
public function __construct(MessageInterface $message) |
15
|
|
|
{ |
16
|
|
|
$this->metadata = $message->getMetadata(); |
17
|
|
|
$envelopes = [static::class]; |
18
|
|
|
while ($message instanceof EnvelopeInterface) { |
19
|
|
|
if ($message::class !== static::class) { |
20
|
|
|
$envelopes = [$message::class]; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$message = $message->getMessage(); |
24
|
|
|
} |
25
|
|
|
$this->message = $message; |
26
|
|
|
|
27
|
|
|
if (is_array($this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY])) { |
28
|
|
|
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = array_merge( |
29
|
|
|
$envelopes, |
30
|
|
|
array_filter( |
31
|
|
|
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY], |
32
|
|
|
static fn (string $envelope): bool => !in_array($envelope, $envelopes), |
33
|
|
|
), |
34
|
|
|
); |
35
|
|
|
} else { |
36
|
|
|
$this->metadata[EnvelopeInterface::ENVELOPE_STACK_KEY] = [static::class]; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getMessage(): MessageInterface |
41
|
|
|
{ |
42
|
|
|
return $this->message; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getHandlerName(): string |
46
|
|
|
{ |
47
|
|
|
return $this->message->getHandlerName(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getData(): mixed |
51
|
|
|
{ |
52
|
|
|
return $this->message->getData(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getMetadata(): array |
56
|
|
|
{ |
57
|
|
|
return ArrayHelper::merge( |
58
|
|
|
$this->metadata, |
59
|
|
|
$this->getEnvelopeMetadata(), |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Metadata of the envelope |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
abstract protected function getEnvelopeMetadata(): array; |
69
|
|
|
} |
70
|
|
|
|