-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Support configuring messages when dispatching #26945
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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
/** | ||
* A message wrapped in an envelope with items (configurations, markers, ...). | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
* | ||
* @experimental in 4.1 | ||
*/ | ||
final class Envelope | ||
{ | ||
private $items = array(); | ||
private $message; | ||
|
||
/** | ||
* @param object $message | ||
* @param EnvelopeItemInterface[] $items | ||
*/ | ||
public function __construct($message, array $items = array()) | ||
{ | ||
$this->message = $message; | ||
foreach ($items as $item) { | ||
$this->items[\get_class($item)] = $item; | ||
} | ||
} | ||
|
||
/** | ||
* Wrap a message into an envelope if not already wrapped. | ||
* | ||
* @param Envelope|object $message | ||
*/ | ||
public static function wrap($message): self | ||
{ | ||
return $message instanceof self ? $message : new self($message); | ||
} | ||
|
||
/** | ||
* @return Envelope a new Envelope instance with additional item | ||
*/ | ||
public function with(EnvelopeItemInterface $item): self | ||
{ | ||
$cloned = clone $this; | ||
|
||
$cloned->items[\get_class($item)] = $item; | ||
|
||
return $cloned; | ||
} | ||
|
||
public function get(string $itemFqcn): ?EnvelopeItemInterface | ||
{ | ||
return $this->items[$itemFqcn] ?? null; | ||
} | ||
|
||
/** | ||
* @return EnvelopeItemInterface[] indexed by fqcn | ||
*/ | ||
public function all(): array | ||
{ | ||
return $this->items; | ||
} | ||
|
||
/** | ||
* @return object The original message contained in the envelope | ||
*/ | ||
public function getMessage() | ||
{ | ||
return $this->message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
/** | ||
* A Messenger protagonist aware of the message envelope and its content. | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
* | ||
* @experimental in 4.1 | ||
*/ | ||
interface EnvelopeAwareInterface | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger; | ||
|
||
/** | ||
* An envelope item related to a message. | ||
* This item must be serializable for transport. | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
* | ||
* @experimental in 4.1 | ||
*/ | ||
interface EnvelopeItemInterface extends \Serializable | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ interface MessageBusInterface | |
* | ||
* The bus can return a value coming from handlers, but is not required to do so. | ||
* | ||
* @param object $message | ||
* @param object|Envelope $message The message or the message pre-wrapped in an envelope | ||
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. kinda an implementation detail.. 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. No. That indicates you can dispatch a configured message and the bus has to handle it, not just the message itself. 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. It eventually depends on the implementation :) the pure API, IMHO, is 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. Ok. Never mind :) you're right. For any SF bus implementation this is true. You always need to anticipate the Envelope case. |
||
* | ||
* @return mixed | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Middleware\Configuration; | ||
|
||
use Symfony\Component\Messenger\EnvelopeItemInterface; | ||
use Symfony\Component\Validator\Constraints\GroupSequence; | ||
|
||
/** | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
* | ||
* @experimental in 4.1 | ||
*/ | ||
final class ValidationConfiguration implements EnvelopeItemInterface | ||
{ | ||
private $groups; | ||
|
||
/** | ||
* @param string[]|GroupSequence $groups | ||
*/ | ||
public function __construct($groups) | ||
{ | ||
$this->groups = $groups; | ||
} | ||
|
||
public function getGroups() | ||
{ | ||
return $this->groups; | ||
} | ||
|
||
public function serialize() | ||
{ | ||
$isGroupSequence = $this->groups instanceof GroupSequence; | ||
|
||
return serialize(array( | ||
'groups' => $isGroupSequence ? $this->groups->groups : $this->groups, | ||
'is_group_sequence' => $isGroupSequence, | ||
)); | ||
} | ||
|
||
public function unserialize($serialized) | ||
{ | ||
list( | ||
'groups' => $groups, | ||
'is_group_sequence' => $isGroupSequence | ||
) = unserialize($serialized, array('allowed_classes' => false)); | ||
|
||
$this->__construct($isGroupSequence ? new GroupSequence($groups) : $groups); | ||
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.
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. Again.. never mind :) makes sense to call __construct and mimic default initialization |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about a new
has()
method for these cases?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not deemed necessary to me, but I'm willing to add it if there are other opinions in this direction :)