Skip to content

Commit 3ed70e4

Browse files
bug #47695 [FrameworkBundle] Filter out trans paths that are covered by a parent folder path (natewiebe13)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [FrameworkBundle] Filter out trans paths that are covered by a parent folder path | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Partially Fixes #45645 | License | MIT | Doc PR | N/A It was possible to end up with duplicated paths which would could result in many files being scanned more than once. This prevents that. Commits ------- 459ac042a9 [FrameworkBundle] Filter out trans paths that are covered by a parent folder path
2 parents 5e9fbe2 + 13e3193 commit 3ed70e4

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

Command/TranslationUpdateCommand.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ private function extractMessages(string $locale, array $transPaths, string $pref
407407
{
408408
$extractedCatalogue = new MessageCatalogue($locale);
409409
$this->extractor->setPrefix($prefix);
410+
$transPaths = $this->filterDuplicateTransPaths($transPaths);
410411
foreach ($transPaths as $path) {
411412
if (is_dir($path) || is_file($path)) {
412413
$this->extractor->extract($path, $extractedCatalogue);
@@ -416,6 +417,27 @@ private function extractMessages(string $locale, array $transPaths, string $pref
416417
return $extractedCatalogue;
417418
}
418419

420+
private function filterDuplicateTransPaths(array $transPaths): array
421+
{
422+
$transPaths = array_filter(array_map('realpath', $transPaths));
423+
424+
sort($transPaths);
425+
426+
$filteredPaths = [];
427+
428+
foreach ($transPaths as $path) {
429+
foreach ($filteredPaths as $filteredPath) {
430+
if (str_starts_with($path, $filteredPath.\DIRECTORY_SEPARATOR)) {
431+
continue 2;
432+
}
433+
}
434+
435+
$filteredPaths[] = $path;
436+
}
437+
438+
return $filteredPaths;
439+
}
440+
419441
private function loadCurrentMessages(string $locale, array $transPaths): MessageCatalogue
420442
{
421443
$currentCatalogue = new MessageCatalogue($locale);

Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,46 @@ public function testWriteMessagesForSpecificDomain()
140140
$this->assertMatchesRegularExpression('/Translation files were successfully updated./', $tester->getDisplay());
141141
}
142142

143+
public function testFilterDuplicateTransPaths()
144+
{
145+
$transPaths = [
146+
$this->translationDir.'/a/test/folder/with/a/subfolder',
147+
$this->translationDir.'/a/test/folder/',
148+
$this->translationDir.'/a/test/folder/with/a/subfolder/and/a/file.txt',
149+
$this->translationDir.'/a/different/test/folder',
150+
];
151+
152+
$expectedPaths = [
153+
$this->translationDir.'/a/different/test/folder',
154+
$this->translationDir.'/a/test/folder',
155+
];
156+
157+
foreach ($transPaths as $transPath) {
158+
if (realpath($transPath)) {
159+
continue;
160+
}
161+
162+
if (preg_match('/\.[a-z]+$/', $transPath)) {
163+
if (!realpath(\dirname($transPath))) {
164+
mkdir(\dirname($transPath), 0777, true);
165+
}
166+
167+
touch($transPath);
168+
} else {
169+
mkdir($transPath, 0777, true);
170+
}
171+
}
172+
173+
$command = $this->createMock(TranslationUpdateCommand::class);
174+
175+
$method = new \ReflectionMethod(TranslationUpdateCommand::class, 'filterDuplicateTransPaths');
176+
$method->setAccessible(true);
177+
178+
$filteredTransPaths = $method->invoke($command, $transPaths);
179+
180+
$this->assertEquals($expectedPaths, $filteredTransPaths);
181+
}
182+
143183
protected function setUp(): void
144184
{
145185
$this->fs = new Filesystem();

0 commit comments

Comments
 (0)