Envelope.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Messenger;
  11. use Symfony\Component\Messenger\Stamp\StampInterface;
  12. /**
  13. * A message wrapped in an envelope with stamps (configurations, markers, ...).
  14. *
  15. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  16. */
  17. final class Envelope
  18. {
  19. /**
  20. * @var array<string, list<StampInterface>>
  21. */
  22. private $stamps = [];
  23. private $message;
  24. /**
  25. * @param StampInterface[] $stamps
  26. */
  27. public function __construct(object $message, array $stamps = [])
  28. {
  29. $this->message = $message;
  30. foreach ($stamps as $stamp) {
  31. $this->stamps[\get_class($stamp)][] = $stamp;
  32. }
  33. }
  34. /**
  35. * Makes sure the message is in an Envelope and adds the given stamps.
  36. *
  37. * @param object|Envelope $message
  38. * @param StampInterface[] $stamps
  39. */
  40. public static function wrap(object $message, array $stamps = []): self
  41. {
  42. $envelope = $message instanceof self ? $message : new self($message);
  43. return $envelope->with(...$stamps);
  44. }
  45. /**
  46. * @return static A new Envelope instance with additional stamp
  47. */
  48. public function with(StampInterface ...$stamps): self
  49. {
  50. $cloned = clone $this;
  51. foreach ($stamps as $stamp) {
  52. $cloned->stamps[\get_class($stamp)][] = $stamp;
  53. }
  54. return $cloned;
  55. }
  56. /**
  57. * @return static A new Envelope instance without any stamps of the given class
  58. */
  59. public function withoutAll(string $stampFqcn): self
  60. {
  61. $cloned = clone $this;
  62. unset($cloned->stamps[$this->resolveAlias($stampFqcn)]);
  63. return $cloned;
  64. }
  65. /**
  66. * Removes all stamps that implement the given type.
  67. */
  68. public function withoutStampsOfType(string $type): self
  69. {
  70. $cloned = clone $this;
  71. $type = $this->resolveAlias($type);
  72. foreach ($cloned->stamps as $class => $stamps) {
  73. if ($class === $type || is_subclass_of($class, $type)) {
  74. unset($cloned->stamps[$class]);
  75. }
  76. }
  77. return $cloned;
  78. }
  79. public function last(string $stampFqcn): ?StampInterface
  80. {
  81. return isset($this->stamps[$stampFqcn = $this->resolveAlias($stampFqcn)]) ? end($this->stamps[$stampFqcn]) : null;
  82. }
  83. /**
  84. * @return StampInterface[]|StampInterface[][] The stamps for the specified FQCN, or all stamps by their class name
  85. */
  86. public function all(string $stampFqcn = null): array
  87. {
  88. if (null !== $stampFqcn) {
  89. return $this->stamps[$this->resolveAlias($stampFqcn)] ?? [];
  90. }
  91. return $this->stamps;
  92. }
  93. /**
  94. * @return object The original message contained in the envelope
  95. */
  96. public function getMessage(): object
  97. {
  98. return $this->message;
  99. }
  100. /**
  101. * BC to be removed in 6.0.
  102. */
  103. private function resolveAlias(string $fqcn): string
  104. {
  105. static $resolved;
  106. return $resolved[$fqcn] ?? ($resolved[$fqcn] = class_exists($fqcn) ? (new \ReflectionClass($fqcn))->getName() : $fqcn);
  107. }
  108. }