tinternet.net/core/EventSuscriber/Site/SiteEventSubscriber.php
2021-03-23 22:27:55 +01:00

67 lines
1.6 KiB
PHP

<?php
namespace App\Core\EventSuscriber\Site;
use App\Core\Entity\EntityInterface;
use App\Core\Entity\Site\Menu;
use App\Core\Entity\Site\Navigation;
use App\Core\Entity\Site\Node;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* 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;
}
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);
}
protected function cleanCache()
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'cache:clear',
]);
$output = new BufferedOutput();
$application->run($input, $output);
}
}