update event suscribers

This commit is contained in:
Simon Vieille 2021-03-17 20:42:09 +01:00
parent 0bf7bc7789
commit 3bf4a1460e
5 changed files with 28 additions and 14 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

View File

@ -15,6 +15,9 @@ class EntityManagerEvent extends Event
const CREATE_EVENT = 'entity_manager_event.create';
const UPDATE_EVENT = 'entity_manager_event.update';
const DELETE_EVENT = 'entity_manager_event.delete';
const PRE_CREATE_EVENT = 'entity_manager_event.pre_create';
const PRE_UPDATE_EVENT = 'entity_manager_event.pre_update';
const PRE_DELETE_EVENT = 'entity_manager_event.pre_delete';
protected EntityInterface $entity;

View File

@ -15,7 +15,7 @@ use Symfony\Component\Finder\Finder;
*
* @author Simon Vieille <simon@deblan.fr>
*/
class BlogPostEventSubscriber implements EventSubscriberInterface
class BlogPostEventSubscriber extends EntityManagerEventSubscriber
{
protected Filesystem $filesystem;
protected PostRepositoryQuery $query;
@ -26,14 +26,6 @@ class BlogPostEventSubscriber implements EventSubscriberInterface
$this->query = $query;
}
public static function getSubscribedEvents()
{
return [
EntityManagerEvent::UPDATE_EVENT => 'onUpdate',
EntityManagerEvent::DELETE_EVENT => 'onDelete',
];
}
public function support(EntityInterface $entity)
{
return $entity instanceof Post;

View File

@ -10,18 +10,17 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*
* @author Simon Vieille <simon@deblan.fr>
*/
class EntityManagerEventSubscriber implements EventSubscriberInterface
abstract class EntityManagerEventSubscriber implements EventSubscriberInterface
{
public function __construct()
{
}
public static function getSubscribedEvents()
{
return [
EntityManagerEvent::CREATE_EVENT => 'onCreate',
EntityManagerEvent::UPDATE_EVENT => 'onUpdate',
EntityManagerEvent::DELETE_EVENT => 'onDelete',
EntityManagerEvent::PRE_CREATE_EVENT => 'onPreCreate',
EntityManagerEvent::PRE_UPDATE_EVENT => 'onPreUpdate',
EntityManagerEvent::PRE_DELETE_EVENT => 'onPreDelete',
];
}
@ -36,4 +35,16 @@ class EntityManagerEventSubscriber implements EventSubscriberInterface
public function onDelete(EntityManagerEvent $event)
{
}
public function onPreCreate(EntityManagerEvent $event)
{
}
public function onPreUpdate(EntityManagerEvent $event)
{
}
public function onPreDelete(EntityManagerEvent $event)
{
}
}

View File

@ -27,7 +27,10 @@ class EntityManager
public function create(EntityInterface $entity): self
{
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT);
$this->persist($entity);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
return $this;
@ -35,7 +38,10 @@ class EntityManager
public function update(EntityInterface $entity): self
{
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT);
$this->persist($entity);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT);
return $this;
@ -43,6 +49,8 @@ class EntityManager
public function delete(EntityInterface $entity): self
{
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT);
$this->entityManager->remove($entity);
$this->flush();