diff --git a/src/Controller/Site/TreeAdminController.php b/src/Controller/Site/TreeAdminController.php index bf34123..13a12cc 100644 --- a/src/Controller/Site/TreeAdminController.php +++ b/src/Controller/Site/TreeAdminController.php @@ -20,10 +20,7 @@ class TreeAdminController extends AdminController */ public function index(NavigationRepositoryQuery $navigationQuery): Response { - $navigation = $navigationQuery->create() - ->orderBy('.label') - ->findOne() - ; + $navigation = $navigationQuery->create()->findOne(); if (null === $navigation) { $this->addFlash('warning', 'Vous devez ajouter une navigation.'); @@ -44,10 +41,7 @@ class TreeAdminController extends AdminController NavigationRepositoryQuery $navigationQuery, MenuFactory $menuFactory ): Response { - $navigations = $navigationQuery->create() - ->orderBy('.label') - ->find() - ; + $navigations = $navigationQuery->create()->find(); $forms = [ 'menu' => $this->createForm(MenuType::class, $menuFactory->create())->createView(), diff --git a/src/Entity/Site/Menu.php b/src/Entity/Site/Menu.php index dea0a1d..a0d675d 100644 --- a/src/Entity/Site/Menu.php +++ b/src/Entity/Site/Menu.php @@ -41,12 +41,12 @@ class Menu implements EntityInterface private $navigation; /** - * @ORM\OneToMany(targetEntity=Node::class, mappedBy="menu", orphanRemoval=true) + * @ORM\OneToMany(targetEntity=Node::class, mappedBy="menu", orphanRemoval=true, cascade={"remove", "persist"}) */ private $nodes; /** - * @ORM\OneToOne(targetEntity=Node::class) + * @ORM\OneToOne(targetEntity=Node::class, cascade={"persist"}) * @ORM\JoinColumn(onDelete="CASCADE") */ private $rootNode; diff --git a/src/EventSuscriber/Site/MenuEventSubscriber.php b/src/EventSuscriber/Site/MenuEventSubscriber.php index ee547b3..eaa0127 100644 --- a/src/EventSuscriber/Site/MenuEventSubscriber.php +++ b/src/EventSuscriber/Site/MenuEventSubscriber.php @@ -63,7 +63,7 @@ class MenuEventSubscriber extends EntityManagerEventSubscriber } $rootNode = $this->nodeFactory->create($menu); - $childNode = $this->nodeFactory->create($menu); + $childNode = $this->nodeFactory->create($menu, '/'); $childNode ->setParent($rootNode) ->setLabel('Premier élément') @@ -71,8 +71,8 @@ class MenuEventSubscriber extends EntityManagerEventSubscriber $menu->setRootNode($rootNode); - $this->entityManager->create($rootNode); - $this->entityManager->create($childNode); + $this->entityManager->getEntityManager()->persist($rootNode); + $this->entityManager->getEntityManager()->persist($childNode); $this->entityManager->getEntityManager()->persist($menu); $this->entityManager->flush(); diff --git a/src/EventSuscriber/Site/NodeEventSubscriber.php b/src/EventSuscriber/Site/NodeEventSubscriber.php index 35deb26..ef1afbb 100644 --- a/src/EventSuscriber/Site/NodeEventSubscriber.php +++ b/src/EventSuscriber/Site/NodeEventSubscriber.php @@ -10,12 +10,7 @@ use App\Factory\Site\NodeFactory; use App\Manager\EntityManager; use App\Repository\Site\NodeRepository; use App\Slugify\Slugify; -use Symfony\Bundle\FrameworkBundle\Console\Application; -use Symfony\Component\Console\Input\ArrayInput; -use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\HttpKernel\KernelInterface; -use App\Entity\Site\Menu; -use App\Entity\Site\Navigation; /** * class NodeEventSubscriber. @@ -33,13 +28,11 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber NodeFactory $nodeFactory, NodeRepository $nodeRepository, EntityManager $entityManager, - KernelInterface $kernel, Slugify $slugify ) { $this->nodeFactory = $nodeFactory; $this->nodeRepository = $nodeRepository; $this->entityManager = $entityManager; - $this->kernel = $kernel; $this->slugify = $slugify; } @@ -90,25 +83,6 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber $node->setUrl($generatedUrl); } - public function onUpdate(EntityManagerEvent $event) - { - $entity = $event->getEntity(); - - if (!$entity instanceof Node && !$entity instanceof Menu && !$entity instanceof Navigation) { - return; - } - - $application = new Application($this->kernel); - $application->setAutoExit(false); - - $input = new ArrayInput([ - 'command' => 'cache:clear', - ]); - - $output = new BufferedOutput(); - $application->run($input, $output); - } - public function onDelete(EntityManagerEvent $event) { if (!$this->support($event->getEntity())) { diff --git a/src/EventSuscriber/Site/SiteEventSubscriber.php b/src/EventSuscriber/Site/SiteEventSubscriber.php new file mode 100644 index 0000000..3e8f232 --- /dev/null +++ b/src/EventSuscriber/Site/SiteEventSubscriber.php @@ -0,0 +1,70 @@ + + */ +class SiteEventSubscriber extends EntityManagerEventSubscriber +{ + protected KernelInterface $kernel; + + public function __construct(KernelInterface $kernel) { + $this->kernel = $kernel; + } + + public function support(EntityInterface $entity) + { + return $entity instanceof Node || $entity instanceof Menu || $entity instanceof Navigation; + } + + protected function cleanCache() + { + $application = new Application($this->kernel); + $application->setAutoExit(false); + + $input = new ArrayInput([ + 'command' => 'cache:clear', + ]); + + $output = new BufferedOutput(); + $application->run($input, $output); + } + + public function onUpdate(EntityManagerEvent $event) + { + if (!$this->support($event->getEntity())) { + return; + } + + $this->cleanCache(); + } + + public function onCreate(EntityManagerEvent $event) + { + return $this->onUpdate($event); + } + + public function onDelete(EntityManagerEvent $event) + { + return $this->onUpdate($event); + } + +} diff --git a/src/Factory/Site/NodeFactory.php b/src/Factory/Site/NodeFactory.php index adac084..9fa5fb6 100644 --- a/src/Factory/Site/NodeFactory.php +++ b/src/Factory/Site/NodeFactory.php @@ -12,7 +12,7 @@ use App\Entity\Site\Node; */ class NodeFactory { - public function create(?Menu $menu = null): Node + public function create(?Menu $menu = null, string $url = null): Node { $entity = new Node(); @@ -20,6 +20,11 @@ class NodeFactory $entity->setMenu($menu); } + if (null !== $url) { + $entity->setUrl($url); + } + + return $entity; } } diff --git a/src/Router/SiteRouteLoader.php b/src/Router/SiteRouteLoader.php index 782ca53..ff31294 100644 --- a/src/Router/SiteRouteLoader.php +++ b/src/Router/SiteRouteLoader.php @@ -35,6 +35,10 @@ class SiteRouteLoader extends Loader foreach ($navigations as $navigation) { foreach ($navigation->getMenus() as $menu) { foreach ($menu->getRootNode()->getAllChildren() as $node) { + if ($node->getParent() === null) { + continue; + } + $requirements = []; $defaults = [