From 9ac35568251b0902e1bd2ce6d09f61154fb18e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 19 Jul 2013 09:14:04 +0200 Subject: [PATCH] [DependencyInjection] Add exception for service name not dumpable in PHP --- .../DependencyInjection/Dumper/PhpDumper.php | 29 +++++++++++++++---- .../Tests/Dumper/PhpDumperTest.php | 12 ++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index cd8366ec74..8888d3f3f4 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -490,7 +490,6 @@ class PhpDumper extends Dumper */ private function addService($id, $definition) { - $name = Container::camelize($id); $this->definitionVariables = new \SplObjectStorage(); $this->referenceVariables = array(); $this->variableCount = 0; @@ -555,7 +554,7 @@ EOF; *$lazyInitializationDoc * $return */ - {$visibility} function get{$name}Service($lazyInitialization) + {$visibility} function get{$this->camelize($id)}Service($lazyInitialization) { EOF; @@ -656,14 +655,12 @@ EOF; return; } - $name = Container::camelize($id); - return <<camelize($id)}Service() { $code } @@ -835,7 +832,7 @@ EOF; $code = " \$this->methodMap = array(\n"; ksort($definitions); foreach ($definitions as $id => $definition) { - $code .= ' '.var_export($id, true).' => '.var_export('get'.Container::camelize($id).'Service', true).",\n"; + $code .= ' '.var_export($id, true).' => '.var_export('get'.$this->camelize($id).'Service', true).",\n"; } return $code . " );\n"; @@ -1260,6 +1257,26 @@ EOF; } } + /** + * Convert a service id to a valid PHP method name. + * + * @param string $id + * + * @return string + * + * @throws InvalidArgumentException + */ + private function camelize($id) + { + $name = Container::camelize($id); + + if (!preg_match('/^[a-zA-Z0-9_\x7f-\xff]+$/', $name)) { + throw new InvalidArgumentException(sprintf('Service id "%s" cannot be converted to a valid PHP method name.', $id)); + } + + return $name; + } + /** * Returns the next name to use * diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 1979d102d4..8bd31b076c 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -120,6 +120,18 @@ class PhpDumperTest extends \PHPUnit_Framework_TestCase } } + /** + * @expectedException InvalidArgumentException + * @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name. + */ + public function testAddServiceInvalidServiceId() + { + $container = new ContainerBuilder(); + $container->register('bar$', 'FooClass'); + $dumper = new PhpDumper($container); + $dumper->dump(); + } + public function testAliases() { $container = include self::$fixturesPath.'/containers/container9.php';