8000 [Translation] added detection for circular references when adding a f… · ftassi/symfony@45b218e · GitHub
[go: up one dir, main page]

Skip to content

Commit 45b218e

Browse files
committed
[Translation] added detection for circular references when adding a fallback catalogue
1 parent 632a99f commit 45b218e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Symfony/Component/Translation/MessageCatalogue.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MessageCatalogue implements MessageCatalogueInterface
2626
private $locale;
2727
private $resources;
2828
private $fallbackCatalogue;
29+
private $parent;
2930

3031
/**
3132
* Constructor.
@@ -183,13 +184,32 @@ public function addCatalogue(MessageCatalogueInterface $catalogue)
183184
*/
184185
public function addFallbackCatalogue(MessageCatalogueInterface $catalogue)
185186
{
187+
// detect circular references
188+
$c = $this;
189+
do {
190+
if ($c->getLocale() === $catalogue->getLocale()) {
191+
throw new \LogicException(sprintf('Circular reference detected when adding a fallback catalogue for locale "%s".', $catalogue->getLocale()));
192+
}
193+
} while ($c = $c->getParent());
194+
195+
$catalogue->setParent($this);
186196
$this->fallbackCatalogue = $catalogue;
187197

188198
foreach ($catalogue->getResources() as $resource) {
189199
$this->addResource($resource);
190200
}
191201
}
192202

203+
public function getParent()
204+
{
205+
return $this->parent;
206+
}
207+
208+
public function setParent(self $parent)
209+
{
210+
$this->parent = $parent;
211+
}
212+
193213
/**
194214
* {@inheritdoc}
195215
*

tests/Symfony/Tests/Component/Translation/MessageCatalogTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ public function testAddFallbackCatalogue()
124124
$this->assertEquals(array($r, $r1), $catalogue->getResources());
125125
}
126126

127+
/**
128+
* @expectedException LogicException
129+
*/
130+
public function testAddFallbackCatalogueWithCircularReference()
131+
{
132+
$main = new MessageCatalogue('en_US');
133+
$fallback = new MessageCatalogue('fr_FR');
134+
135+
$fallback->addFallbackCatalogue($main);
136+
$main->addFallbackCatalogue($fallback);
137+
}
138+
127139
/**
128140
* @expectedException LogicException
129141
*/

0 commit comments

Comments
 (0)
0