Skip to content

Commit e2273b5

Browse files
committed
feature #45512 [DependencyInjection] Allow using expressions as service factories (nicolas-grekas, jvasseur)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [DependencyInjection] Allow using expressions as service factories | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - Replaces #45447 This PR allows using expressions as service factories: - in YAML: `factory: '@=service("foo").bar()'` - in PHP: `->factory(expr('service("foo").bar()'))` - in XML: `<factory expression="service('foo').bar()" />` In addition, it allows the corresponding expressions to get access to the arguments of the service definition using the `arg($index)` function and `args` variable inside expressions: ```yaml services: foo: factory: '@=arg(0).baz()' # works also: @=args.get(0).baz() arguments: ['@bar'] ``` Internally, instead of allowing `Expression` objects in `Definition` objects as in #45447, factory expressions are conveyed as a strings that starts with `@=`. This is chosen by taking inspiration from yaml and to not collide with any existing callable. Commits ------- c43098919c [DependencyInjection] Allow using expressions as service factories
2 parents f6334df + 2162113 commit e2273b5

File tree

2 files changed

+2
-9
lines changed

2 files changed

+2
-9
lines changed

DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,7 @@ public function process(ContainerBuilder $container)
142142
[$bindingValue, $bindingId, , $bindingType, $bindingFile] = $binding->getValues();
143143
$binding->setValues([$bindingValue, $bindingId, true, $bindingType, $bindingFile]);
144144

145-
if (!$bindingValue instanceof Reference) {
146-
$args[$p->name] = new Reference('.value.'.$container->hash($bindingValue));
147-
$container->register((string) $args[$p->name], 'mixed')
148-
->setFactory('current')
149-
->addArgument([$bindingValue]);
150-
} else {
151-
$args[$p->name] = $bindingValue;
152-
}
145+
$args[$p->name] = $bindingValue;
153146

154147
continue;
155148
} elseif (!$autowire || (!($autowireAttributes ??= $p->getAttributes(Autowire::class)) && (!$type || '\\' !== $target[0]))) {

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"symfony/config": "^5.4|^6.0",
2929
"symfony/console": "^5.4|^6.0",
3030
"symfony/css-selector": "^5.4|^6.0",
31-
"symfony/dependency-injection": "^5.4|^6.0",
31+
"symfony/dependency-injection": "^6.1",
3232
"symfony/dom-crawler": "^5.4|^6.0",
3333
"symfony/expression-language": "^5.4|^6.0",
3434
"symfony/finder": "^5.4|^6.0",

0 commit comments

Comments
 (0)