Move twig template cache

This commit is contained in:
Zankaria
2024-04-26 14:01:32 +02:00
parent 7025035376
commit ebbb5fce53
2 changed files with 27 additions and 29 deletions

View File

@@ -18,7 +18,7 @@ function load_twig() {
$twig = new Twig\Environment($loader, array(
'autoescape' => false,
'cache' => is_writable('templates/') || (is_dir($cache_dir) && is_writable($cache_dir)) ?
new Twig_Cache_TinyboardFilesystem($cache_dir) : false,
new TinyboardTwigCache($cache_dir) : false,
'debug' => $config['debug'],
'auto_reload' => $config['twig_auto_reload']
));
@@ -73,6 +73,32 @@ function Element($templateFile, array $options) {
}
}
class TinyboardTwigCache extends Twig\Cache\FilesystemCache {
private string $directory;
public function __construct(string $directory) {
parent::__construct($directory);
$this->directory = $directory;
}
/**
* This function was removed in Twig 2.x due to developer views on the Twig library.
* Who says we can't keep it for ourselves though?
*/
public function clear() {
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->directory),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($iter as $file) {
if ($file->isFile()) {
@unlink($file->getPathname());
}
}
}
}
class Tinyboard extends Twig\Extension\AbstractExtension
{
/**