Skip to content
Snippets Groups Projects
Commit cce64198 authored by Mateusz Charytoniuk's avatar Mateusz Charytoniuk
Browse files

chore: warmup twig

parent f7e7cc25
Branches
Tags
No related merge requests found
......@@ -15,9 +15,12 @@ use Distantmagic\Resonance\SingletonCollection;
use Distantmagic\Resonance\SingletonContainer;
use Distantmagic\Resonance\SingletonProvider;
use Distantmagic\Resonance\TwigChainLoader;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Twig\Cache\FilesystemCache;
use Twig\Environment as TwigEnvironment;
use Twig\Error\Error;
use Twig\Extension\ExtensionInterface;
/**
......@@ -29,6 +32,7 @@ final readonly class TwigEnvironmentProvider extends SingletonProvider
{
public function __construct(
private ApplicationConfiguration $applicationConfiguration,
private LoggerInterface $logger,
private TwigChainLoader $twigChainLoader,
) {}
......@@ -43,6 +47,8 @@ final readonly class TwigEnvironmentProvider extends SingletonProvider
$twigEnvironment->addExtension($twigExtensionAttribute->singleton);
}
$this->warmupCache($twigEnvironment);
return $twigEnvironment;
}
......@@ -71,4 +77,37 @@ final readonly class TwigEnvironmentProvider extends SingletonProvider
return new FilesystemCache($cacheDirectory, FilesystemCache::FORCE_BYTECODE_INVALIDATION);
}
private function warmupCache(TwigEnvironment $twigEnvironment): void
{
$viewsDirectory = DM_APP_ROOT.'/views';
if (!is_dir($viewsDirectory)) {
return;
}
$finder = new Finder();
$found = $finder
->files()
->ignoreDotFiles(true)
->ignoreUnreadableDirs()
->ignoreVCS(true)
->in($viewsDirectory)
;
foreach ($found as $file) {
$relativePathname = $file->getRelativePathname();
try {
$twigEnvironment->load($relativePathname);
} catch (Error $error) {
$this->logger->warning(sprintf(
'twig_cache_warmup_error("%s", %d): %s',
$relativePathname,
$error->getTemplateLine(),
$error->getMessage()
));
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment