|
6 | 6 |
|
7 | 7 | use Gettext\Scanner\PhpScanner; |
8 | 8 | use SimpleSAML\Configuration; |
| 9 | +use SimpleSAML\Error\Exception; |
| 10 | +use SimpleSAML\Module; |
9 | 11 | use SimpleSAML\XHTML\Template; |
10 | 12 | use Symfony\Bridge\Twig\Translation\TwigExtractor; |
11 | 13 | use Symfony\Component\Finder\Finder; |
12 | 14 | use Symfony\Component\Translation\MessageCatalogue; |
13 | 15 |
|
| 16 | +use function array_merge; |
| 17 | +use function explode; |
| 18 | +use function is_dir; |
| 19 | +use function str_starts_with; |
| 20 | +use function strlen; |
| 21 | +use function substr; |
| 22 | + |
14 | 23 | /** |
15 | 24 | * @package SimpleSAMLphp |
16 | 25 | */ |
@@ -45,19 +54,54 @@ 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/ directory */ |
| 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( |
| 83 | + DIRECTORY_SEPARATOR, |
| 84 | + substr($file->getPath(), strlen($moduleThemeDir)), |
| 85 | + 2, |
| 86 | + ); |
| 87 | + if ($themedModule !== 'default' && !Module::isModuleEnabled($themedModule)) { |
| 88 | + throw new Exception( |
| 89 | + 'The module \'' . $themedModule . '\' (themed by \'' . $module . ':' . $theme . '\') ' . |
| 90 | + 'is not enabled. Perhaps you need to need to require --dev simplesamlphp-module-' . |
| 91 | + $themedModule . ' within the ' . $module . ' module?', |
| 92 | + ); |
| 93 | + } |
| 94 | + $template = new Template( |
| 95 | + Configuration::loadFromArray( |
| 96 | + array_merge( |
| 97 | + $this->configuration->toArray(), |
| 98 | + ['theme.use' => $module . ':' . $theme,], |
| 99 | + ), |
| 100 | + ), |
| 101 | + ($themedModule !== 'default' ? ($themedModule . ':') : '') . |
| 102 | + substr($file->getRelativePathname(), strlen($theme . DIRECTORY_SEPARATOR . $themedModule) + 1), |
| 103 | + ); |
| 104 | + } |
61 | 105 |
|
62 | 106 | $catalogue = new MessageCatalogue('en', []); |
63 | 107 | $extractor = new TwigExtractor($template->getTwig()); |
|
0 commit comments