src/Menu/ModelFormMenuBuilder.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Menu;
  4. use App\Entity\Configurator\Model;
  5. use App\Events\ModelMenuBuilderEvent;
  6. use Knp\Menu\FactoryInterface;
  7. use Knp\Menu\ItemInterface;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
  11. final class ModelFormMenuBuilder
  12. {
  13.     public const EVENT_NAME 'app.menu.admin.library.form';
  14.     /** @var FactoryInterface */
  15.     private $factory;
  16.     /** @var EventDispatcherInterface */
  17.     private $eventDispatcher;
  18.     public function __construct(FactoryInterface $factoryEventDispatcherInterface $eventDispatcher)
  19.     {
  20.         $this->factory $factory;
  21.         if (class_exists('Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy')) {
  22.             /**
  23.              * It could return null only if we pass null, but we pass not null in any case
  24.              *
  25.              * @var ContractsEventDispatcherInterface
  26.              */
  27.             $eventDispatcher LegacyEventDispatcherProxy::decorate($eventDispatcher);
  28.         }
  29.         $this->eventDispatcher $eventDispatcher;
  30.     }
  31.     public function createMenu(array $options = []): ItemInterface
  32.     {
  33.         $menu $this->factory->createItem('root');
  34.         if (!array_key_exists('model'$options) || !$options['model'] instanceof Model) {
  35.             return $menu;
  36.         }
  37.         $menu
  38.             ->addChild('details')
  39.             ->setAttribute('template''@SyliusAdmin/Configurator/Model/Tab/_details.html.twig')
  40.             ->setLabel('sylius.ui.details')
  41.             ->setCurrent(true)
  42.         ;
  43.         $menu
  44.             ->addChild('configurator')
  45.             ->setAttribute('template''@SyliusAdmin/Configurator/Model/Tab/_configurator.html.twig')
  46.             ->setLabel('app.form.configurator.configurator')
  47.         ;
  48.         if (class_exists('Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy')) {
  49.             $this->eventDispatcher->dispatch(
  50.                 new ModelMenuBuilderEvent($this->factory$menu$options['model']),
  51.                 self::EVENT_NAME
  52.             );
  53.         } else {
  54.             $this->eventDispatcher->dispatch(
  55.                 self::EVENT_NAME,
  56.                 new ModelMenuBuilderEvent($this->factory$menu$options['model'])
  57.             );
  58.         }
  59.         return $menu;
  60.     }
  61. }