10000 [DependencyInjection] Add `env` and `param` parameters for Autowire attribute by alexndlm · Pull Request #48147 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] Add env and param parameters for Autowire attribute #48147

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
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 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ class Autowire
/**
* Use only ONE of the following.
*
* @param string|null $value Parameter value (ie "%kernel.project_dir%/some/path")
* @param string|null $service Service ID (ie "some.service")
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
* @param string|array|null $value Parameter value (ie "%kernel.project_dir%/some/path")
* @param string|null $service Service ID (ie "some.service")
* @param string|null $expression Expression (ie 'service("some.service").someMethod()')
* @param string|null $env Environment variable name (ie 'SOME_ENV_VARIABLE')
* @param string|null $param Parameter name (ie 'some.parameter.name')
*/
public function __construct(
string|array $value = null,
string $service = null,
string $expression = null,
string $env = null,
string $param = null,
) {
if (!($service xor $expression xor null !== $value)) {
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, or $value.');
if (!(null !== $value xor null !== $service xor null !== $expression xor null !== $env xor null !== $param)) {
throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, $env, $param or $value.');
}

if (\is_string($value) && str_starts_with($value, '@')) {
Expand All @@ -52,6 +56,8 @@ public function __construct(
$this->value = match (true) {
null !== $service => new Reference($service),
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".'),
null !== $env => "%env($env)%",
null !== $param => "%$param%",
null !== $value => $value,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

class AutowireTest extends TestCase
{
public function testCanOnlySetOneParameter()
/**
* @dataProvider provideMultipleParameters
*/
public function testCanOnlySetOneParameter(array $parameters)
{
$this->expectException(LogicException::class);

new Autowire(service: 'id', expression: 'expr');
new Autowire(...$parameters);
}

public function testMustSetOneParameter()
Expand Down Expand Up @@ -57,4 +60,26 @@ public function testCanUseValueWithAtAndEqualSign()
{
$this->assertInstanceOf(Expression::class, (new Autowire(value: '@=service'))->value);
}

public function testCanUseEnv()
{
$this->assertSame('%env(SOME_ENV_VAR)%', (new Autowire(env: 'SOME_ENV_VAR'))->value);
}

public function testCanUseParam()
{
$this->assertSame('%some.param%', (new Autowire(param: 'some.param'))->value);
}

/**
* @see testCanOnlySetOneParameter
*/
private function provideMultipleParameters(): iterable
{
yield [['service' => 'id', 'expression' => 'expr']];

yield [['env' => 'ENV', 'param' => 'param']];

yield [['value' => 'some-value', 'expression' => 'expr']];
}
}
0