Skip to content

Commit 459ac04

Browse files
natewiebe13nicolas-grekas
authored andcommitted
[FrameworkBundle] Filter out trans paths that are covered by a parent folder path
1 parent 97e4212 commit 459ac04

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/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);

src/Symfony/Bundle/FrameworkBundle/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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy