public function onRegistration(BundleTranslationFilesEvent $event) { $bundleAlias = $event->getBundleAlias(); $tplDir = $this->fileCollector->resolve($bundleAlias); // storing the old values to reset them when we’re done $actualCachePath = $this->twig->getCache(); $actualAutoReload = $this->twig->isAutoReload(); // create tmp cache path $filesystem = new Filesystem(); $cachePath = $event->getCacheBasePath() . md5(__CLASS__); $filesystem->mkdir($cachePath); // setting temporary values $this->twig->enableAutoReload(); $this->twig->setCache($cachePath); foreach ($this->fileCollector->collect($tplDir, "twig") as $tplPath) { $config = $this->getConfigFromTemplate($tplPath); if ($config && is_array($config) && isset($config["name"]) && $config["name"] instanceof Twig_Node_Expression_Function) { $compiler = new Twig_Compiler($this->twig); $config["name"]->compile($compiler); $cacheFilePath = $this->twig->getCacheFilename($tplPath); $filesystem->dumpFile($cacheFilePath, "<?php " . $compiler->getSource()); $tplPathId = str_replace($tplDir, "@{$bundleAlias}/", $tplPath); $event->registerSourceFile($tplPathId, $cacheFilePath); } } // resetting original values $this->twig->setCache($actualCachePath); call_user_func([$this->twig, $actualAutoReload ? "enableAutoReload" : "disableAutoReload"]); }
public function onRegistration(BundleTranslationFilesEvent $event) { $bundleAlias = $event->getBundleAlias(); $tplDir = $this->fileCollector->resolve($bundleAlias); // storing the old values to reset them when we’re done $actualCachePath = $this->twig->getCache(); $actualAutoReload = $this->twig->isAutoReload(); // create tmp cache path $filesystem = new Filesystem(); $cachePath = $event->getCacheBasePath() . md5(__CLASS__); $filesystem->mkdir($cachePath); // setting temporary values $this->twig->enableAutoReload(); $this->twig->setCache($cachePath); foreach ($this->fileCollector->collect($tplDir, "twig") as $file) { $this->twig->loadTemplate($file); // force rendering $cacheFilePath = $this->twig->getCacheFilename($file); $fileId = str_replace($tplDir, "@{$bundleAlias}/", $file); $event->registerSourceFile($fileId, $cacheFilePath); } // resetting original values $this->twig->setCache($actualCachePath); call_user_func([$this->twig, $actualAutoReload ? "enableAutoReload" : "disableAutoReload"]); }