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
{
$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(),

View File

@ -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;

View File

@ -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();

View File

@ -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())) {

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
{
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;
}
}

View File

@ -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 = [