Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2117a39

Browse filesBrowse files
committed
[DependencyInjection] Make named arguments optional for prototypes
1 parent 4149eab commit 2117a39
Copy full SHA for 2117a39

File tree

Expand file treeCollapse file tree

5 files changed

+81
-4
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+81
-4
lines changed

‎src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
+40-3Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,48 @@ public function registerClasses(Definition $prototype, $namespace, $resource)
5959
}
6060

6161
$classes = $this->findClasses($namespace, $resource);
62+
63+
$unusedArguments = array();
64+
foreach ($prototype->getArguments() as $key => $argument) {
65+
if ('' === $key || '$' !== $key[0]) {
66+
continue;
67+
}
68+
69+
$unusedArguments[$key] = true;
70+
}
71+
6272
// prepare for deep cloning
6373
$prototype = serialize($prototype);
6474

65-
foreach ($classes as $class) {
66-
$this->setDefinition($class, unserialize($prototype));
75+
foreach ($classes as $class => $reflectionClass) {
76+
$parameters = array();
77+
if ($reflectionClass->hasMethod('__construct')) {
78+
foreach ($reflectionClass->getMethod('__construct')->getParameters() as $parameter) {
79+
$parameters['$'.$parameter->name] = true;
80+
}
81+
}
82+
83+
$definition = unserialize($prototype);
84+
85+
$arguments = array();
86+
foreach ($definition->getArguments() as $key => $argument) {
87+
if ('' !== $key && '$' === $key[0]) {
88+
if (!isset($parameters[$key])) {
89+
continue;
90+
}
91+
92+
unset($unusedArguments[$key]);
93+
}
94+
95+
$arguments[$key] = $argument;
96+
}
97+
$definition->setArguments($arguments);
98+
99+
$this->setDefinition($class, $definition);
100+
}
101+
102+
if ($unusedArguments) {
103+
throw new InvalidArgumentException(sprintf('Unused named arguments in a prototype: "%s".', implode('", "', array_keys($unusedArguments))));
67104
}
68105
}
69106

@@ -104,7 +141,7 @@ private function findClasses($namespace, $resource)
104141
continue;
105142
}
106143
if (!$r->isInterface() && !$r->isTrait()) {
107-
$classes[] = $class;
144+
$classes[$class] = $r;
108145
}
109146
}
110147

‎src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/Foo.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Fixtures/Prototype/Foo.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44

55
class Foo
66
{
7+
public function __construct($bar = null)
8+
{
9+
}
710
}
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
services:
2+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
3+
resource: ../Prototype
4+
arguments:
5+
$bar: foo
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
3+
resource: ../Prototype
4+
arguments:
5+
$invalid: foo
6+
$invalid2: quz

‎src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
+27-1Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Symfony\Component\Config\Resource\DirectoryResource;
2525
use Symfony\Component\Config\Resource\FileResource;
2626
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
27-
use Symfony\Component\DependencyInjection\ContainerInterface;
2827
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
2928
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
3029
use Symfony\Component\ExpressionLanguage\Expression;
@@ -450,6 +449,33 @@ public function testNamedArguments()
450449
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition('another_one')->getMethodCalls());
451450
}
452451

452+
public function testNamedArgumentsInPrototypes()
453+
{
454+
$container = new ContainerBuilder();
455+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
456+
$loader->load('prototype_named_args.yml');
457+
458+
$fooDefinition = $container->getDefinition('Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo');
459+
$this->assertEquals(array('$bar' => 'foo'), $fooDefinition->getArguments());
460+
461+
$this->assertEmpty($container->getDefinition('Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar')->getArguments());
462+
463+
$container->compile();
464+
465+
$this->assertEquals(array('foo'), $fooDefinition->getArguments());
466+
}
467+
468+
/**
469+
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
470+
* @expectedExceptionMessage Unused named arguments in a prototype: "$invalid", "$invalid2".
471+
*/
472+
public function testUnusedNamedArgumentsInPrototypes()
473+
{
474+
$container = new ContainerBuilder();
475+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
476+
$loader->load('prototype_unused_named_args.yml');
477+
}
478+
453479
public function testInstanceof()
454480
{
455481
$container = new ContainerBuilder();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.