vendor/symfony/dependency-injection/Attribute/Autowire.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\DependencyInjection\Attribute;
  11. use Symfony\Component\DependencyInjection\Exception\LogicException;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. use Symfony\Component\ExpressionLanguage\Expression;
  14. /**
  15.  * Attribute to tell a parameter how to be autowired.
  16.  *
  17.  * @author Kevin Bond <kevinbond@gmail.com>
  18.  */
  19. #[\Attribute(\Attribute::TARGET_PARAMETER)]
  20. class Autowire
  21. {
  22.     public readonly string|Expression|Reference $value;
  23.     /**
  24.      * Use only ONE of the following.
  25.      *
  26.      * @param string|null $value      Parameter value (ie "%kernel.project_dir%/some/path")
  27.      * @param string|null $service    Service ID (ie "some.service")
  28.      * @param string|null $expression Expression (ie 'service("some.service").someMethod()')
  29.      */
  30.     public function __construct(
  31.         string $value null,
  32.         string $service null,
  33.         string $expression null,
  34.     ) {
  35.         if (!($service xor $expression xor null !== $value)) {
  36.             throw new LogicException('#[Autowire] attribute must declare exactly one of $service, $expression, or $value.');
  37.         }
  38.         if (null !== $value && str_starts_with($value'@')) {
  39.             match (true) {
  40.                 str_starts_with($value'@@') => $value substr($value1),
  41.                 str_starts_with($value'@=') => $expression substr($value2),
  42.                 default => $service substr($value1),
  43.             };
  44.         }
  45.         $this->value = match (true) {
  46.             null !== $service => new Reference($service),
  47.             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".'),
  48.             null !== $value => $value,
  49.         };
  50.     }
  51. }