This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\TranslatorBundle\Service\Command\Importer; |
||
4 | |||
5 | use Kunstmaan\TranslatorBundle\Model\Import\ImportCommand; |
||
6 | use Kunstmaan\TranslatorBundle\Service\Command\AbstractCommandHandler; |
||
7 | use Kunstmaan\TranslatorBundle\Service\Exception\TranslationsNotFoundException; |
||
8 | use Kunstmaan\TranslatorBundle\Service\TranslationFileExplorer; |
||
9 | use Symfony\Component\Finder\Finder; |
||
10 | use Symfony\Component\HttpKernel\Kernel; |
||
11 | |||
12 | /** |
||
13 | * Parses an ImportCommand |
||
14 | */ |
||
15 | class ImportCommandHandler extends AbstractCommandHandler |
||
16 | { |
||
17 | /** |
||
18 | * TranslationFileExplorer |
||
19 | * |
||
20 | * @var TranslationFileExplorer |
||
21 | */ |
||
22 | private $translationFileExplorer; |
||
23 | |||
24 | /** |
||
25 | * Importer |
||
26 | * |
||
27 | * @var Importer |
||
28 | */ |
||
29 | private $importer; |
||
30 | |||
31 | /** |
||
32 | * Execute an import command |
||
33 | * |
||
34 | * @param ImportCommand $importCommand |
||
35 | * |
||
36 | * @return int total number of files imported |
||
0 ignored issues
–
show
|
|||
37 | */ |
||
38 | 1 | public function executeImportCommand(ImportCommand $importCommand) |
|
39 | { |
||
40 | 1 | $amount = 0; |
|
41 | 1 | $defaultBundleNotSet = $importCommand->getDefaultBundle() === false || $importCommand->getDefaultBundle() === null; |
|
42 | |||
43 | 1 | if ($importCommand->getGlobals()) { |
|
44 | 1 | $amount = $this->importGlobalTranslationFiles($importCommand); |
|
45 | 1 | if ($defaultBundleNotSet) { |
|
46 | 1 | return $amount; |
|
47 | } |
||
48 | } |
||
49 | |||
50 | if ($defaultBundleNotSet) { |
||
51 | $importCommand->setDefaultBundle('all'); |
||
52 | } |
||
53 | |||
54 | $amount += $this->importBundleTranslationFiles($importCommand); |
||
55 | |||
56 | return $amount; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Import all translation files from app resources |
||
61 | * |
||
62 | * @param ImportCommand $importCommand |
||
63 | * |
||
64 | * @return int total number of files imported |
||
0 ignored issues
–
show
|
|||
65 | */ |
||
66 | 1 | private function importGlobalTranslationFiles(ImportCommand $importCommand) |
|
67 | { |
||
68 | 1 | $baseDir = Kernel::VERSION_ID >= 40000 ? $this->kernel->getProjectDir() : $this->kernel->getRootDir(); |
|
69 | 1 | $translationsDir = Kernel::VERSION_ID >= 40000 ? 'translations' : null; |
|
70 | 1 | $locales = $this->determineLocalesToImport($importCommand); |
|
71 | 1 | $finder = $this->translationFileExplorer->find($baseDir, $locales, $translationsDir); |
|
72 | |||
73 | 1 | return $this->importTranslationFiles($finder, $importCommand->getForce()); |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Import all translation files from a specific bundle, bundle name will be lowercased so cases don't matter |
||
78 | * |
||
79 | * @param ImportCommand $importCommand |
||
80 | * |
||
81 | * @return int total number of files imported |
||
82 | */ |
||
83 | 1 | public function importBundleTranslationFiles(ImportCommand $importCommand) |
|
84 | { |
||
85 | 1 | $importBundle = strtolower($importCommand->getDefaultBundle()); |
|
86 | |||
87 | 1 | if ($importBundle === 'all') { |
|
88 | $importCount = $this->importAllBundlesTranslationFiles($importCommand); |
||
89 | |||
90 | if (Kernel::VERSION_ID >= 40000) { |
||
91 | $importCount += $this->importSf4TranslationFiles($importCommand); |
||
92 | } |
||
93 | |||
94 | return $importCount; |
||
95 | } |
||
96 | |||
97 | 1 | if ($importBundle === 'custom') { |
|
98 | return $this->importCustomBundlesTranslationFiles($importCommand); |
||
99 | } |
||
100 | |||
101 | 1 | if (Kernel::VERSION_ID >= 40000) { |
|
102 | 1 | return $this->importSf4TranslationFiles($importCommand); |
|
103 | } |
||
104 | |||
105 | return $this->importOwnBundlesTranslationFiles($importCommand); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Import all translation files from all registered bundles (in AppKernel) |
||
110 | * |
||
111 | * @param ImportCommand $importCommand |
||
112 | * |
||
113 | * @return int total number of files imported |
||
0 ignored issues
–
show
|
|||
114 | */ |
||
115 | View Code Duplication | private function importAllBundlesTranslationFiles(ImportCommand $importCommand) |
|
116 | { |
||
117 | $bundles = array_map('strtolower', array_keys($this->kernel->getBundles())); |
||
118 | $imported = 0; |
||
119 | |||
120 | foreach ($bundles as $bundle) { |
||
121 | $importCommand->setDefaultBundle($bundle); |
||
122 | |||
123 | try { |
||
124 | $imported += $this->importSingleBundleTranslationFiles($importCommand); |
||
125 | } catch (TranslationsNotFoundException $e) { |
||
126 | continue; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return $imported; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Import all translation files from your own registered bundles (in src/ directory) |
||
135 | * |
||
136 | * @param ImportCommand $importCommand |
||
137 | * |
||
138 | * @return int The total number of imported files |
||
139 | */ |
||
140 | private function importOwnBundlesTranslationFiles(ImportCommand $importCommand) |
||
141 | { |
||
142 | $imported = 0; |
||
143 | $srcDir = $this->kernel->getProjectDir().'/src'; |
||
144 | |||
145 | foreach ($this->kernel->getBundles() as $name => $bundle) { |
||
146 | if (strpos($bundle->getPath(), $srcDir) !== false) { |
||
147 | $importCommand->setDefaultBundle(strtolower($name)); |
||
148 | |||
149 | try { |
||
150 | $imported += $this->importSingleBundleTranslationFiles($importCommand); |
||
151 | } catch (TranslationsNotFoundException $e) { |
||
152 | continue; |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | return $imported; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Import the translation files from the defined bundles. |
||
162 | * |
||
163 | * @param ImportCommand $importCommand |
||
164 | * |
||
165 | * @return int The total number of imported files |
||
166 | */ |
||
167 | View Code Duplication | private function importCustomBundlesTranslationFiles(ImportCommand $importCommand) |
|
168 | { |
||
169 | $imported = 0; |
||
170 | |||
171 | foreach ($importCommand->getBundles() as $bundle) { |
||
172 | $importCommand->setDefaultBundle(strtolower($bundle)); |
||
173 | |||
174 | try { |
||
175 | $imported += $this->importSingleBundleTranslationFiles($importCommand); |
||
176 | } catch (TranslationsNotFoundException $e) { |
||
177 | continue; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | return $imported; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Import all translation files from a single bundle |
||
186 | * |
||
187 | * @param ImportCommand $importCommand |
||
188 | * |
||
189 | * @return int total number of files imported |
||
0 ignored issues
–
show
|
|||
190 | */ |
||
191 | private function importSingleBundleTranslationFiles(ImportCommand $importCommand) |
||
192 | { |
||
193 | $this->validateBundleName($importCommand->getDefaultBundle()); |
||
194 | $bundles = array_change_key_case($this->kernel->getBundles(), CASE_LOWER); |
||
195 | $finder = $this->translationFileExplorer->find($bundles[strtolower($importCommand->getDefaultBundle())]->getPath(), $this->determineLocalesToImport($importCommand)); |
||
196 | |||
197 | if ($finder === null) { |
||
198 | return 0; |
||
199 | } |
||
200 | |||
201 | return $this->importTranslationFiles($finder, $importCommand->getForce()); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Import translation files from a specific Finder object |
||
206 | * The finder object shoud already have the files to look for defined; |
||
207 | * Forcing the import will override all existing translations in the stasher |
||
208 | * |
||
209 | * @param Finder $finder |
||
210 | * @param bool $force override identical translations in the stasher (domain/locale and keyword combination) |
||
211 | * |
||
212 | * @return int total number of files imported |
||
0 ignored issues
–
show
|
|||
213 | */ |
||
214 | 2 | private function importTranslationFiles(Finder $finder, $force = false) |
|
215 | { |
||
216 | 2 | if (!$finder instanceof Finder) { |
|
217 | return false; |
||
218 | } |
||
219 | |||
220 | 2 | $imported = 0; |
|
221 | |||
222 | 2 | foreach ($finder as $file) { |
|
223 | 2 | $imported += $this->importer->import($file, $force); |
|
224 | } |
||
225 | |||
226 | 2 | return $imported; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Validates that a bundle is registered in the AppKernel |
||
231 | * |
||
232 | * @param string $bundle |
||
233 | * |
||
234 | * @return bool bundle is valid or not |
||
235 | * |
||
236 | * @throws \Exception If the bundlename isn't valid |
||
237 | */ |
||
238 | public function validateBundleName($bundle) |
||
239 | { |
||
240 | // strtolower all bundle names |
||
241 | $bundles = array_map('strtolower', array_keys($this->kernel->getBundles())); |
||
242 | |||
243 | if (\in_array(strtolower(trim($bundle)), $bundles)) { |
||
244 | return true; |
||
245 | } |
||
246 | |||
247 | throw new \Exception(sprintf('bundle "%s" not found in available bundles: %s', $bundle, implode(', ', $bundles))); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Gives an array with all languages that needs to be imported (from the given ImportCommand) |
||
252 | * If non is given, all managed locales will be used (defined in config) |
||
253 | * |
||
254 | * @param ImportCommand $importCommand |
||
255 | * |
||
256 | * @return array all locales to import by the given ImportCommand |
||
257 | */ |
||
258 | 3 | public function determineLocalesToImport(ImportCommand $importCommand) |
|
259 | { |
||
260 | 3 | if ($importCommand->getLocales() === false || $importCommand->getLocales() === null) { |
|
261 | 3 | return $this->managedLocales; |
|
262 | } |
||
263 | |||
264 | return $this->parseRequestedLocales($importCommand->getLocales()); |
||
265 | } |
||
266 | |||
267 | 6 | public function setTranslationFileExplorer($translationFileExplorer) |
|
268 | { |
||
269 | 6 | $this->translationFileExplorer = $translationFileExplorer; |
|
270 | 6 | } |
|
271 | |||
272 | 6 | public function setImporter($importer) |
|
273 | { |
||
274 | 6 | $this->importer = $importer; |
|
275 | 6 | } |
|
276 | |||
277 | 1 | private function importSf4TranslationFiles($importCommand) |
|
278 | { |
||
279 | 1 | $finder = $this->translationFileExplorer->find($this->kernel->getProjectDir(), $this->determineLocalesToImport($importCommand), 'translations'); |
|
280 | |||
281 | 1 | if ($finder === null) { |
|
282 | return 0; |
||
283 | } |
||
284 | |||
285 | 1 | return $this->importTranslationFiles($finder, $importCommand->getForce()); |
|
286 | } |
||
287 | } |
||
288 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.