Skip to content

Commit 9f45283

Browse files
committed
[DependencyInjection] Fix a limitation of the PhpDumper
1 parent bb2727a commit 9f45283

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class PhpDumper extends Dumper
5858
private $targetDirRegex;
5959
private $targetDirMaxMatches;
6060
private $docStar;
61+
private $allocatedNames = array();
6162

6263
/**
6364
* @var \Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface
@@ -1349,13 +1350,21 @@ private function getServiceCall($id, Reference $reference = null)
13491350
*/
13501351
private function camelize($id)
13511352
{
1353+
if (isset($this->allocatedNames[$id])) {
1354+
return $this->allocatedNames[$id];
1355+
}
1356+
13521357
$name = Container::camelize($id);
1358+
$finalName = $name = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', $name);
1359+
$prefix = 1;
13531360

1354-
if (!preg_match('/^[a-zA-Z0-9_\x7f-\xff]+$/', $name)) {
1355-
throw new InvalidArgumentException(sprintf('Service id "%s" cannot be converted to a valid PHP method name.', $id));
1361+
while (in_array($finalName, $this->allocatedNames)) {
1362+
$prefix++;
1363+
$finalName = $name.$prefix;
13561364
}
1365+
$this->allocatedNames[$id] = $finalName;
13571366

1358-
return $name;
1367+
return $finalName;
13591368
}
13601369

13611370
/**

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,17 @@ public function testServicesWithAnonymousFactories()
132132
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
133133
}
134134

135-
/**
136-
* @expectedException \InvalidArgumentException
137-
* @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
138-
*/
139-
public function testAddServiceInvalidServiceId()
135+
public function testAddServiceIdWithUnsupportedCharacters()
140136
{
141137
$container = new ContainerBuilder();
142138
$container->register('bar$', 'FooClass');
139+
$container->register('bar$!', 'FooClass');
143140
$dumper = new PhpDumper($container);
144-
$dumper->dump();
141+
eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Unsupported_Characters')));
142+
143+
$container = new \Symfony_DI_PhpDumper_Test_Unsupported_Characters();
144+
$this->assertTrue(method_exists($container, 'getBarService'));
145+
$this->assertTrue(method_exists($container, 'getBar2Service'));
145146
}
146147

147148
/**

0 commit comments

Comments
 (0)