[DependencyInjection] Add exception for service name not dumpable in PHP

This commit is contained in:
Jérôme Tamarelle 2013-07-19 09:14:04 +02:00
parent 40f7e6875f
commit 9ac3556825
2 changed files with 35 additions and 6 deletions

View File

@ -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 <<<EOF
/**
* Updates the '$id' service.
*/
protected function synchronize{$name}Service()
protected function synchronize{$this->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
*

View File

@ -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';