-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathTemplateLoader.php
More file actions
77 lines (69 loc) · 2.59 KB
/
Copy pathTemplateLoader.php
File metadata and controls
77 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
declare(strict_types=1);
namespace SimpleSAML\XHTML;
use SimpleSAML\Module;
/**
* This class extends the Twig\Loader\FilesystemLoader so that we can load templates from modules in twig, even
* when the main template is not part of a module (or the same one).
*
* @package simplesamlphp/simplesamlphp
*/
class TemplateLoader extends \Twig\Loader\FilesystemLoader
{
/**
* This method adds a namespace dynamically so that we can load templates from modules whenever we want.
*
* {@inheritdoc}
*
* @param string $name
* @param bool $throw
* @return string|null
*/
protected function findTemplate(string $name, bool $throw = true)
{
list($namespace, $shortname) = $this->parseModuleName($name);
if (!in_array($namespace, $this->paths, true) && $namespace !== self::MAIN_NAMESPACE) {
$this->addPath(self::getModuleTemplateDir($namespace), $namespace);
}
return parent::findTemplate($name, $throw);
}
/**
* Parse the name of a template in a module.
*
* @param string $name The full name of the template, including namespace and template name / path.
* @param string $default
*
* @return array An array with the corresponding namespace and name of the template. The namespace defaults to
* \Twig\Loader\FilesystemLoader::MAIN_NAMESPACE, if none was specified in $name.
*/
protected function parseModuleName(string $name, string $default = self::MAIN_NAMESPACE): array
{
if (strpos($name, ':')) {
// we have our old SSP format
list($namespace, $shortname) = explode(':', $name, 2);
return [$namespace, $shortname];
}
return [$default, $name];
}
/**
* Get the template directory of a module, if it exists.
*
* @param string $module
* @return string The templates directory of a module.
*
* @throws \InvalidArgumentException If the module is not enabled or it has no templates directory.
*/
public static function getModuleTemplateDir(string $module): string
{
if (!Module::isModuleEnabled($module)) {
throw new \InvalidArgumentException('The module \'' . $module . '\' is not enabled.');
}
$moduledir = 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;
}
}