src/Frontend/Core/Engine/TwigTemplate.php line 53

Open in your IDE?
  1. <?php
  2. namespace Frontend\Core\Engine;
  3. use Frontend\Core\Language\Locale;
  4. use Common\Core\Twig\BaseTwigTemplate;
  5. use Common\Core\Twig\Extensions\TwigFilters;
  6. use Symfony\Bridge\Twig\Form\TwigRendererEngine;
  7. use Symfony\Component\Config\FileLocatorInterface;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Bridge\Twig\Extension\FormExtension as SymfonyFormExtension;
  10. use Symfony\Component\Form\FormRenderer;
  11. use Symfony\Component\Templating\TemplateNameParserInterface;
  12. use Twig_Environment;
  13. use Twig_FactoryRuntimeLoader;
  14. /**
  15.  * This is a twig template wrapper
  16.  * that glues spoon libraries and code standards with twig
  17.  */
  18. class TwigTemplate extends BaseTwigTemplate
  19. {
  20.     /**
  21.      * @var string
  22.      */
  23.     private $themePath;
  24.     public function __construct(
  25.         Twig_Environment $environment,
  26.         TemplateNameParserInterface $parser,
  27.         FileLocatorInterface $locator
  28.     ) {
  29.         $container Model::getContainer();
  30.         $this->forkSettings $container->get('fork.settings');
  31.         $this->language Locale::frontendLanguage();
  32.         parent::__construct($environment$parser$locator);
  33.         $this->debugMode $container->getParameter('kernel.debug');
  34.         if ($this->debugMode) {
  35.             $this->environment->enableAutoReload();
  36.             $this->environment->setCache(false);
  37.         }
  38.         $this->environment->disableStrictVariables();
  39.         new FormExtension($this->environment);
  40.         TwigFilters::addFilters($this->environment'Frontend');
  41.         $this->startGlobals($this->environment);
  42.         if (!$container->getParameter('fork.is_installed')) {
  43.             return;
  44.         }
  45.         $this->addFrontendPathsToTheTemplateLoader($this->forkSettings->get('Core''theme''Fork'));
  46.         $this->connectSymfonyForms();
  47.     }
  48.     private function addFrontendPathsToTheTemplateLoader(string $theme): void
  49.     {
  50.         $this->themePath FRONTEND_PATH '/Themes/' $theme;
  51.         $this->environment->setLoader(
  52.             new \Twig_Loader_Chain(
  53.                 [$this->environment->getLoader(), new \Twig_Loader_Filesystem($this->getLoadingFolders())]
  54.             )
  55.         );
  56.     }
  57.     private function connectSymfonyForms(): void
  58.     {
  59.         $rendererEngine = new TwigRendererEngine($this->getFormTemplates('FormLayout.html.twig'), $this->environment);
  60.         $csrfTokenManager Model::get('security.csrf.token_manager');
  61.         $this->environment->addRuntimeLoader(
  62.             new Twig_FactoryRuntimeLoader(
  63.                 [
  64.                     FormRenderer::class => function () use ($rendererEngine$csrfTokenManager): FormRenderer {
  65.                         return new FormRenderer($rendererEngine$csrfTokenManager);
  66.                     },
  67.                 ]
  68.             )
  69.         );
  70.         if (!$this->environment->hasExtension(SymfonyFormExtension::class)) {
  71.             $this->environment->addExtension(new SymfonyFormExtension());
  72.         }
  73.     }
  74.     /**
  75.      * Convert a filename extension
  76.      *
  77.      * @param string $template
  78.      *
  79.      * @return string
  80.      */
  81.     public function getPath(string $template): string
  82.     {
  83.         if (strpos($templateFRONTEND_MODULES_PATH) !== false) {
  84.             return str_replace(FRONTEND_MODULES_PATH '/'''$template);
  85.         }
  86.         // else it's in the theme folder
  87.         return str_replace($this->themePath '/'''$template);
  88.     }
  89.     /**
  90.      * Fetch the parsed content from this template.
  91.      *
  92.      * @param string $template The location of the template file, used to display this template.
  93.      *
  94.      * @return string The actual parsed content after executing this template.
  95.      */
  96.     public function getContent(string $template): string
  97.     {
  98.         $template $this->getPath($template);
  99.         $content $this->render(
  100.             $template,
  101.             $this->variables
  102.         );
  103.         $this->variables = [];
  104.         return $content;
  105.     }
  106.     private function getLoadingFolders(): array
  107.     {
  108.         return $this->filterOutNonExistingPaths(
  109.             [
  110.                 $this->themePath '/Modules',
  111.                 $this->themePath,
  112.                 FRONTEND_MODULES_PATH,
  113.                 FRONTEND_PATH,
  114.             ]
  115.         );
  116.     }
  117.     private function getFormTemplates(string $fileName): array
  118.     {
  119.         return $this->filterOutNonExistingPaths(
  120.             [
  121.                 FRONTEND_PATH '/Core/Layout/Templates/' $fileName,
  122.                 $this->themePath '/Core/Layout/Templates/' $fileName,
  123.             ]
  124.         );
  125.     }
  126.     private function filterOutNonExistingPaths(array $files): array
  127.     {
  128.         $filesystem = new Filesystem();
  129.         return array_filter(
  130.             $files,
  131.             function ($folder) use ($filesystem) {
  132.                 return $filesystem->exists($folder);
  133.             }
  134.         );
  135.     }
  136. }