<?php
namespace Symfony\Component\DependencyInjection\Attribute;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
class Autowire
{
public readonly string|array|Expression|Reference|ArgumentInterface|null $value;
public readonly bool|array $lazy;
public function __construct(
string|array|ArgumentInterface|null $value = null,
?string $service = null,
?string $expression = null,
?string $env = null,
?string $param = null,
bool|string|array $lazy = false,
) {
if ($this->lazy = \is_string($lazy) ? [$lazy] : $lazy) {
if (null !== ($expression ?? $env ?? $param)) {
throw new LogicException('#[Autowire] attribute cannot be $lazy and use $expression, $env, or $param.');
}
if (null !== $value && null !== $service) {
throw new LogicException('#[Autowire] attribute cannot declare $value and $service at the same time.');
}
} elseif (1 !== (null !== $value) + (null !== $service) + (null !== $expression) + (null !== $env) + (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, '@')) {
match (true) {
str_starts_with($value, '@@') => $value = substr($value, 1),
str_starts_with($value, '@=') => $expression = substr($value, 2),
default => $service = substr($value, 1),
};
}
$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%",
default => $value,
};
}
}