10000 Adding support to bind scalar values to controller arguments by weaverryan · Pull Request #26658 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Adding support to bind scalar values to controller arguments #26658

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

Merged
merged 1 commit into from
Mar 25, 2018
Merged
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
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ public function setAutowired($autowired)
/**
* Gets bindings.
*
* @return array
* @return array|BoundArgument[]
*/
public function getBindings()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,17 @@ public function process(ContainerBuilder $container)
$binding = $bindings[$bindingName];

list($bindingValue, $bindingId) = $binding->getValues();
$binding->setValues(array($bindingValue, $bindingId, true));

if (!$bindingValue instanceof Reference) {
continue;
$args[$p->name] = new Reference('value.'.$container->hash($bindingValue));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use _value as prefix, to reduce the likeliness of conflicts with an actual service, as the YAML format forbids using the underscore prefix in service ids since 4.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in ba05588

< 8000 /details>
$container->register((string) $args[$p->name], 'mixed')
->setFactory('current')
->addArgument(array($bindingValue));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this service is here to build a scalar, I should we should mark it as non-shared, to avoiding registering the scalar in $this->services

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? what's the issue with sharing a scalar (or array btw?)
looks better shared to me, did I miss something?

} else {
$args[$p->name] = $bindingValue;
}

$binding->setValues(array($bindingValue, $bindingId, true));
$args[$p->name] = $bindingValue;

continue;
} elseif (!$type || !$autowire) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function provideBindings()
return array(array(ControllerDummy::class), array('$bar'));
}

public function testDoNotBindScalarValueToControllerArgument()
public function testBindScalarValueToControllerArgument()
{
$container = new ContainerBuilder();
$resolver = $container->register('argument_resolver.service')->addArgument(array());
Expand All @@ -320,11 +320,24 @@ public function testDoNotBindScalarValueToControllerArgument()
->setBindings(array('$someArg' => '%foo%'))
->addTag('controller.service_arguments');

$container->setParameter('foo', 'foo_val');

$pass = new RegisterControllerArgumentLocatorsPass();
$pass->process($container);

$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
$this->assertEmpty($locator);

$locator = $container->getDefinition((string) $locator['foo::fooAction']->getValues()[0]);

// assert the locator has a someArg key
$arguments = $locator->getArgument(0);
$this->assertArrayHasKey('someArg', $arguments);
$this->assertInstanceOf(ServiceClosureArgument::class, $arguments['someArg']);
// get the Reference that someArg points to
$reference = $arguments['someArg']->getValues()[0];
// make sure this service *does* exist and returns the correct value
$this->assertTrue($container->has((string) $reference));
$this->assertSame('foo_val', $container->get((string) $reference));
}
}

Expand Down
0