Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Template / Loader / FilesystemLoader.php
1 <?php
2
3 namespace Drupal\Core\Template\Loader;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Extension\ThemeHandlerInterface;
7
8 /**
9  * Loads templates from the filesystem.
10  *
11  * This loader adds module and theme template paths as namespaces to the Twig
12  * filesystem loader so that templates can be referenced by namespace, like
13  * @block/block.html.twig or @mytheme/page.html.twig.
14  */
15 class FilesystemLoader extends \Twig_Loader_Filesystem {
16
17   /**
18    * Constructs a new FilesystemLoader object.
19    *
20    * @param string|array $paths
21    *   A path or an array of paths to check for templates.
22    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
23    *   The module handler service.
24    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
25    *   The theme handler service.
26    */
27   public function __construct($paths, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
28     parent::__construct($paths);
29
30     // Add namespaced paths for modules and themes.
31     $namespaces = [];
32     foreach ($module_handler->getModuleList() as $name => $extension) {
33       $namespaces[$name] = $extension->getPath();
34     }
35     foreach ($theme_handler->listInfo() as $name => $extension) {
36       $namespaces[$name] = $extension->getPath();
37     }
38
39     foreach ($namespaces as $name => $path) {
40       $this->addPath($path . '/templates', $name);
41     }
42   }
43
44   /**
45    * Adds a path where templates are stored.
46    *
47    * @param string $path
48    *   A path where to look for templates.
49    * @param string $namespace
50    *   (optional) A path name.
51    */
52   public function addPath($path, $namespace = self::MAIN_NAMESPACE) {
53     // Invalidate the cache.
54     $this->cache = [];
55     $this->paths[$namespace][] = rtrim($path, '/\\');
56   }
57
58 }