ComponentsLoader.php in Components! 8.2
File
src/Template/Loader/ComponentsLoader.php
View source
<?php
namespace Drupal\components\Template\Loader;
use Drupal\Core\Theme\ActiveTheme;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\components\Template\ComponentsInfo;
use Twig\Loader\FilesystemLoader;
class ComponentsLoader extends FilesystemLoader {
protected $componentsInfo;
protected $themeManager;
protected $activeTheme;
protected $activeThemeNamespaces;
protected $moduleNamespaces;
public function __construct(ComponentsInfo $components_info, ThemeManagerInterface $theme_manager) {
parent::__construct();
$this->componentsInfo = $components_info;
$this->themeManager = $theme_manager;
}
public function checkActiveTheme() {
$active_theme = $this->themeManager
->getActiveTheme();
if ($this->activeTheme !== $active_theme
->getName()) {
$this
->setActiveTheme($active_theme);
}
return $this->activeTheme;
}
protected function setActiveTheme(ActiveTheme $active_theme) {
$this->activeTheme = $active_theme
->getName();
$this->cache = $this->errorCache = [];
if (isset($this->activeThemeNamespaces[$this->activeTheme])) {
$this->paths = $this->activeThemeNamespaces[$this->activeTheme];
return;
}
$active_themes = [
$this->activeTheme,
];
foreach ($active_theme
->getBaseThemeExtensions() as $extension) {
$active_themes[] = $extension
->getName();
}
$theme_info = $this->componentsInfo
->getAllThemeInfo();
$this->paths = [];
if (!isset($this->moduleNamespaces)) {
$this->moduleNamespaces = [];
$module_info = $this->componentsInfo
->getAllModuleInfo();
foreach ($module_info as $extensionName => $info) {
if (isset($info['namespaces']) && isset($info['namespaces'][$extensionName])) {
$this->moduleNamespaces[$extensionName] = $info['namespaces'][$extensionName];
}
}
foreach ($module_info as $moduleName => $info) {
if (isset($info['namespaces'])) {
foreach ($info['namespaces'] as $namespace => $paths) {
if ($this->componentsInfo
->isProtectedNamespace($namespace)) {
$extensionInfo = $this->componentsInfo
->getProtectedNamespaceExtensionInfo($namespace);
$this->componentsInfo
->logWarning(sprintf('The %s module attempted to alter the protected Twig namespace, %s, owned by the %s %s. See https://www.drupal.org/node/3190969#s-extending-a-default-twig-namespace to fix this error.', $moduleName, $namespace, $extensionInfo['name'], $extensionInfo['type']));
}
elseif ($namespace !== $moduleName) {
if (!isset($this->moduleNamespaces[$namespace])) {
$this->moduleNamespaces[$namespace] = [];
}
foreach (array_reverse($paths) as $path) {
array_unshift($this->moduleNamespaces[$namespace], $path);
}
}
}
}
}
}
foreach ($this->moduleNamespaces as $name => $paths) {
$this
->setPaths($paths, $name);
}
foreach (array_reverse($active_themes) as $theme_name) {
if (isset($theme_info[$theme_name]) && isset($theme_info[$theme_name]['namespaces'])) {
foreach ($theme_info[$theme_name]['namespaces'] as $namespace => $paths) {
if ($this->componentsInfo
->isProtectedNamespace($namespace)) {
$extensionInfo = $this->componentsInfo
->getProtectedNamespaceExtensionInfo($namespace);
$this->componentsInfo
->logWarning(sprintf('The %s theme attempted to alter the protected Twig namespace, %s, owned by the %s %s. See https://www.drupal.org/node/3190969#s-extending-a-default-twig-namespace to fix this error.', $theme_name, $namespace, $extensionInfo['name'], $extensionInfo['type']));
}
else {
foreach (array_reverse($paths) as $path) {
$this
->prependPath($path, $namespace);
}
}
}
}
}
$this->componentsInfo
->suppressWarnings();
$this->activeThemeNamespaces[$this->activeTheme] = $this->paths;
}
public function addPath($path, $namespace = self::MAIN_NAMESPACE) {
$this->cache = $this->errorCache = [];
$this->paths[$namespace][] = rtrim($path, '/\\');
}
public function prependPath($path, $namespace = self::MAIN_NAMESPACE) {
$this->cache = $this->errorCache = [];
$path = rtrim($path, '/\\');
if (!isset($this->paths[$namespace])) {
$this->paths[$namespace][] = $path;
}
else {
array_unshift($this->paths[$namespace], $path);
}
}
protected function findTemplate($name, $throw = TRUE) {
$this
->checkActiveTheme();
return parent::findTemplate($name, $throw);
}
}