Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
sroze marked this conversation as resolved.
Show resolved Hide resolved
*/
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will fail.
The postFlush event is not a lifecycle event. It passes an instance of Doctrine\ORM\Event\PostFlushEventArgs which is not a subclass of LifecycleEventArgs. See https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#postflush .

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the method is called collectEventsFromEntity, then it should accept object $entity as argument?

{
$entity = $message->getEntity();

if ($entity instanceof EntityMessageCollectionInterface) {
foreach ($entity->getRecordedMessages() as $message) {
$this->messageBus->dispatch($message, [new DispatchAfterCurrentBusStamp()]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the message bus throws an exception?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will bubble up. But the Doctrine transaction will still be committed.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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();
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.