src/EventSubscriber/VarnishCacheClearSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use FOS\HttpCacheBundle\CacheManager;
  4. use Symfony\Component\Console\ConsoleEvents;
  5. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class VarnishCacheClearSubscriber implements EventSubscriberInterface
  8. {
  9. private CacheManager $cacheManager;
  10. public function __construct(CacheManager $cacheManager)
  11. {
  12. $this->cacheManager = $cacheManager;
  13. }
  14. public static function getSubscribedEvents(): array
  15. {
  16. return [
  17. ConsoleEvents::COMMAND => ['onConsoleCommand', 255],
  18. ];
  19. }
  20. public function onConsoleCommand(ConsoleCommandEvent $event): void
  21. {
  22. if ($event->getCommand()->getName() === 'cache:clear') {
  23. try {
  24. $this->cacheManager->invalidateRegex('.');
  25. $this->cacheManager->flush();
  26. } catch (\Exception $e) {
  27. // Silently catch exceptions to avoid breaking cache:clear command
  28. }
  29. }
  30. }
  31. }