<?php
namespace App\EventSubscriber\SonataAdmin;
use App\Contract\EventSubscriber\SonataAdmin\ConfigureDataGridInterface;
use App\Contract\EventSubscriber\SonataAdmin\ConfigureFormInterface;
use App\Contract\EventSubscriber\SonataAdmin\ConfigureListInterface;
use App\Contract\EventSubscriber\SonataAdmin\ConfigureShowInterface;
use App\Contract\EventSubscriber\SonataAdmin\PostPersistInterface;
use App\Contract\EventSubscriber\SonataAdmin\PostRemoveInterface;
use App\Contract\EventSubscriber\SonataAdmin\PostUpdateInterface;
use App\Contract\EventSubscriber\SonataAdmin\PrePersistInterface;
use App\Contract\EventSubscriber\SonataAdmin\PreRemoveInterface;
use App\Contract\EventSubscriber\SonataAdmin\PreUpdateInterface;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Event\ConfigureEvent;
use Sonata\AdminBundle\Event\PersistenceEvent;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class AbstractAdminSubscriber.
*
* @method void configureForm(AdminInterface $admin, FormMapper $formMapper): void
* @method void configureList(AdminInterface $admin, ListMapper $listMapper): void
* @method void configureDataGrid(AdminInterface $admin, DatagridMapper $dataGridMapper): void
* @method void configureShow(AdminInterface $admin, ShowMapper $showMapper): void
* @method void preUpdate(AdminInterface $admin, object $entity): void
* @method void postUpdate(AdminInterface $admin, object $entity): void
* @method void prePersist(AdminInterface $admin, object $entity): void
* @method void postPersist(AdminInterface $admin, object $entity): void
* @method void preRemove(AdminInterface $admin, object $entity): void
* @method void postRemove(AdminInterface $admin, object $entity): void
*/
abstract class AbstractAdminSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'sonata.admin.event.configure.form' => 'onConfigureForm',
'sonata.admin.event.configure.list' => 'onConfigureList',
'sonata.admin.event.configure.show' => 'onConfigureShow',
'sonata.admin.event.configure.datagrid' => 'onConfigureDataGrid',
'sonata.admin.event.persistence.pre_update' => 'onPreUpdate',
'sonata.admin.event.persistence.post_update' => 'onPostUpdate',
'sonata.admin.event.persistence.pre_persist' => 'onPrePersist',
'sonata.admin.event.persistence.post_persist' => 'onPostPersist',
'sonata.admin.event.persistence.pre_remove' => 'onPreRemove',
'sonata.admin.event.persistence.post_remove' => 'onPostRemove',
];
}
/**
* Helper method to indicate whether or not a subscriber callback should be invoked.
*
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
* The instance of the currently loaded admin
*
* @return bool
* The result check
*/
abstract public function supports(AdminInterface $admin): bool;
/**
* Responsible for providing form configuration.
*
* @param \Sonata\AdminBundle\Event\ConfigureEvent $event
* The instance of the dispatched event
*/
final public function onConfigureForm(ConfigureEvent $event): void
{
if (!$this instanceof ConfigureFormInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->configureForm($event->getAdmin(), $event->getMapper());
}
/**
* Responsible for providing list configuration.
*
* @param \Sonata\AdminBundle\Event\ConfigureEvent $event
* The instance of the dispatched event
*/
final public function onConfigureList(ConfigureEvent $event): void
{
if (!$this instanceof ConfigureListInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->configureList($event->getAdmin(), $event->getMapper());
}
/**
* Responsible for providing data grid configuration.
*
* @param \Sonata\AdminBundle\Event\ConfigureEvent $event
* The instance of the dispatched event
*/
final public function onConfigureDataGrid(ConfigureEvent $event): void
{
if (!$this instanceof ConfigureDataGridInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->configureDataGrid($event->getAdmin(), $event->getMapper());
}
/**
* Responsible for providing entity show configuration.
*
* @param \Sonata\AdminBundle\Event\ConfigureEvent $event
* The instance of the dispatched event
*/
final public function onConfigureShow(ConfigureEvent $event): void
{
if (!$this instanceof ConfigureShowInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->configureShow($event->getAdmin(), $event->getMapper());
}
/**
* Responsible for providing a hook right before an entity is updated.
*
* @param \Sonata\AdminBundle\Event\PersistenceEvent $event
* The instance of the dispatched event
*/
final public function onPreUpdate(PersistenceEvent $event): void
{
if (!$this instanceof PreUpdateInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->preUpdate($event->getAdmin(), $event->getObject());
}
/**
* Responsible for providing a hook right after an entity is updated.
*
* @param \Sonata\AdminBundle\Event\PersistenceEvent $event
* The instance of the dispatched event
*/
final public function onPostUpdate(PersistenceEvent $event): void
{
if (!$this instanceof PostUpdateInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->postUpdate($event->getAdmin(), $event->getObject());
}
/**
* Responsible for providing a hook right before an entity is created.
*
* @param \Sonata\AdminBundle\Event\PersistenceEvent $event
* The instance of the dispatched event
*/
final public function onPrePersist(PersistenceEvent $event): void
{
if (!$this instanceof PrePersistInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->prePersist($event->getAdmin(), $event->getObject());
}
/**
* Responsible for providing a hook right after an entity is created.
*
* @param \Sonata\AdminBundle\Event\PersistenceEvent $event
* The instance of the dispatched event
*/
final public function onPostPersist(PersistenceEvent $event): void
{
if (!$this instanceof PostPersistInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->postPersist($event->getAdmin(), $event->getObject());
}
/**
* Responsible for providing a hook right before an entity is removed.
*
* @param \Sonata\AdminBundle\Event\PersistenceEvent $event
* The instance of the dispatched event
*/
final public function onPreRemove(PersistenceEvent $event): void
{
if (!$this instanceof PreRemoveInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->preRemove($event->getAdmin(), $event->getObject());
}
/**
* Responsible for providing a hook right after an entity is removed.
*
* @param \Sonata\AdminBundle\Event\PersistenceEvent $event
* The instance of the dispatched event
*/
final public function onPostRemove(PersistenceEvent $event): void
{
if (!$this instanceof PostRemoveInterface || !$this->supports($event->getAdmin())) {
return;
}
$this->postRemove($event->getAdmin(), $event->getObject());
}
}