[DependencyInjection] Add exception for service name not dumpable in PHP
This commit is contained in:
parent
76f918a125
commit
242b3184b9
|
@ -436,7 +436,6 @@ class PhpDumper extends Dumper
|
|||
*/
|
||||
private function addService($id, $definition)
|
||||
{
|
||||
$name = Container::camelize($id);
|
||||
$this->definitionVariables = new \SplObjectStorage();
|
||||
$this->referenceVariables = array();
|
||||
$this->variableCount = 0;
|
||||
|
@ -479,7 +478,7 @@ EOF;
|
|||
*
|
||||
* $return
|
||||
*/
|
||||
protected function get{$name}Service()
|
||||
protected function {$this->getMethodName($id)}()
|
||||
{
|
||||
|
||||
EOF;
|
||||
|
@ -527,7 +526,6 @@ EOF;
|
|||
*/
|
||||
private function addServiceAlias($alias, $id)
|
||||
{
|
||||
$name = Container::camelize($alias);
|
||||
$type = 'Object';
|
||||
|
||||
if ($this->container->hasDefinition($id)) {
|
||||
|
@ -542,7 +540,7 @@ EOF;
|
|||
*
|
||||
* @return $type An instance of the $id service
|
||||
*/
|
||||
protected function get{$name}Service()
|
||||
protected function {$this->getMethodName($alias)}()
|
||||
{
|
||||
return {$this->getServiceCall($id)};
|
||||
}
|
||||
|
@ -550,6 +548,26 @@ EOF;
|
|||
EOF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a service id to a valid PHP method name.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function getMethodName($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 'get'.$name.'Service';
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple services
|
||||
*
|
||||
|
|
|
@ -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 testOverrideServiceWhenUsingADumpedContainer()
|
||||
{
|
||||
require_once self::$fixturesPath.'/php/services9.php';
|
||||
|
|
Reference in New Issue