-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Add a new Messenger component #24411
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
87 changes: 87 additions & 0 deletions
87
src/Symfony/Bundle/FrameworkBundle/Command/MessengerConsumeMessagesCommand.php
This file contains hidden or 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,87 @@ | ||
<?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\Bundle\FrameworkBundle\Command; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Transport\Enhancers\MaximumCountReceiver; | ||
use Symfony\Component\Messenger\Transport\ReceiverInterface; | ||
use Symfony\Component\Messenger\Worker; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class MessengerConsumeMessagesCommand extends Command | ||
{ | ||
protected static $defaultName = 'messenger:consume-messages'; | ||
|
||
private $bus; | ||
private $receiverLocator; | ||
|
||
public function __construct(MessageBusInterface $bus, ContainerInterface $receiverLocator) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->bus = $bus; | ||
$this->receiverLocator = $receiverLocator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setDefinition(array( | ||
new InputArgument('receiver', InputArgument::REQUIRED, 'Name of the receiver'), | ||
new InputOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Limit the number of received messages'), | ||
)) | ||
->setDescription('Consumes messages') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command consumes messages and dispatches them to the message bus. | ||
|
||
<info>php %command.full_name% <receiver-name></info> | ||
|
||
Use the --limit option to limit the number of messages received: | ||
|
||
<info>php %command.full_name% <receiver-name> --limit=10</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if (!$this->receiverLocator->has($receiverName = $input->getArgument('receiver'))) { | ||
throw new \RuntimeException(sprintf('Receiver "%s" does not exist.', $receiverName)); | ||
} | ||
|
||
if (!($receiver = $this->receiverLocator->get($receiverName)) instanceof ReceiverInterface) { | ||
throw new \RuntimeException(sprintf('Receiver "%s" is not a valid message consumer. It must implement the "%s" interface.', $receiverName, ReceiverInterface::class)); | ||
} | ||
|
||
if ($limit = $input->getOption('limit')) { | ||
$receiver = new MaximumCountReceiver($receiver, $limit); | ||
} | ||
|
||
$worker = new Worker($receiver, $this->bus); | ||
$worker->run(); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -382,4 +382,20 @@ protected function isCsrfTokenValid(string $id, ?string $token): bool | |
|
||
return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token)); | ||
} | ||
|
||
/** | ||
* Dispatches a message to the bus. | ||
* | ||
* @param object $message The message to dispatch | ||
* | ||
* @final | ||
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 is an annotation |
||
*/ | ||
protected function dispatchMessage($message) | ||
{ | ||
if (!$this->container->has('message_bus')) { | ||
throw new \LogicException('The message bus is not enabled in your application. Try running "composer require symfony/messenger".'); | ||
} | ||
|
||
return $this->container->get('message_bus')->dispatch($message); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/Symfony/Bundle/FrameworkBundle/DataCollector/MessengerDataCollector.php
This file contains hidden or 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,93 @@ | ||
<?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\Bundle\FrameworkBundle\DataCollector; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\DataCollector\DataCollector; | ||
use Symfony\Component\Messenger\MiddlewareInterface; | ||
|
||
/** | ||
* @author Samuel Roze <samuel.roze@gmail.com> | ||
*/ | ||
class MessengerDataCollector extends DataCollector implements MiddlewareInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function collect(Request $request, Response $response, \Exception $exception = null) | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'messages'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function reset() | ||
{ | ||
$this->data = array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle($message, callable $next) | ||
{ | ||
$debugRepresentation = array( | ||
'message' => array( | ||
'type' => get_class($message), | ||
), | ||
); | ||
|
||
$exception = null; | ||
try { | ||
$result = $next($message); | ||
|
||
if (is_object($result)) { | ||
$debugRepresentation['result'] = array( | ||
'type' => get_class($result), | ||
); | ||
} else { | ||
$debugRepresentation['result'] = array( | ||
'type' => gettype($result), | ||
'value' => $result, | ||
); | ||
} | ||
} catch (\Throwable $exception) { | ||
$debugRepresentation['exception'] = array( | ||
'type' => get_class($exception), | ||
'message' => $exception->getMessage(), | ||
); | ||
} | ||
|
||
$this->data[] = $debugRepresentation; | ||
|
||
if (null !== $exception) { | ||
throw $exception; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function getMessages(): array | ||
{ | ||
return $this->data; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why? It looks like this service is used nowhere in the trait.
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.
So you can do a
$this->get('message_bus')
within your controllers.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.
I don't think that we do that for any other service, nor that we want to promote that (we prefer to promote regular injection since 4.0). Other services are injected here mostly for backward compatibility (when using the proxy methods).
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.
However, maybe can you add a proxy method in the trait, for convenience and discoverability by newcomers.
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.
Added the
dispatchMessage
method on theControllerTrait
in 0a31079.