#!/bin/php isDir() && !$fpath->isLink()) ? rmdir($fpath->getPathname()) : unlink($fpath->getPathname()); } rmdir($path); return true; } return false; } /** * Return a custom Twig cache handler. * This handler is useful to be able to preserve filenames of compiled files. * * @param string $directory * * @return CacheInterface */ function getTwigCacheHandler(string $directory): CacheInterface { return new class($directory) extends FilesystemCache { private string $directory; public function __construct(string $directory, int $options = 0) { $this->directory = rtrim($directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; parent::__construct($directory, $options); } public function generateKey(string $name, string $className): string { return $this->directory . $name; } }; } $directory = sprintf('%s/../galette/templates/default', __DIR__); $cache_dir = sprintf('%s/../tempcache', __DIR__); $cache = getTwigCacheHandler($cache_dir); if (file_exists($cache_dir)) { rmdir_recursive($cache_dir); } mkdir($cache_dir); $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::LEAVES_ONLY ); $loader = new FilesystemLoader($directory); $twig = new Environment( $loader, [ 'cache' => $cache, 'auto_reload' => true, ] ); $twig_functions = [ '__', '_T', '_Tn', '_Tx', '_Tnx', 'url_for', 'memberName', 'callstatic', 'is_current_url', 'get_class', 'base_path', 'file_exists' ]; foreach ($twig_functions as $function) { $twig->addFunction(new TwigFunction($function, $function)); } /** @var SplFileInfo $file */ foreach ($iterator as $file) { if ($file->isFile()) { $twig->load(str_replace($directory . '/', '', $file)); } }