Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Autowire.php

class Autowire

Attribute to tell a parameter how to be autowired.

@author Kevin Bond <kevinbond@gmail.com>

Hierarchy

  • class \Symfony\Component\DependencyInjection\Attribute\Autowire

Expanded class hierarchy of Autowire

6 files declare their use of Autowire
AutowirePass.php in vendor/symfony/dependency-injection/Compiler/AutowirePass.php
AutowireTrait.php in core/lib/Drupal/Core/DependencyInjection/AutowireTrait.php
ModuleInstaller.php in core/lib/Drupal/Core/Extension/ModuleInstaller.php
RegisterControllerArgumentLocatorsPass.php in vendor/symfony/http-kernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php
RegisterServiceSubscribersPass.php in vendor/symfony/dependency-injection/Compiler/RegisterServiceSubscribersPass.php

... See full list

2 string references to 'Autowire'
XmlDumper::addService in vendor/symfony/dependency-injection/Dumper/XmlDumper.php
XmlFileLoader::parseDefinition in vendor/symfony/dependency-injection/Loader/XmlFileLoader.php
Parses an individual Definition.

File

vendor/symfony/dependency-injection/Attribute/Autowire.php, line 24

Namespace

Symfony\Component\DependencyInjection\Attribute
View source
class Autowire {
    public readonly string|array|Expression|Reference|ArgumentInterface|null $value;
    public readonly bool|array $lazy;
    
    /**
     * Use only ONE of the following.
     *
     * @param string|array|ArgumentInterface|null $value      Value to inject (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')
     * @param bool|class-string|class-string[]    $lazy       Whether to use lazy-loading for this argument
     */
    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 (!(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, '@')) {
            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,
        
        };
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
Autowire::$lazy public property
Autowire::$value public property
Autowire::__construct public function Use only ONE of the following. 4

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal