-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #30958 [Messenger] Allows to register handlers on a specific …
…transport (sroze) This PR was merged into the 4.3-dev branch. Discussion ---------- [Messenger] Allows to register handlers on a specific transport | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30110 | License | MIT | Doc PR | symfony/symfony-docs#11236 With the #30008 pull-request in, we can now do the following: ```yaml framework: messenger: transports: events: dsn: amqp://guest:guest@127.0.0.1/%2f/events options: queues: 3rdparty: binding_keys: [ 3rdparty ] projection: binding_keys: [ projection ] events_3rdparty: amqp://guest:guest@127.0.0.1/%2f/events?queues[3rdparty] events_projection: amqp://guest:guest@127.0.0.1/%2f/events?queues[projection] routing: 'App\Message\RegisterBet': events ``` This will bind two queues to the `events` exchange, fantastic: <img width="325" alt="Screenshot 2019-04-07 at 10 26 27" src="https://user-images.githubusercontent.com/804625/55680861-af373580-591f-11e9-8f1e-2d3b6ddba2fd.png"> --- Now, in this setup, the message will be duplicated within the `3rdparty` & `projection` queues. If you just run the consumer for each transport, it will consume the message and call all the handlers. You can't do different things based on which queue you have consumed the message. **This pull-request adds the following feature:** ```php class RegisterBetHandler implements MessageSubscriberInterface { public function __invoke(RegisterBet $message) { // Do something only when the message comes from the `events_projection` transport... } /** * {@inheritdoc} */ public static function getHandledMessages(): iterable { yield RegisterBet::class => [ 'from_transport' => 'events_projection', ]; } } ``` --- In the debugger, it looks like this: <img width="649" alt="Screenshot 2019-04-07 at 10 29 55" src="https://user-images.githubusercontent.com/804625/55680892-1d7bf800-5920-11e9-80f7-853f0201c6d8.png"> Commits ------- f0b2acd Allows to register handlers on a specific transport (and get rid of this handler alias)
- Loading branch information
Showing
23 changed files
with
426 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Symfony/Component/Messenger/Handler/HandlerDescriptor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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\Handler; | ||
|
||
/** | ||
* Describes a handler and the possible associated options, such as `from_transport`, `bus`, etc. | ||
* | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
* | ||
* @experimental in 4.3 | ||
*/ | ||
final class HandlerDescriptor | ||
{ | ||
private $handler; | ||
private $options; | ||
|
||
public function __construct(callable $handler, array $options = []) | ||
{ | ||
$this->handler = $handler; | ||
$this->options = $options; | ||
} | ||
|
||
public function getHandler(): callable | ||
{ | ||
return $this->handler; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
$name = $this->callableName($this->handler); | ||
$alias = $this->options['alias'] ?? null; | ||
|
||
if (null !== $alias) { | ||
$name .= '@'.$alias; | ||
} | ||
|
||
return $name; | ||
} | ||
|
||
public function getOption(string $option) | ||
{ | ||
return $this->options[$option] ?? null; | ||
} | ||
|
||
private function callableName(callable $handler) | ||
{ | ||
if (\is_array($handler)) { | ||
if (\is_object($handler[0])) { | ||
return \get_class($handler[0]).'::'.$handler[1]; | ||
} | ||
|
||
return $handler[0].'::'.$handler[1]; | ||
} | ||
|
||
if (\is_string($handler)) { | ||
return $handler; | ||
} | ||
|
||
if ($handler instanceof \Closure) { | ||
$r = new \ReflectionFunction($handler); | ||
if (false !== strpos($r->name, '{closure}')) { | ||
return 'Closure'; | ||
} | ||
if ($class = $r->getClosureScopeClass()) { | ||
return $class->name.'::'.$r->name; | ||
} | ||
|
||
return $r->name; | ||
} | ||
|
||
return \get_class($handler).'::__invoke'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.