Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c7198c

Browse files
committed
Adding support to bind scalar values to controller arguments
1 parent d5a55a5 commit 2c7198c

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/Symfony/Component/DependencyInjection/Definition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ public function setAutowired($autowired)
838838
/**
839839
* Gets bindings.
840840
*
841-
* @return array
841+
* @return array|BoundArgument[]
842842
*/
843843
public function getBindings()
844844
{

src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,17 @@ public function process(ContainerBuilder $container)
136136
$binding = $bindings[$bindingName];
137137

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

140141
if (!$bindingValue instanceof Reference) {
141-
continue;
142+
$args[$p->name] = new Reference('value.'.$container->hash($bindingValue));
143+
$container->register((string) $args[$p->name], 'mixed')
144+
->setFactory('current')
145+
->addArgument(array($bindingValue));
146+
} else {
147+
$args[$p->name] = $bindingValue;
142148
}
143149

144-
$binding->setValues(array($bindingValue, $bindingId, true));
145-
$args[$p->name] = $bindingValue;
146-
147150
continue;
148151
} elseif (!$type || !$autowire) {
149152
continue;

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public function provideBindings()
311311
return array(array(ControllerDummy::class), array('$bar'));
312312
}
313313

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

323+
$container->setParameter('foo', 'foo_val');
324+
323325
$pass = new RegisterControllerArgumentLocatorsPass();
324326
$pass->process($container);
325327

326328
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
327-
$this->assertEmpty($locator);
329+
330+
$locator = $container->getDefinition((string) $locator['foo::fooAction']->getValues()[0]);
331+
332+
// assert the locator has a someArg key
333+
$arguments = $locator->getArgument(0);
334+
$this->assertArrayHasKey('someArg', $arguments);
335+
$this->assertInstanceOf(ServiceClosureArgument::class, $arguments['someArg']);
336+
// get the Reference that someArg points to
337+
$reference = $arguments['someArg']->getValues()[0];
338+
// make sure this service *does* exist and returns the correct value
339+
$this->assertTrue($container->has((string) $reference));
340+
$this->assertSame('foo_val', $container->get((string) $reference));
328341
}
329342
}
330343

0 commit comments

Comments
 (0)