Skip to content

Commit 05170c8

Browse files
ro0NLnicolas-grekas
authored andcommitted
[DI] Handle root namespace in service definitions
1 parent 3c9958c commit 05170c8

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,9 @@ private function addServiceReturn($id, $definition)
377377
*/
378378
private function addServiceInstance($id, Definition $definition)
379379
{
380-
$class = $definition->getClass();
381-
382-
if ('\\' === substr($class, 0, 1)) {
383-
$class = substr($class, 1);
384-
}
385-
386-
$class = $this->dumpValue($class);
380+
$class = $this->dumpValue($definition->getClass());
387381

388-
if (0 === strpos($class, "'") && !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
382+
if (0 === strpos($class, "'") && !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
389383
throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
390384
}
391385

@@ -1440,11 +1434,13 @@ private function dumpLiteralClass($class)
14401434
if (false !== strpos($class, '$')) {
14411435
throw new RuntimeException('Cannot dump definitions which have a variable class name.');
14421436
}
1443-
if (0 !== strpos($class, "'") || !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
1437+
if (0 !== strpos($class, "'") || !preg_match('/^\'(?:\\\{2})?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
14441438
throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s)', $class ?: 'n/a'));
14451439
}
14461440

1447-
return '\\'.substr(str_replace('\\\\', '\\', $class), 1, -1);
1441+
$class = substr(str_replace('\\\\', '\\', $class), 1, -1);
1442+
1443+
return 0 === strpos($class, '\\') ? $class : '\\'.$class;
14481444
}
14491445

14501446
/**

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,17 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic
339339

340340
$this->addToAssertionCount(1);
341341
}
342+
343+
public function testDumpHandlesLiteralClassWithRootNamespace()
344+
{
345+
$container = new ContainerBuilder();
346+
$container->register('foo', '\\stdClass');
347+
348+
$dumper = new PhpDumper($container);
349+
eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace')));
350+
351+
$container = new \Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace();
352+
353+
$this->assertInstanceOf('stdClass', $container->get('foo'));
354+
}
342355
}

0 commit comments

Comments
 (0)