[DependencyInjection] Add exception for service name not dumpable in PHP
This commit is contained in:
parent
40f7e6875f
commit
9ac3556825
@ -490,7 +490,6 @@ class PhpDumper extends Dumper
|
|||||||
*/
|
*/
|
||||||
private function addService($id, $definition)
|
private function addService($id, $definition)
|
||||||
{
|
{
|
||||||
$name = Container::camelize($id);
|
|
||||||
$this->definitionVariables = new \SplObjectStorage();
|
$this->definitionVariables = new \SplObjectStorage();
|
||||||
$this->referenceVariables = array();
|
$this->referenceVariables = array();
|
||||||
$this->variableCount = 0;
|
$this->variableCount = 0;
|
||||||
@ -555,7 +554,7 @@ EOF;
|
|||||||
*$lazyInitializationDoc
|
*$lazyInitializationDoc
|
||||||
* $return
|
* $return
|
||||||
*/
|
*/
|
||||||
{$visibility} function get{$name}Service($lazyInitialization)
|
{$visibility} function get{$this->camelize($id)}Service($lazyInitialization)
|
||||||
{
|
{
|
||||||
|
|
||||||
EOF;
|
EOF;
|
||||||
@ -656,14 +655,12 @@ EOF;
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = Container::camelize($id);
|
|
||||||
|
|
||||||
return <<<EOF
|
return <<<EOF
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the '$id' service.
|
* Updates the '$id' service.
|
||||||
*/
|
*/
|
||||||
protected function synchronize{$name}Service()
|
protected function synchronize{$this->camelize($id)}Service()
|
||||||
{
|
{
|
||||||
$code }
|
$code }
|
||||||
|
|
||||||
@ -835,7 +832,7 @@ EOF;
|
|||||||
$code = " \$this->methodMap = array(\n";
|
$code = " \$this->methodMap = array(\n";
|
||||||
ksort($definitions);
|
ksort($definitions);
|
||||||
foreach ($definitions as $id => $definition) {
|
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";
|
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
|
* Returns the next name to use
|
||||||
*
|
*
|
||||||
|
@ -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()
|
public function testAliases()
|
||||||
{
|
{
|
||||||
$container = include self::$fixturesPath.'/containers/container9.php';
|
$container = include self::$fixturesPath.'/containers/container9.php';
|
||||||
|
Reference in New Issue
Block a user