Skip to content

Setono/TwigCachePurgerBundle

Repository files navigation

Twig Cache Purger Bundle

Latest Version Software License build codecov

Purge the cache for individual Twig template files instead of removing the whole cache directory.

Requirements

  • PHP 8.2+
  • Symfony 6.0/7.0/8.0
  • Twig 2.x or 3.x

Installation

composer require setono/twig-cache-purger-bundle

This will install the bundle and enable it if you're using Symfony Flex. If you're not using Flex, add the bundle manually to bundles.php:

Setono\TwigCachePurgerBundle\SetonoTwigCachePurgerBundle::class => ['all' => true],

Usage

The bundle provides a PurgerInterface service that you can inject into your services to purge individual Twig template cache files by their logical template name:

use Setono\TwigCachePurgerBundle\Purger\PurgerInterface;

final class YourService
{
    public function __construct(private readonly PurgerInterface $purger)
    {
    }

    public function updateTemplate(): void
    {
        // Purge the cached compiled version of a specific template
        $this->purger->purge('emails/welcome.html.twig');
    }
}

The purger resolves the logical template name to its compiled cache file, invalidates the opcache/APC bytecode cache, and deletes the file. The next time the template is rendered, Twig will recompile it from the source.

About

Purge the cache for individual template files instead of removing the whole cache directory.

Resources

License

Stars

Watchers

Forks

Contributors