<?php
namespace App\EventSubscriber;
use FOS\HttpCacheBundle\CacheManager;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class VarnishCacheClearSubscriber implements EventSubscriberInterface
{
private CacheManager $cacheManager;
public function __construct(CacheManager $cacheManager)
{
$this->cacheManager = $cacheManager;
}
public static function getSubscribedEvents(): array
{
return [
ConsoleEvents::COMMAND => ['onConsoleCommand', 255],
];
}
public function onConsoleCommand(ConsoleCommandEvent $event): void
{
if ($event->getCommand()->getName() === 'cache:clear') {
try {
$this->cacheManager->invalidateRegex('.');
$this->cacheManager->flush();
} catch (\Exception $e) {
// Silently catch exceptions to avoid breaking cache:clear command
}
}
}
}