ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
LanguageFileDirectoryManager.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28{
32 private array $directories;
33
34 public function __construct(
35 private readonly LanguageFileDirectory $local_directory,
36 LanguageFileDirectory ...$global_directories
37 ) {
38 $this->directories = $global_directories;
39 $this->check();
40 }
41
42 private function check(): void
43 {
44 if ($this->local_directory instanceof CustomizingLanguageFileDirectory) {
45 if (!$this->local_directory->getPath()) {
46 throw new \InvalidArgumentException(
47 "CustomizingLanguageFileDirectory must have a non-empty path"
48 );
49 }
50 if ($this->local_directory->getPrefix() !== '') {
51 throw new \InvalidArgumentException(
52 "CustomizingLanguageFileDirectory must have an empty prefix"
53 );
54 }
55 }
56 $main_files = 0;
57 $prefixes = [];
58 $shipped_files = [];
59 foreach ($this->directories as $d) {
60 switch (true) {
61 case $d instanceof MainLanguageFileDirectory:
62 $main_files++;
63 if ($main_files > 1) {
64 throw new \InvalidArgumentException(
65 "There must not be more than one MainLanguageFileDirectory"
66 );
67 }
68 break;
69 case $d instanceof ComponentLanguageFileDirectory:
70 if (empty($d->getPrefix())) {
71 throw new \InvalidArgumentException(
72 "ComponentLanguageFileDirectory must have a non-empty prefix"
73 );
74 }
75 if (isset($prefixes[$d->getPrefix()])) {
76 throw new \InvalidArgumentException(
77 "There must not be two ComponentLanguageFileDirectory with the same prefix"
78 );
79 }
80 $prefixes[$d->getPrefix()] = true;
81 break;
82 default:
83 if (empty($d->getPrefix())) {
84 throw new \InvalidArgumentException("LanguageFileDirectory must have a non-empty prefix");
85 }
86 break;
87 }
88 // modules sharing a folder are told apart by their file names only
89 if ($d->getPrefix() !== '') {
90 try {
91 $files = rtrim($d->getPath(), '/') . '/' . MigratedLanguageFilePaths::shippedFileNamePattern($d);
92 } catch (\InvalidArgumentException) {
93 // an invalid pattern only warns in the build, see ShippedTranslationsBuild
94 continue;
95 }
96 if (isset($shipped_files[$files])) {
97 throw new \InvalidArgumentException(
98 "There must not be two LanguageFileDirectory shipping the same files " . $files
99 );
100 }
101 $shipped_files[$files] = true;
102 }
103 }
104 }
105
109 public function getDirectories(): \Generator
110 {
112 }
113
117 public function getCustomizingDirectories(): \Generator
118 {
119 yield $this->local_directory;
120 }
121
125 public function getAllDirectories(): \Generator
126 {
127 yield from $this->getDirectories();
128 yield from $this->getCustomizingDirectories();
129 }
130}
__construct(private readonly LanguageFileDirectory $local_directory, LanguageFileDirectory ... $global_directories)
static shippedFileNamePattern(LanguageFileDirectory $directory)
The name of $directory's shipped .po without ".po", with "%s" for the language key: its own pattern (...
A directory of language files with a path and a prefix for them.