Skip to content

Commit 4970770

Browse files
committed
merged branch GromNaN/di-dump-exception (PR #8494)
This PR was merged into the 2.2 branch. Discussion ---------- [DependencyInjection] Add exception for service name not dumpable in PHP | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8485 #8030 | License | MIT | Doc PR | n/a Throws an exception when the DIC is dumped to PHP, before generating invalid PHP. The regex comes from the PHP doc: http://www.php.net/manual/en/language.oop5.basic.php Commits ------- 242b318 [DependencyInjection] Add exception for service name not dumpable in PHP
2 parents c3d7eb9 + 242b318 commit 4970770

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ private function addServiceConfigurator($id, $definition, $variableName = 'insta
436436
*/
437437
private function addService($id, $definition)
438438
{
439-
$name = Container::camelize($id);
440439
$this->definitionVariables = new \SplObjectStorage();
441440
$this->referenceVariables = array();
442441
$this->variableCount = 0;
@@ -479,7 +478,7 @@ private function addService($id, $definition)
479478
*
480479
* $return
481480
*/
482-
protected function get{$name}Service()
481+
protected function {$this->getMethodName($id)}()
483482
{
484483
485484
EOF;
@@ -527,7 +526,6 @@ protected function get{$name}Service()
527526
*/
528527
private function addServiceAlias($alias, $id)
529528
{
530-
$name = Container::camelize($alias);
531529
$type = 'Object';
532530

533531
if ($this->container->hasDefinition($id)) {
@@ -542,14 +540,34 @@ private function addServiceAlias($alias, $id)
542540
*
543541
* @return $type An instance of the $id service
544542
*/
545-
protected function get{$name}Service()
543+
protected function {$this->getMethodName($alias)}()
546544
{
547545
return {$this->getServiceCall($id)};
548546
}
549547
550548
EOF;
551549
}
552550

551+
/**
552+
* Convert a service id to a valid PHP method name.
553+
*
554+
* @param string $id
555+
*
556+
* @return string
557+
*
558+
* @throws InvalidArgumentException
559+
*/
560+
private function getMethodName($id)
561+
{
562+
$name = Container::camelize($id);
563+
564+
if (!preg_match('/^[a-zA-Z0-9_\x7f-\xff]+$/', $name)) {
565+
throw new InvalidArgumentException(sprintf('Service id "%s" cannot be converted to a valid PHP method name.', $id));
566+
}
567+
568+
return 'get'.$name.'Service';
569+
}
570+
553571
/**
554572
* Adds multiple services
555573
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ public function testAddService()
120120
}
121121
}
122122

123+
/**
124+
* @expectedException InvalidArgumentException
125+
* @expectedExceptionMessage Service id "bar$" cannot be converted to a valid PHP method name.
126+
*/
127+
public function testAddServiceInvalidServiceId()
128+
{
129+
$container = new ContainerBuilder();
130+
$container->register('bar$', 'FooClass');
131+
$dumper = new PhpDumper($container);
132+
$dumper->dump();
133+
}
134+
123135
public function testOverrideServiceWhenUsingADumpedContainer()
124136
{
125137
require_once self::$fixturesPath.'/php/services9.php';

0 commit comments

Comments
 (0)