Envelope.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. private $stamps = [];
  20. private $message;
  21. /**
  22. * @param object $message
  23. * @param StampInterface[] $stamps
  24. */
  25. public function __construct($message, array $stamps = [])
  26. {
  27. if (!\is_object($message)) {
  28. throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object but got "%s".', __METHOD__, get_debug_type($message)));
  29. }
  30. $this->message = $message;
  31. foreach ($stamps as $stamp) {
  32. $this->stamps[\get_class($stamp)][] = $stamp;
  33. }
  34. }
  35. /**
  36. * Makes sure the message is in an Envelope and adds the given stamps.
  37. *
  38. * @param object|Envelope $message
  39. * @param StampInterface[] $stamps
  40. */
  41. public static function wrap($message, array $stamps = []): self
  42. {
  43. $envelope = $message instanceof self ? $message : new self($message);
  44. return $envelope->with(...$stamps);
  45. }
  46. /**
  47. * @return Envelope a new Envelope instance with additional stamp
  48. */
  49. public function with(StampInterface ...$stamps): self
  50. {
  51. $cloned = clone $this;
  52. foreach ($stamps as $stamp) {
  53. $cloned->stamps[\get_class($stamp)][] = $stamp;
  54. }
  55. return $cloned;
  56. }
  57. /**
  58. * @return Envelope a new Envelope instance without any stamps of the given class
  59. */
  60. public function withoutAll(string $stampFqcn): self
  61. {
  62. $cloned = clone $this;
  63. unset($cloned->stamps[$this->resolveAlias($stampFqcn)]);
  64. return $cloned;
  65. }
  66. /**
  67. * Removes all stamps that implement the given type.
  68. */
  69. public function withoutStampsOfType(string $type): self
  70. {
  71. $cloned = clone $this;
  72. $type = $this->resolveAlias($type);
  73. foreach ($cloned->stamps as $class => $stamps) {
  74. if ($class === $type || is_subclass_of($class, $type)) {
  75. unset($cloned->stamps[$class]);
  76. }
  77. }
  78. return $cloned;
  79. }
  80. public function last(string $stampFqcn): ?StampInterface
  81. {
  82. return isset($this->stamps[$stampFqcn = $this->resolveAlias($stampFqcn)]) ? end($this->stamps[$stampFqcn]) : null;
  83. }
  84. /**
  85. * @return StampInterface[]|StampInterface[][] The stamps for the specified FQCN, or all stamps by their class name
  86. */
  87. public function all(string $stampFqcn = null): array
  88. {
  89. if (null !== $stampFqcn) {
  90. return $this->stamps[$this->resolveAlias($stampFqcn)] ?? [];
  91. }
  92. return $this->stamps;
  93. }
  94. /**
  95. * @return object The original message contained in the envelope
  96. */
  97. public function getMessage(): object
  98. {
  99. return $this->message;
  100. }
  101. /**
  102. * BC to be removed in 6.0.
  103. */
  104. private function resolveAlias(string $fqcn): string
  105. {
  106. static $resolved;
  107. return $resolved[$fqcn] ?? ($resolved[$fqcn] = (new \ReflectionClass($fqcn))->getName());
  108. }
  109. }