src/EventSubscriber/SonataAdmin/AbstractAdminSubscriber.php line 101

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\SonataAdmin;
  3. use App\Contract\EventSubscriber\SonataAdmin\ConfigureDataGridInterface;
  4. use App\Contract\EventSubscriber\SonataAdmin\ConfigureFormInterface;
  5. use App\Contract\EventSubscriber\SonataAdmin\ConfigureListInterface;
  6. use App\Contract\EventSubscriber\SonataAdmin\ConfigureShowInterface;
  7. use App\Contract\EventSubscriber\SonataAdmin\PostPersistInterface;
  8. use App\Contract\EventSubscriber\SonataAdmin\PostRemoveInterface;
  9. use App\Contract\EventSubscriber\SonataAdmin\PostUpdateInterface;
  10. use App\Contract\EventSubscriber\SonataAdmin\PrePersistInterface;
  11. use App\Contract\EventSubscriber\SonataAdmin\PreRemoveInterface;
  12. use App\Contract\EventSubscriber\SonataAdmin\PreUpdateInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Sonata\AdminBundle\Event\ConfigureEvent;
  15. use Sonata\AdminBundle\Event\PersistenceEvent;
  16. use Sonata\AdminBundle\Datagrid\ListMapper;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19.  * Class AbstractAdminSubscriber.
  20.  *
  21.  * @method void configureForm(AdminInterface $admin, FormMapper $formMapper): void
  22.  * @method void configureList(AdminInterface $admin, ListMapper $listMapper): void
  23.  * @method void configureDataGrid(AdminInterface $admin, DatagridMapper $dataGridMapper): void
  24.  * @method void configureShow(AdminInterface $admin, ShowMapper $showMapper): void
  25.  * @method void preUpdate(AdminInterface $admin, object $entity): void
  26.  * @method void postUpdate(AdminInterface $admin, object $entity): void
  27.  * @method void prePersist(AdminInterface $admin, object $entity): void
  28.  * @method void postPersist(AdminInterface $admin, object $entity): void
  29.  * @method void preRemove(AdminInterface $admin, object $entity): void
  30.  * @method void postRemove(AdminInterface $admin, object $entity): void
  31.  */
  32. abstract class AbstractAdminSubscriber implements EventSubscriberInterface
  33. {
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             'sonata.admin.event.configure.form' => 'onConfigureForm',
  38.             'sonata.admin.event.configure.list' => 'onConfigureList',
  39.             'sonata.admin.event.configure.show' => 'onConfigureShow',
  40.             'sonata.admin.event.configure.datagrid' => 'onConfigureDataGrid',
  41.             'sonata.admin.event.persistence.pre_update' => 'onPreUpdate',
  42.             'sonata.admin.event.persistence.post_update' => 'onPostUpdate',
  43.             'sonata.admin.event.persistence.pre_persist' => 'onPrePersist',
  44.             'sonata.admin.event.persistence.post_persist' => 'onPostPersist',
  45.             'sonata.admin.event.persistence.pre_remove' => 'onPreRemove',
  46.             'sonata.admin.event.persistence.post_remove' => 'onPostRemove',
  47.         ];
  48.     }
  49.     /**
  50.      * Helper method to indicate whether or not a subscriber callback should be invoked.
  51.      *
  52.      * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  53.      *   The instance of the currently loaded admin
  54.      *
  55.      * @return bool
  56.      *   The result check
  57.      */
  58.     abstract public function supports(AdminInterface $admin): bool;
  59.     /**
  60.      * Responsible for providing form configuration.
  61.      *
  62.      * @param \Sonata\AdminBundle\Event\ConfigureEvent $event
  63.      *   The instance of the dispatched event
  64.      */
  65.     final public function onConfigureForm(ConfigureEvent $event): void
  66.     {
  67.         if (!$this instanceof ConfigureFormInterface || !$this->supports($event->getAdmin())) {
  68.             return;
  69.         }
  70.         $this->configureForm($event->getAdmin(), $event->getMapper());
  71.     }
  72.     /**
  73.      * Responsible for providing list configuration.
  74.      *
  75.      * @param \Sonata\AdminBundle\Event\ConfigureEvent $event
  76.      *   The instance of the dispatched event
  77.      */
  78.     final public function onConfigureList(ConfigureEvent $event): void
  79.     {
  80.         if (!$this instanceof ConfigureListInterface || !$this->supports($event->getAdmin())) {
  81.             return;
  82.         }
  83.         $this->configureList($event->getAdmin(), $event->getMapper());
  84.     }
  85.     /**
  86.      * Responsible for providing data grid configuration.
  87.      *
  88.      * @param \Sonata\AdminBundle\Event\ConfigureEvent $event
  89.      *   The instance of the dispatched event
  90.      */
  91.     final public function onConfigureDataGrid(ConfigureEvent $event): void
  92.     {
  93.         if (!$this instanceof ConfigureDataGridInterface || !$this->supports($event->getAdmin())) {
  94.             return;
  95.         }
  96.         $this->configureDataGrid($event->getAdmin(), $event->getMapper());
  97.     }
  98.     /**
  99.      * Responsible for providing entity show configuration.
  100.      *
  101.      * @param \Sonata\AdminBundle\Event\ConfigureEvent $event
  102.      *   The instance of the dispatched event
  103.      */
  104.     final public function onConfigureShow(ConfigureEvent $event): void
  105.     {
  106.         if (!$this instanceof ConfigureShowInterface || !$this->supports($event->getAdmin())) {
  107.             return;
  108.         }
  109.         $this->configureShow($event->getAdmin(), $event->getMapper());
  110.     }
  111.     /**
  112.      * Responsible for providing a hook right before an entity is updated.
  113.      *
  114.      * @param \Sonata\AdminBundle\Event\PersistenceEvent $event
  115.      *   The instance of the dispatched event
  116.      */
  117.     final public function onPreUpdate(PersistenceEvent $event): void
  118.     {
  119.         if (!$this instanceof PreUpdateInterface || !$this->supports($event->getAdmin())) {
  120.             return;
  121.         }
  122.         $this->preUpdate($event->getAdmin(), $event->getObject());
  123.     }
  124.     /**
  125.      * Responsible for providing a hook right after an entity is updated.
  126.      *
  127.      * @param \Sonata\AdminBundle\Event\PersistenceEvent $event
  128.      *   The instance of the dispatched event
  129.      */
  130.     final public function onPostUpdate(PersistenceEvent $event): void
  131.     {
  132.         if (!$this instanceof PostUpdateInterface || !$this->supports($event->getAdmin())) {
  133.             return;
  134.         }
  135.         $this->postUpdate($event->getAdmin(), $event->getObject());
  136.     }
  137.     /**
  138.      * Responsible for providing a hook right before an entity is created.
  139.      *
  140.      * @param \Sonata\AdminBundle\Event\PersistenceEvent $event
  141.      *   The instance of the dispatched event
  142.      */
  143.     final public function onPrePersist(PersistenceEvent $event): void
  144.     {
  145.         if (!$this instanceof PrePersistInterface || !$this->supports($event->getAdmin())) {
  146.             return;
  147.         }
  148.         $this->prePersist($event->getAdmin(), $event->getObject());
  149.     }
  150.     /**
  151.      * Responsible for providing a hook right after an entity is created.
  152.      *
  153.      * @param \Sonata\AdminBundle\Event\PersistenceEvent $event
  154.      *   The instance of the dispatched event
  155.      */
  156.     final public function onPostPersist(PersistenceEvent $event): void
  157.     {
  158.         if (!$this instanceof PostPersistInterface || !$this->supports($event->getAdmin())) {
  159.             return;
  160.         }
  161.         $this->postPersist($event->getAdmin(), $event->getObject());
  162.     }
  163.     /**
  164.      * Responsible for providing a hook right before an entity is removed.
  165.      *
  166.      * @param \Sonata\AdminBundle\Event\PersistenceEvent $event
  167.      *   The instance of the dispatched event
  168.      */
  169.     final public function onPreRemove(PersistenceEvent $event): void
  170.     {
  171.         if (!$this instanceof PreRemoveInterface || !$this->supports($event->getAdmin())) {
  172.             return;
  173.         }
  174.         $this->preRemove($event->getAdmin(), $event->getObject());
  175.     }
  176.     /**
  177.      * Responsible for providing a hook right after an entity is removed.
  178.      *
  179.      * @param \Sonata\AdminBundle\Event\PersistenceEvent $event
  180.      *   The instance of the dispatched event
  181.      */
  182.     final public function onPostRemove(PersistenceEvent $event): void
  183.     {
  184.         if (!$this instanceof PostRemoveInterface || !$this->supports($event->getAdmin())) {
  185.             return;
  186.         }
  187.         $this->postRemove($event->getAdmin(), $event->getObject());
  188.     }
  189. }