|
6 | 6 |
|
7 | 7 | use Gettext\Scanner\PhpScanner; |
8 | 8 | use SimpleSAML\Configuration; |
| 9 | +use SimpleSAML\Module; |
9 | 10 | use SimpleSAML\XHTML\Template; |
10 | 11 | use Symfony\Bridge\Twig\Translation\TwigExtractor; |
11 | 12 | use Symfony\Component\Finder\Finder; |
12 | 13 | use Symfony\Component\Translation\MessageCatalogue; |
13 | 14 |
|
| 15 | +use array_merge; |
| 16 | +use explode; |
| 17 | +use in_array; |
| 18 | +use is_dir; |
| 19 | +use str_starts_with; |
| 20 | +use strlen; |
| 21 | +use substr; |
| 22 | + |
14 | 23 | /** |
15 | 24 | * @package SimpleSAMLphp |
16 | 25 | */ |
@@ -45,19 +54,48 @@ public function getTranslationsFromPhp(string $module, PhpScanner $phpScanner): |
45 | 54 | } |
46 | 55 |
|
47 | 56 |
|
48 | | - public function getTranslationsFromTwig(string $module): array |
| 57 | + public function getTranslationsFromTwig(string $module, bool $includeThemes = false): array |
49 | 58 | { |
50 | 59 | $twigTranslations = []; |
51 | 60 | $moduleDir = $this->baseDir . ($module === '' ? '' : 'modules/' . $module . '/'); |
52 | 61 | $moduleTemplateDir = $moduleDir . 'templates/'; |
| 62 | + $moduleThemeDir = $moduleDir . 'themes/'; |
| 63 | + $moduleDirs = []; |
| 64 | + if (is_dir($moduleTemplateDir)) { |
| 65 | + $moduleDirs[] = $moduleTemplateDir; |
| 66 | + } |
| 67 | + if ($includeThemes && is_dir($moduleThemeDir)) { |
| 68 | + $moduleDirs[] = $moduleThemeDir; |
| 69 | + } |
53 | 70 |
|
54 | 71 | // Scan Twig-templates |
55 | 72 | $finder = new Finder(); |
56 | | - foreach ($finder->files()->in($moduleTemplateDir)->name('*.twig') as $file) { |
57 | | - $template = new Template( |
58 | | - $this->configuration, |
59 | | - ($module ? ($module . ':') : '') . $file->getRelativePathname(), |
60 | | - ); |
| 73 | + foreach ($finder->files()->in($moduleDirs)->name('*.twig') as $file) { |
| 74 | + if (!($includeThemes && str_starts_with($file->getPathname(), $moduleThemeDir))) { |
| 75 | + /* process templates */ |
| 76 | + $template = new Template( |
| 77 | + $this->configuration, |
| 78 | + ($module ? ($module . ':') : '') . $file->getRelativePathname(), |
| 79 | + ); |
| 80 | + } else { |
| 81 | + /* process themed templates from other modules */ |
| 82 | + list($theme, $themedModule) = explode(DIRECTORY_SEPARATOR, substr($file->getPath(), strlen($moduleThemeDir)), 2); |
| 83 | + if ($themedModule !== 'default' && !Module::isModuleEnabled($themedModule)) { |
| 84 | + throw new Exception( |
| 85 | + 'The module \'' . $themedModule . '\' (themed by \'' . $module . ':' . $theme . '\') is not enabled.' |
| 86 | + ); |
| 87 | + } |
| 88 | + $template = new Template( |
| 89 | + Configuration::loadFromArray( |
| 90 | + array_merge( |
| 91 | + $this->configuration->toArray(), |
| 92 | + ['theme.use' => $module . ':' . $theme,], |
| 93 | + ), |
| 94 | + ), |
| 95 | + ($themedModule !== 'default' ? ($themedModule . ':') : '') . |
| 96 | + substr($file->getRelativePathname(), strlen($theme . DIRECTORY_SEPARATOR . $themedModule) + 1), |
| 97 | + ); |
| 98 | + } |
61 | 99 |
|
62 | 100 | $catalogue = new MessageCatalogue('en', []); |
63 | 101 | $extractor = new TwigExtractor($template->getTwig()); |
|
0 commit comments