Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 79e29c1

Browse filesBrowse files
committed
[Translator][fallback catalogues] fixed circular reference.
1 parent fedbf71 commit 79e29c1
Copy full SHA for 79e29c1

File tree

Expand file treeCollapse file tree

3 files changed

+27
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+27
-5
lines changed

‎src/Symfony/Component/Translation/MessageCatalogue.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/MessageCatalogue.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ public function addCatalogue(MessageCatalogueInterface $catalogue)
190190
public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
191191
{
192192
// detect circular references
193+
$c = $catalogue;
194+
while ($c = $c->getFallbackCatalogue()) {
195+
if ($c->getLocale() === $this->getLocale()) {
196+
throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
197+
}
198+
}
199+
193200
$c = $this;
194201
do {
195202
if ($c->getLocale() === $catalogue->getLocale()) {

‎src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Tests/MessageCatalogueTest.php
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function testAddFallbackCatalogue()
127127
/**
128128
* @expectedException \LogicException
129129
*/
130-
public function testAddFallbackCatalogueWithCircularReference()
130+
public function testAddFallbackCatalogueWithParentCircularReference()
131131
{
132132
$main = new MessageCatalogue('en_US');
133133
$fallback = new MessageCatalogue('fr_FR');
@@ -136,6 +136,20 @@ public function testAddFallbackCatalogueWithCircularReference()
136136
$main->addFallbackCatalogue($fallback);
137137
}
138138

139+
/**
140+
* @expectedException \LogicException
141+
*/
142+
public function testAddFallbackCatalogueWithFallbackCircularReference()
143+
{
144+
$fr = new MessageCatalogue('fr');
145+
$en = new MessageCatalogue('en');
146+
$es = new MessageCatalogue('es');
147+
148+
$fr->addFallbackCatalogue($en);
149+
$es->addFallbackCatalogue($en);
150+
$en->addFallbackCatalogue($fr);
151+
}
152+
139153
/**
140154
* @expectedException \LogicException
141155
*/
@@ -178,10 +192,10 @@ public function testMetadataSetGetDelete()
178192
$this->assertEquals(array(), $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 is array');
179193

180194
$catalogue->deleteMetadata('key2', 'messages');
181-
$this->assertEquals(null, $catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
195+
$this->assertNull($catalogue->getMetadata('key2', 'messages'), 'Metadata key2 should is deleted.');
182196

183197
$catalogue->deleteMetadata('key2', 'domain');
184-
$this->assertEquals(null, $catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
198+
$this->assertNull($catalogue->getMetadata('key2', 'domain'), 'Metadata key2 should is deleted.');
185199
}
186200

187201
public function testMetadataMerge()

‎src/Symfony/Component/Translation/Translator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Translation/Translator.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ private function loadFallbackCatalogues($locale)
276276
$this->doLoadCatalogue($fallback);
277277
}
278278

279-
$current->addFallbackCatalogue($this->catalogues[$fallback]);
280-
$current = $this->catalogues[$fallback];
279+
$fallbackCatalogue = new MessageCatalogue($fallback, $this->catalogues[$fallback]->all());
280+
$current->addFallbackCatalogue($fallbackCatalogue);
281+
$current = $fallbackCatalogue;
281282
}
282283
}
283284

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.