tinternet.net/src/EventSuscriber/Site/SiteEventSubscriber.php

71 lines
1.7 KiB
PHP

<?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);
}
}