Skip to content
Snippets Groups Projects
Commit 74a954b4 authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Minor enhancements to SimpleSAML_XHTML_Template.

parent 34a070ac
Branches
Tags
No related merge requests found
...@@ -65,17 +65,22 @@ class SimpleSAML_XHTML_Template ...@@ -65,17 +65,22 @@ class SimpleSAML_XHTML_Template
*/ */
private $twig_template; private $twig_template;
/* /**
* Main Twig namespace, to avoid misspelling it *again* * Current module, if any.
*/ */
private $twig_namespace = \Twig_Loader_Filesystem::MAIN_NAMESPACE; private $module;
/* /**
* Current module, if any * Whether we are using a non-default theme or not.
*
* If we are using a theme, this variable holds an array with two keys: "module" and "name", those being the name
* of the module and the name of the theme, respectively. If we are using the default theme, the variable defaults
* to false.
*
* @var bool|array
*/ */
private $module; private $theme;
/** /**
* Constructor * Constructor
...@@ -147,7 +152,7 @@ class SimpleSAML_XHTML_Template ...@@ -147,7 +152,7 @@ class SimpleSAML_XHTML_Template
// default, themeless templates are checked last // default, themeless templates are checked last
$templateDirs[] = array( $templateDirs[] = array(
$this->twig_namespace => $this->configuration->resolvePath('templates') \Twig_Loader_Filesystem::MAIN_NAMESPACE => $this->configuration->resolvePath('templates')
); );
foreach ($templateDirs as $entry) { foreach ($templateDirs as $entry) {
$loader->addPath($entry[key($entry)], key($entry)); $loader->addPath($entry[key($entry)], key($entry));
...@@ -197,45 +202,52 @@ class SimpleSAML_XHTML_Template ...@@ -197,45 +202,52 @@ class SimpleSAML_XHTML_Template
return $twig; return $twig;
} }
/* /**
* Add overriding templates in configured theme * Add overriding templates from the configured theme.
* *
* @return array an array of module => templatedir lookups * @return array An array of module => templatedir lookups.
*/ */
private function findThemeTemplateDirs() private function findThemeTemplateDirs()
{ {
// parse config to find theme and module theme is in, if any // parse config to find theme and module theme is in, if any
$tmp = explode(':', $this->configuration->getString('theme.use', 'default'), 2); $theme = explode(':', $this->configuration->getString('theme.use', 'default'), 2);
if (count($tmp) === 2) { if (count($theme) === 1) { // no module involved
$themeModule = $tmp[0]; if ($theme === 'default') { // default theme
$themeName = $tmp[1]; return array();
} else { }
$themeModule = null; // non-default theme
$themeName = $tmp[0]; $this->theme = array(
'module' => null,
'name' => $theme[0],
);
return array();
} }
// default theme in use, abort
if ($themeName == 'default') { // theme from a module
$this->theme['module'] = $theme[0];
$this->theme['name'] = $theme[1];
// setup directories & namespaces
$themeDir = \SimpleSAML\Module::getModuleDir($this->theme['module']).'/themes/'.$this->theme['name'];
$subdirs = scandir($themeDir);
if (!$subdirs) { // no subdirectories in the theme directory, nothing to do here
// this is probably wrong, log a message
\SimpleSAML\Logger::warning('Emtpy theme directory for theme "'.$this->theme['name'].'".');
return array(); return array();
} }
if ($themeModule !== null) {
$moduleDir = \SimpleSAML\Module::getModuleDir($themeModule); $themeTemplateDirs = array();
$themeDir = $moduleDir.'/themes/'.$themeName; foreach ($subdirs as $entry) {
$files = scandir($themeDir); // discard anything that's not a directory. Expression is negated to profit from lazy evaluation
if ($files) { if (!($entry !== '.' && $entry !== '..' && is_dir($themeDir.'/'.$entry))) {
$themeTemplateDirs = array(); continue;
foreach ($files as $file) {
if ($file == '.' || $file == '..') {
continue;
}
// set correct name for default namespace
$ns = $file == 'default' ? $this->twig_namespace : $file;
$themeTemplateDirs[] = array($ns => $themeDir.'/'.$file);
}
return $themeTemplateDirs;
} }
// set correct name for the default namespace
$ns = ($entry === 'default') ? \Twig_Loader_Filesystem::MAIN_NAMESPACE : $entry;
$themeTemplateDirs[] = array($ns => $themeDir.'/'.$entry);
} }
// theme not found return $themeTemplateDirs;
return array();
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment