vendor/contao/core-bundle/src/Resources/contao/library/Contao/TemplateLoader.php line 156

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. use Symfony\Component\Filesystem\Filesystem;
  11. use Symfony\Component\Finder\SplFileInfo;
  12. /**
  13.  * Automatically loads template files based on a mapper array
  14.  *
  15.  * The class stores template names and automatically loads the files upon their
  16.  * first usage. It uses a mapper array to support complex nesting and arbitrary
  17.  * subfolders to store the template files in.
  18.  *
  19.  * Usage:
  20.  *
  21.  *     ClassLoader::addFile('moo_mediabox', 'core/templates');
  22.  */
  23. class TemplateLoader
  24. {
  25.     /**
  26.      * Known files
  27.      * @var array
  28.      */
  29.     protected static $files = array();
  30.     /**
  31.      * Add a new template with its file path
  32.      *
  33.      * @param string $name The template name
  34.      * @param string $file The path to the template folder
  35.      */
  36.     public static function addFile($name, $file)
  37.     {
  38.         self::$files[$name] = $file;
  39.     }
  40.     /**
  41.      * Add multiple new templates with their file paths
  42.      *
  43.      * @param array $files An array of files
  44.      */
  45.     public static function addFiles($files)
  46.     {
  47.         foreach ($files as $name=>$file)
  48.         {
  49.             self::addFile($name, $file);
  50.         }
  51.     }
  52.     /**
  53.      * Return the template files as array
  54.      *
  55.      * @return array An array of files
  56.      */
  57.     public static function getFiles()
  58.     {
  59.         return self::$files;
  60.     }
  61.     /**
  62.      * Return the files matching a prefix as array
  63.      *
  64.      * @param string $prefix The prefix (e.g. "moo_")
  65.      *
  66.      * @return array An array of matching files
  67.      */
  68.     public static function getPrefixedFiles($prefix)
  69.     {
  70.         if (substr($prefix, -1) != '_')
  71.         {
  72.             $prefix .= '($|_)';
  73.         }
  74.         return array_values(preg_grep('/^' . $prefix . '/', array_keys(self::$files)));
  75.     }
  76.     /**
  77.      * Return a template path
  78.      *
  79.      * @param string $template The template name
  80.      * @param string $format   The output format (e.g. "html5")
  81.      * @param string $custom   The custom templates folder (defaults to "templates")
  82.      *
  83.      * @return string The path to the template file
  84.      */
  85.     public static function getPath($template, $format, $custom='templates')
  86.     {
  87.         $file = $template . '.' . $format;
  88.         $projectDir = System::getContainer()->getParameter('kernel.project_dir');
  89.         // Check the theme folder first
  90.         if (file_exists($projectDir . '/' . $custom . '/' . $file))
  91.         {
  92.             return $projectDir . '/' . $custom . '/' . $file;
  93.         }
  94.         // Then check the global templates directory (see #5547)
  95.         if ($custom != 'templates' && file_exists($projectDir . '/templates/' . $file))
  96.         {
  97.             return $projectDir . '/templates/' . $file;
  98.         }
  99.         return static::getDefaultPath($template, $format);
  100.     }
  101.     /**
  102.      * Return the path to the default template
  103.      *
  104.      * @param string $template The template name
  105.      * @param string $format   The output format (e.g. "html5")
  106.      *
  107.      * @return string The path to the default template file
  108.      *
  109.      * @throws \Exception If $template does not exist
  110.      */
  111.     public static function getDefaultPath($template, $format)
  112.     {
  113.         $file = $template . '.' . $format;
  114.         $container = System::getContainer();
  115.         $projectDir = $container->getParameter('kernel.project_dir');
  116.         if (isset(self::$files[$template]))
  117.         {
  118.             return $projectDir . '/' . self::$files[$template] . '/' . $file;
  119.         }
  120.         $strPath = null;
  121.         try
  122.         {
  123.             // Search for the template if it is not in the lookup array (last match wins)
  124.             foreach ($container->get('contao.resource_finder')->findIn('templates')->name($file) as $file)
  125.             {
  126.                 /** @var SplFileInfo $file */
  127.                 $strPath = $file->getPathname();
  128.             }
  129.         }
  130.         catch (\InvalidArgumentException $e)
  131.         {
  132.         }
  133.         if ($strPath !== null)
  134.         {
  135.             return $strPath;
  136.         }
  137.         throw new \Exception('Could not find template "' . $template . '"');
  138.     }
  139.     /**
  140.      * Find the templates in the Contao resource folders.
  141.      */
  142.     public static function initialize()
  143.     {
  144.         $objFilesystem = new Filesystem();
  145.         $container = System::getContainer();
  146.         $strCacheDir = $container->getParameter('kernel.cache_dir');
  147.         // Try to load from cache
  148.         if (file_exists($strCacheDir . '/contao/config/templates.php'))
  149.         {
  150.             self::addFiles(include $strCacheDir . '/contao/config/templates.php');
  151.         }
  152.         else
  153.         {
  154.             try
  155.             {
  156.                 foreach (System::getContainer()->get('contao.resource_finder')->findIn('templates')->name('*.html5') as $file)
  157.                 {
  158.                     /** @var SplFileInfo $file */
  159.                     self::addFile($file->getBasename('.html5'), rtrim($objFilesystem->makePathRelative($file->getPath(), $container->getParameter('kernel.project_dir')), '/'));
  160.                 }
  161.             }
  162.             catch (\InvalidArgumentException $e)
  163.             {
  164.             }
  165.         }
  166.     }
  167.     /**
  168.      * Reset the template list
  169.      *
  170.      * @internal Do not use this method in your code; it is meant for unit testing only
  171.      */
  172.     public static function reset()
  173.     {
  174.         self::$files = array();
  175.     }
  176. }
  177. class_alias(TemplateLoader::class, 'TemplateLoader');