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 4a602ca

Browse filesBrowse files
committed
feature #26658 Adding support to bind scalar values to controller arguments (weaverryan)
This PR was merged into the 4.1-dev branch. Discussion ---------- Adding support to bind scalar values to controller arguments | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25960 | License | MIT | Doc PR | symfony/symfony-docs#9477 Hi guys! This fixes (I think) the last rough edge with autowiring & error messages. 100% credit to @nicolas-grekas for this implementation - he has generously allowed me to steal his code in return for writing the test. I did test this on a real project. Cheers! Commits ------- 2c7198c Adding support to bind scalar values to controller arguments
2 parents ce66ef0 + 2c7198c commit 4a602ca
Copy full SHA for 4a602ca

File tree

Expand file treeCollapse file tree

3 files changed

+23
-7
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+23
-7
lines changed

‎src/Symfony/Component/DependencyInjection/Definition.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Definition.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php
+7-4Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php
+15-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.