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

[DependencyInjection] Make named arguments optional for prototypes #22152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions 43 src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,48 @@ public function registerClasses(Definition $prototype, $namespace, $resource)
}

$classes = $this->findClasses($namespace, $resource);

$unusedArguments = array();
foreach ($prototype->getArguments() as $key => $argument) {
if ('' === $key || '$' !== $key[0]) {
continue;
}

$unusedArguments[$key] = true;
}

// prepare for deep cloning
$prototype = serialize($prototype);

foreach ($classes as $class) {
$this->setDefinition($class, unserialize($prototype));
foreach ($classes as $class => $reflectionClass) {
$parameters = array();
if ($reflectionClass->hasMethod('__construct')) {
foreach ($reflectionClass->getMethod('__construct')->getParameters() as $parameter) {
$parameters['$'.$parameter->name] = true;
}
}

$definition = unserialize($prototype);

$arguments = array();
foreach ($definition->getArguments() as $key => $argument) {
if ('' !== $key && '$' === $key[0]) {
if (!isset($parameters[$key])) {
continue;
}

unset($unusedArguments[$key]);
}

$arguments[$key] = $argument;
}
$definition->setArguments($arguments);

$this->setDefinition($class, $definition);
}

if ($unusedArguments) {
throw new InvalidArgumentException(sprintf('Unused named arguments in a prototype: "%s".', implode('", "', array_keys($unusedArguments))));
}
}

Expand Down Expand Up @@ -104,7 +141,7 @@ private function findClasses($namespace, $resource)
continue;
}
if (!$r->isInterface() && !$r->isTrait()) {
$classes[] = $class;
$classes[$class] = $r;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

class Foo
{
public function __construct($bar = null)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
resource: ../Prototype
arguments:
$bar: foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\:
resource: ../Prototype
arguments:
$invalid: foo
$invalid2: quz
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype;
use Symfony\Component\DependencyInjection\Tests\Fixtures\NamedArgumentsDummy;
use Symfony\Component\ExpressionLanguage\Expression;
Expand Down Expand Up @@ -450,6 +449,33 @@ public function testNamedArguments()
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition('another_one')->getMethodCalls());
}

public function testNamedArgumentsInPrototypes()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('prototype_named_args.yml');

$fooDefinition = $container->getDefinition('Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo');
$this->assertEquals(array('$bar' => 'foo'), $fooDefinition->getArguments());

$this->assertEmpty($container->getDefinition('Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Sub\Bar')->getArguments());

$container->compile();

$this->assertEquals(array('foo'), $fooDefinition->getArguments());
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Unused named arguments in a prototype: "$invalid", "$invalid2".
*/
public function testUnusedNamedArgumentsInPrototypes()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('prototype_unused_named_args.yml');
}

public function testInstanceof()
{
$container = new ContainerBuilder();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.