-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Add support for "recording" events from entities #28850
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,31 @@ | ||
<?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\Bridge\Doctrine\Messenger\EntityMessage; | ||
|
||
/** | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
interface EntityMessageCollectionInterface | ||
{ | ||
/** | ||
* Fetch recorded messages. | ||
* | ||
* @return object[] | ||
*/ | ||
public function getRecordedMessages(): array; | ||
|
||
/** | ||
* Remove all recorded messages. | ||
*/ | ||
public function resetRecordedMessages(): void; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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\Bridge\Doctrine\Messenger\EntityMessage; | ||
|
||
/** | ||
* Use this trait in classes which implement EntityMessageCollectionInterface | ||
* to privately record and later release Message instances, like events. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
trait MessageRecorderTrait | ||
{ | ||
private $messages = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getRecordedMessages(): array | ||
{ | ||
return $this->messages; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resetRecordedMessages(): void | ||
{ | ||
$this->messages = []; | ||
} | ||
|
||
/** | ||
* Record a message. | ||
* | ||
* @param object $message | ||
*/ | ||
private function record($message): void | ||
{ | ||
$this->messages[] = $message; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?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\Bridge\Doctrine\Messenger\EventSubscriber; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
use Doctrine\ORM\Event\LifecycleEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Symfony\Bridge\Doctrine\Messenger\EntityMessage\EntityMessageCollectionInterface; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp; | ||
|
||
/** | ||
* Doctrine listener that listens to Persist, Update and Remove. Every time this is | ||
* invoked we take messages from the entities. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
* @author Matthias Noback <matthiasnoback@gmail.com> | ||
*/ | ||
class EntityMessageCollector implements EventSubscriber | ||
{ | ||
private $messageBus; | ||
|
||
public function __construct(MessageBusInterface $messageBus) | ||
{ | ||
$this->messageBus = $messageBus; | ||
} | ||
|
||
public function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::postFlush, | ||
]; | ||
} | ||
|
||
public function postFlush(LifecycleEventArgs $event) | ||
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. This code will fail. 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. So this event cannot be used here at all, because at this point you do not know which entities were flushed. |
||
{ | ||
$this->collectEventsFromEntity($event); | ||
} | ||
|
||
private function collectEventsFromEntity(LifecycleEventArgs $message) | ||
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. if the method is called |
||
{ | ||
$entity = $message->getEntity(); | ||
|
||
if ($entity instanceof EntityMessageCollectionInterface) { | ||
foreach ($entity->getRecordedMessages() as $message) { | ||
$this->messageBus->dispatch($message, [new DispatchAfterCurrentBusStamp()]); | ||
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. What if the message bus throws an exception? 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. That will bubble up. But the Doctrine transaction will still be committed. 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. So there's a risk some messages will be dispatched while some won't. Not sure if that's an issue yet, just pointing it out. 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. Yes, If we dispatch 5 messages and number 2 fails, then the 3 other will never be handled. But that is an issue with the message bus and has nothing to do with this PR. |
||
} | ||
|
||
$entity->resetRecordedMessages(); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.