<?php
declare(strict_types=1);
namespace App\Menu;
use App\Entity\Configurator\Model;
use App\Events\ModelMenuBuilderEvent;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
final class ModelFormMenuBuilder
{
public const EVENT_NAME = 'app.menu.admin.library.form';
/** @var FactoryInterface */
private $factory;
/** @var EventDispatcherInterface */
private $eventDispatcher;
public function __construct(FactoryInterface $factory, EventDispatcherInterface $eventDispatcher)
{
$this->factory = $factory;
if (class_exists('Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy')) {
/**
* It could return null only if we pass null, but we pass not null in any case
*
* @var ContractsEventDispatcherInterface
*/
$eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
}
$this->eventDispatcher = $eventDispatcher;
}
public function createMenu(array $options = []): ItemInterface
{
$menu = $this->factory->createItem('root');
if (!array_key_exists('model', $options) || !$options['model'] instanceof Model) {
return $menu;
}
$menu
->addChild('details')
->setAttribute('template', '@SyliusAdmin/Configurator/Model/Tab/_details.html.twig')
->setLabel('sylius.ui.details')
->setCurrent(true)
;
$menu
->addChild('configurator')
->setAttribute('template', '@SyliusAdmin/Configurator/Model/Tab/_configurator.html.twig')
->setLabel('app.form.configurator.configurator')
;
if (class_exists('Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy')) {
$this->eventDispatcher->dispatch(
new ModelMenuBuilderEvent($this->factory, $menu, $options['model']),
self::EVENT_NAME
);
} else {
$this->eventDispatcher->dispatch(
self::EVENT_NAME,
new ModelMenuBuilderEvent($this->factory, $menu, $options['model'])
);
}
return $menu;
}
}