Skip to content
Snippets Groups Projects
Unverified Commit 7ec9caa0 authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Do not automatically load all the template directories of all modules.

It has also an impact in performance, and covers an unlikely scenario. Instead, if you plan to use templates from another module, now you need to call the "addTemplatesFromModule()" method right after creating the template. That way you can register manually what templates you are supposed to use, being much more efficient.
parent a506502b
No related branches found
No related tags found
No related merge requests found
...@@ -140,10 +140,11 @@ class SimpleSAML_XHTML_Template ...@@ -140,10 +140,11 @@ class SimpleSAML_XHTML_Template
} }
$this->twig_template = $namespace ? '@'.$namespace.'/'.$filename : $filename; $this->twig_template = $namespace ? '@'.$namespace.'/'.$filename : $filename;
$loader = new \Twig_Loader_Filesystem(); $loader = new \Twig_Loader_Filesystem();
$templateDirs = array_merge( $templateDirs = $this->findThemeTemplateDirs();
$this->findThemeTemplateDirs(), if ($this->module) {
$this->findModuleTemplateDirs() $templateDirs[] = array($this->module => $this->getModuleTemplateDir($this->module));
); }
// default, themeless templates are checked last // default, themeless templates are checked last
$templateDirs[] = array( $templateDirs[] = array(
$this->twig_namespace => $this->configuration->resolvePath('templates') $this->twig_namespace => $this->configuration->resolvePath('templates')
...@@ -241,27 +242,44 @@ class SimpleSAML_XHTML_Template ...@@ -241,27 +242,44 @@ class SimpleSAML_XHTML_Template
return array(); return array();
} }
/* /**
* Which enabled modules have templates? * Get the template directory of a module, if it exists.
* *
* @return array an array of module => templatedir lookups * @return string The templates directory of a module.
*
* @throws InvalidArgumentException If the module is not enabled or it has no templates directory.
*/ */
private function findModuleTemplateDirs() private function getModuleTemplateDir($module)
{ {
$all_modules = \SimpleSAML\Module::getModules(); if (!\SimpleSAML\Module::isModuleEnabled($module)) {
$modules = array(); throw new InvalidArgumentException('The module \''.$module.'\' is not enabled.');
foreach ($all_modules as $module) {
if (!\SimpleSAML\Module::isModuleEnabled($module)) {
continue;
}
$moduledir = \SimpleSAML\Module::getModuleDir($module);
// check if module has a /templates dir, if so, append
$templatedir = $moduledir.'/templates';
if (is_dir($templatedir)) {
$modules[] = array($module => $templatedir);
}
} }
return $modules; $moduledir = \SimpleSAML\Module::getModuleDir($module);
// check if module has a /templates dir, if so, append
$templatedir = $moduledir.'/templates';
if (!is_dir($templatedir)) {
throw new InvalidArgumentException('The module \''.$module.'\' has no templates directory.');
}
return $templatedir;
}
/**
* Add the templates from a given module.
*
* Note that the module must be installed, enabled, and contain a "templates" directory.
*
* @param string $module The module where we need to search for templates.
*
* @throws InvalidArgumentException If the module is not enabled or it has no templates directory.
*/
public function addTemplatesFromModule($module)
{
$dir = $this->getModuleTemplateDir($module);
/** @var Twig_Loader_Filesystem $loader */
$loader = $this->twig->getLoader();
$loader->addPath($dir, $module);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment