setDescription('Warms up the Twig templates cache'); $this->setHelp('The %command.name% command warms up the cache of the Twig templates.'); } protected function execute(InputInterface $input, OutputInterface $output): int { global $cfg, $PMA_Config, $dbi; $cfg['environment'] = 'production'; $PMA_Config = new Config(CONFIG_FILE); $PMA_Config->set('environment', $cfg['environment']); $dbi = new DatabaseInterface(new DbiDummy()); $tplDir = ROOT_PATH . 'templates'; $tmpDir = ROOT_PATH . 'twig-templates'; $loader = new FilesystemLoader($tplDir); $twig = new Environment($loader, [ 'auto_reload' => true, 'cache' => $tmpDir, ]); $twig->setExtensions([ new CoreExtension(), new I18nExtension(), new MessageExtension(), new PluginsExtension(), new RelationExtension(), new SanitizeExtension(), new StorageEngineExtension(), new TableExtension(), new TrackerExtension(), new TransformationsExtension(), new UrlExtension(), new UtilExtension(), ]); /** @var CacheInterface $twigCache */ $twigCache = $twig->getCache(false); $replacements = []; $templates = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($tplDir), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ($templates as $file) { // Skip test files if (strpos($file->getPathname(), '/test/') !== false) { continue; } // force compilation if ($file->isFile() && $file->getExtension() === 'twig') { $name = str_replace($tplDir . '/', '', $file->getPathname()); $template = $twig->loadTemplate($name); // Generate line map $cacheFilename = $twigCache->generateKey($name, $twig->getTemplateClass($name)); $template_file = 'templates/' . $name; $cache_file = str_replace($tmpDir, 'twig-templates', $cacheFilename); $replacements[$cache_file] = [$template_file, $template->getDebugInfo()]; } } // Store replacements in JSON $handle = fopen($tmpDir . '/replace.json', 'w'); if ($handle === false) { return 1; } fwrite($handle, (string) json_encode($replacements)); fclose($handle); return 0; } }