fix issue with node url

This commit is contained in:
Simon Vieille 2021-03-19 17:26:36 +01:00
parent 3ded5260fa
commit 383c6f41e2
7 changed files with 87 additions and 40 deletions

View file

@ -20,10 +20,7 @@ class TreeAdminController extends AdminController
*/ */
public function index(NavigationRepositoryQuery $navigationQuery): Response public function index(NavigationRepositoryQuery $navigationQuery): Response
{ {
$navigation = $navigationQuery->create() $navigation = $navigationQuery->create()->findOne();
->orderBy('.label')
->findOne()
;
if (null === $navigation) { if (null === $navigation) {
$this->addFlash('warning', 'Vous devez ajouter une navigation.'); $this->addFlash('warning', 'Vous devez ajouter une navigation.');
@ -44,10 +41,7 @@ class TreeAdminController extends AdminController
NavigationRepositoryQuery $navigationQuery, NavigationRepositoryQuery $navigationQuery,
MenuFactory $menuFactory MenuFactory $menuFactory
): Response { ): Response {
$navigations = $navigationQuery->create() $navigations = $navigationQuery->create()->find();
->orderBy('.label')
->find()
;
$forms = [ $forms = [
'menu' => $this->createForm(MenuType::class, $menuFactory->create())->createView(), 'menu' => $this->createForm(MenuType::class, $menuFactory->create())->createView(),

View file

@ -41,12 +41,12 @@ class Menu implements EntityInterface
private $navigation; 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; private $nodes;
/** /**
* @ORM\OneToOne(targetEntity=Node::class) * @ORM\OneToOne(targetEntity=Node::class, cascade={"persist"})
* @ORM\JoinColumn(onDelete="CASCADE") * @ORM\JoinColumn(onDelete="CASCADE")
*/ */
private $rootNode; private $rootNode;

View file

@ -63,7 +63,7 @@ class MenuEventSubscriber extends EntityManagerEventSubscriber
} }
$rootNode = $this->nodeFactory->create($menu); $rootNode = $this->nodeFactory->create($menu);
$childNode = $this->nodeFactory->create($menu); $childNode = $this->nodeFactory->create($menu, '/');
$childNode $childNode
->setParent($rootNode) ->setParent($rootNode)
->setLabel('Premier élément') ->setLabel('Premier élément')
@ -71,8 +71,8 @@ class MenuEventSubscriber extends EntityManagerEventSubscriber
$menu->setRootNode($rootNode); $menu->setRootNode($rootNode);
$this->entityManager->create($rootNode); $this->entityManager->getEntityManager()->persist($rootNode);
$this->entityManager->create($childNode); $this->entityManager->getEntityManager()->persist($childNode);
$this->entityManager->getEntityManager()->persist($menu); $this->entityManager->getEntityManager()->persist($menu);
$this->entityManager->flush(); $this->entityManager->flush();

View file

@ -10,12 +10,7 @@ use App\Factory\Site\NodeFactory;
use App\Manager\EntityManager; use App\Manager\EntityManager;
use App\Repository\Site\NodeRepository; use App\Repository\Site\NodeRepository;
use App\Slugify\Slugify; 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 Symfony\Component\HttpKernel\KernelInterface;
use App\Entity\Site\Menu;
use App\Entity\Site\Navigation;
/** /**
* class NodeEventSubscriber. * class NodeEventSubscriber.
@ -33,13 +28,11 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber
NodeFactory $nodeFactory, NodeFactory $nodeFactory,
NodeRepository $nodeRepository, NodeRepository $nodeRepository,
EntityManager $entityManager, EntityManager $entityManager,
KernelInterface $kernel,
Slugify $slugify Slugify $slugify
) { ) {
$this->nodeFactory = $nodeFactory; $this->nodeFactory = $nodeFactory;
$this->nodeRepository = $nodeRepository; $this->nodeRepository = $nodeRepository;
$this->entityManager = $entityManager; $this->entityManager = $entityManager;
$this->kernel = $kernel;
$this->slugify = $slugify; $this->slugify = $slugify;
} }
@ -90,25 +83,6 @@ class NodeEventSubscriber extends EntityManagerEventSubscriber
$node->setUrl($generatedUrl); $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) public function onDelete(EntityManagerEvent $event)
{ {
if (!$this->support($event->getEntity())) { if (!$this->support($event->getEntity())) {

View file

@ -0,0 +1,70 @@
<?php
namespace App\EventSuscriber\Site;
use App\Entity\EntityInterface;
use App\Entity\Site\Node;
use App\Event\EntityManager\EntityManagerEvent;
use App\EventSuscriber\EntityManagerEventSubscriber;
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 SiteEventSubscriber.
*
* @author Simon Vieille <simon@deblan.fr>
*/
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);
}
}

View file

@ -12,7 +12,7 @@ use App\Entity\Site\Node;
*/ */
class NodeFactory class NodeFactory
{ {
public function create(?Menu $menu = null): Node public function create(?Menu $menu = null, string $url = null): Node
{ {
$entity = new Node(); $entity = new Node();
@ -20,6 +20,11 @@ class NodeFactory
$entity->setMenu($menu); $entity->setMenu($menu);
} }
if (null !== $url) {
$entity->setUrl($url);
}
return $entity; return $entity;
} }
} }

View file

@ -35,6 +35,10 @@ class SiteRouteLoader extends Loader
foreach ($navigations as $navigation) { foreach ($navigations as $navigation) {
foreach ($navigation->getMenus() as $menu) { foreach ($navigation->getMenus() as $menu) {
foreach ($menu->getRootNode()->getAllChildren() as $node) { foreach ($menu->getRootNode()->getAllChildren() as $node) {
if ($node->getParent() === null) {
continue;
}
$requirements = []; $requirements = [];
$defaults = [ $defaults = [