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 fd855dd

Browse filesBrowse files
committed
feature #48147 [DependencyInjection] Add env and param parameters for Autowire attribute (alexndlm)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [DependencyInjection] Add `env` and `param` parameters for Autowire attribute | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | These parameters will help not to miss `%` when auto-wiring the env variable or parameter. Commits ------- ed94fdc [DependencyInjection] Add `env` and `param` parameters for Autowire attribute
2 parents 7dfa101 + ed94fdc commit fd855dd
Copy full SHA for fd855dd

File tree

2 files changed

+38
-7
lines changed
Filter options

2 files changed

+38
-7
lines changed

‎src/Symfony/Component/DependencyInjection/Attribute/Autowire.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
+11-5Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@ class Autowire
2828
/**
2929
* Use only ONE of the following.
3030
*
31-
* @param string|null $value Parameter value (ie "%kernel.project_dir%/some/path")
32-
* @param string|null $service Service ID (ie "some.service")
33-
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
31+
* @param string|array|null $value Parameter value (ie "%kernel.project_dir%/some/path")
32+
* @param string|null $service Service ID (ie "some.service")
33+
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
34+
* @param string|null $env Environment variable name (ie 'SOME_ENV_VARIABLE')
35+
* @param string|null $param Parameter name (ie 'some.parameter.name')
3436
*/
3537
public function __construct(
3638
string|array $value = null,
3739
string $service = null,
3840
string $expression = null,
41+
string $env = null,
42+
string $param = null,
3943
) {
40-
if (!($service xor $expression xor null !== $value)) {
41-
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, or $value.');
44+
if (!(null !== $value xor null !== $service xor null !== $expression xor null !== $env xor null !== $param)) {
45+
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, $env, $param or $value.');
4246
}
4347

4448
if (\is_string($value) && str_starts_with($value, '@')) {
@@ -52,6 +56,8 @@ public function __construct(
5256
$this->value = match (true) {
5357
null !== $service => new Reference($service),
5458
null !== $expression => class_exists(Expression::class) ? new Expression($expression) : throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed. Try running "composer require symfony/expression-language".'),
59+
null !== $env => "%env($env)%",
60+
null !== $param => "%$param%",
5561
null !== $value => $value,
5662
};
5763
}

‎src/Symfony/Component/DependencyInjection/Tests/Attribute/AutowireTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Attribute/AutowireTest.php
+27-2Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
class AutowireTest extends TestCase
2121
{
22-
public function testCanOnlySetOneParameter()
22+
/**
23+
* @dataProvider provideMultipleParameters
24+
*/
25+
public function testCanOnlySetOneParameter(array $parameters)
2326
{
2427
$this->expectException(LogicException::class);
2528

26-
new Autowire(service: 'id', expression: 'expr');
29+
new Autowire(...$parameters);
2730
}
2831

2932
public function testMustSetOneParameter()
@@ -57,4 +60,26 @@ public function testCanUseValueWithAtAndEqualSign()
5760
{
5861
$this->assertInstanceOf(Expression::class, (new Autowire(value: '@=service'))->value);
5962
}
63+
64+
public function testCanUseEnv()
65+
{
66+
$this->assertSame('%env(SOME_ENV_VAR)%', (new Autowire(env: 'SOME_ENV_VAR'))->value);
67+
}
68+
69+
public function testCanUseParam()
70+
{
71+
$this->assertSame('%some.param%', (new Autowire(param: 'some.param'))->value);
72+
}
73+
74+
/**
75+
* @see testCanOnlySetOneParameter
76+
*/
77+
private function provideMultipleParameters(): iterable
78+
{
79+
yield [['service' => 'id', 'expression' => 'expr']];
80+
81+
yield [['env' => 'ENV', 'param' => 'param']];
82+
83+
yield [['value' => 'some-value', 'expression' => 'expr']];
84+
}
6085
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.