Skip to content

Commit d5c9e49

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: Set the redraw frequency at least to 1. Setting it to 0 would otherwise produce an error. [Process] Unset callback after stop to free memory Improve error message for undefined DIC aliases [VarDumper] fixed .sf-dump z-index [DependencyInjection] Validate class names and factory methods ampq → amqp Fix typo CS: remove unneeded parentheses around control statements [TwigBridge] Clean deps now that 2.8.0 is tagged
2 parents 1ac8ce1 + 87b80a2 commit d5c9e49

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

Compiler/ReplaceAliasByActualDefinitionPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function process(ContainerBuilder $container)
4545
try {
4646
$definition = $container->getDefinition($aliasId);
4747
} catch (InvalidArgumentException $e) {
48-
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with "%s".', $alias, $id), null, $e);
48+
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $id, $alias), null, $e);
4949
}
5050

5151
if ($definition->isPublic()) {

Dumper/PhpDumper.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,10 @@ private function addNewInstance($id, Definition $definition, $return, $instantia
780780
if (null !== $definition->getFactory()) {
781781
$callable = $definition->getFactory();
782782
if (is_array($callable)) {
783+
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $callable[1])) {
784+
throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $callable[1] ?: 'n/a'));
785+
}
786+
783787
if ($callable[0] instanceof Reference
784788
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
785789
return sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->dumpValue($callable[0]), $callable[1], $arguments ? implode(', ', $arguments) : '');
@@ -1331,8 +1335,12 @@ private function dumpValue($value, $interpolate = true)
13311335
}
13321336

13331337
if (is_array($factory)) {
1338+
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $factory[1])) {
1339+
throw new RuntimeException(sprintf('Cannot dump definition because of invalid factory method (%s)', $factory[1] ?: 'n/a'));
1340+
}
1341+
13341342
if (is_string($factory[0])) {
1335-
return sprintf('\\%s::%s(%s)', $factory[0], $factory[1], implode(', ', $arguments));
1343+
return sprintf('%s::%s(%s)', $this->dumpLiteralClass($this->dumpValue($factory[0])), $factory[1], implode(', ', $arguments));
13361344
}
13371345

13381346
if ($factory[0] instanceof Definition) {
@@ -1363,12 +1371,8 @@ private function dumpValue($value, $interpolate = true)
13631371
if (null === $class) {
13641372
throw new RuntimeException('Cannot dump definitions which have no class nor factory.');
13651373
}
1366-
$class = $this->dumpValue($class);
1367-
if (false !== strpos($class, '$')) {
1368-
throw new RuntimeException('Cannot dump definitions which have a variable class name.');
1369-
}
13701374

1371-
return sprintf('new \\%s(%s)', substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
1375+
return sprintf('new %s(%s)', $this->dumpLiteralClass($this->dumpValue($class)), implode(', ', $arguments));
13721376
} elseif ($value instanceof Variable) {
13731377
return '$'.$value;
13741378
} elseif ($value instanceof Reference) {
@@ -1409,9 +1413,18 @@ private function dumpValue($value, $interpolate = true)
14091413
* @param string $class
14101414
*
14111415
* @return string
1416+
*
1417+
* @throws RuntimeException
14121418
*/
14131419
private function dumpLiteralClass($class)
14141420
{
1421+
if (false !== strpos($class, '$')) {
1422+
throw new RuntimeException('Cannot dump definitions which have a variable class name.');
1423+
}
1424+
if (0 !== strpos($class, "'") || !preg_match('/^\'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\\\{2}[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)*\'$/', $class)) {
1425+
throw new RuntimeException(sprintf('Cannot dump definition because of invalid class name (%s)', $class ?: 'n/a'));
1426+
}
1427+
14151428
return '\\'.substr(str_replace('\\\\', '\\', $class), 1, -1);
14161429
}
14171430

Tests/Dumper/PhpDumperTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,31 @@ public function testAddServiceInvalidServiceId()
154154
$dumper->dump();
155155
}
156156

157+
/**
158+
* @dataProvider provideInvalidFactories
159+
* @expectedException Symfony\Component\DependencyInjection\Exception\RuntimeException
160+
* @expectedExceptionMessage Cannot dump definition
161+
*/
162+
public function testInvalidFactories($factory)
163+
{
164+
$container = new ContainerBuilder();
165+
$def = new Definition('stdClass');
166+
$def->setFactory($factory);
167+
$container->setDefinition('bar', $def);
168+
$dumper = new PhpDumper($container);
169+
$dumper->dump();
170+
}
171+
172+
public function provideInvalidFactories()
173+
{
174+
return array(
175+
array(array('', 'method')),
176+
array(array('class', '')),
177+
array(array('...', 'method')),
178+
array(array('class', '...')),
179+
);
180+
}
181+
157182
public function testAliases()
158183
{
159184
$container = include self::$fixturesPath.'/containers/container9.php';

0 commit comments

Comments
 (0)