-
-
Save quentint/8230c9365716d264a2a35603116a66d3 to your computer and use it in GitHub Desktop.
[Twig] Refreshing modified Templates when OPcache or APC is enabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file src/Kernel.php | |
*/ | |
<?php | |
namespace App; | |
use App\DependencyInjection\Compiler\TwigCacheFileSystemPass; | |
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | |
use Symfony\Component\Config\Loader\LoaderInterface; | |
use Symfony\Component\Config\Resource\FileResource; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\HttpKernel\Kernel as BaseKernel; | |
use Symfony\Component\Routing\RouteCollectionBuilder; | |
class Kernel extends BaseKernel | |
{ | |
// (...) | |
protected function build(ContainerBuilder $container) { | |
$container->addCompilerPass(new TwigCacheFileSystemPass($this->isDebug())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file src/DependencyInjection/Compiler/TwigCacheFileSystemPass.php | |
*/ | |
namespace App\DependencyInjection\Compiler; | |
use App\Twig\Cache\TwigCacheForceByteCodeRefreshFactory; | |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Definition; | |
use Symfony\Component\DependencyInjection\Reference; | |
use Twig\Cache\FilesystemCache; | |
class TwigCacheFileSystemPass implements CompilerPassInterface { | |
protected bool $isDebug; | |
public function __construct(bool $isDebug) { | |
$this->isDebug = $isDebug; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function process(ContainerBuilder $container) { | |
if (false === $container->hasDefinition('twig')) { | |
return; | |
} | |
$container->register( | |
'app.twig.fileSystem_cache_refresh_factory', | |
TwigCacheForceByteCodeRefreshFactory::class | |
) | |
->addArgument($container->getParameter('kernel.cache_dir') . '/twig') | |
->setPublic(false); | |
$twigCacheFileSystem = (new Definition(FilesystemCache::class)) | |
->setPublic(false) | |
->setFactory( | |
array( | |
new Reference('app.twig.fileSystem_cache_refresh_factory'), | |
'factory', | |
) | |
); | |
$options = array( | |
'cache' => $twigCacheFileSystem, | |
'debug' => $this->isDebug, | |
); | |
$twigDef = $container->findDefinition('twig'); | |
$twigDef->replaceArgument( | |
1, | |
array_merge((array)$twigDef->getArgument(0), $options) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file src/Twig/Cache/TwigCacheForceByteCodeRefreshFactory.php | |
*/ | |
namespace App\Twig\Cache; | |
use Twig\Cache\FilesystemCache; | |
class TwigCacheForceByteCodeRefreshFactory { | |
private string $cacheDir; | |
public function __construct($cacheDir) { | |
$this->cacheDir = $cacheDir; | |
} | |
public function factory(): FilesystemCache { | |
return new FilesystemCache( | |
$this->cacheDir, | |
FilesystemCache::FORCE_BYTECODE_INVALIDATION | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment