<?php
namespace App\EventListener;
use App\Event\PageEvent;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouterInterface;
class PageEventListener
{
public const PAGE_CACHE_DIRTY = 'page_cache_dirty';
public const PAGE_CACHE_DIRTY_MODAL = 'page_cache_dirty_modal';
private RouterInterface $router;
private Filesystem $filesystem;
private KernelInterface $kernel;
public function __construct(
RouterInterface $router,
Filesystem $filesystem,
KernelInterface $kernel
) {
$this->router = $router;
$this->filesystem = $filesystem;
$this->kernel = $kernel;
}
public function onPostPersist(PageEvent $pageEvent)
{
$cacheDir = $this->kernel->getCacheDir();
$cacheFiles = [];
foreach (['url_generating_routes', 'url_matching_routes'] as $className) {
$cacheFiles[] = sprintf('%s%s%s.php', $cacheDir, \DIRECTORY_SEPARATOR, $className);
}
$this->filesystem->remove($cacheFiles);
$this->router->warmUp($cacheDir);
}
}