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

Breadcrumb

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

trait FromCallableTrait

Hierarchy

  • trait \Symfony\Component\DependencyInjection\Loader\Configurator\Traits\FromCallableTrait

File

vendor/symfony/dependency-injection/Loader/Configurator/Traits/FromCallableTrait.php, line 20

Namespace

Symfony\Component\DependencyInjection\Loader\Configurator\Traits
View source
trait FromCallableTrait {
    public final function fromCallable(string|array|ReferenceConfigurator|Expression $callable) : FromCallableConfigurator {
        if ($this->definition instanceof ChildDefinition) {
            throw new InvalidArgumentException('The configuration key "parent" is unsupported when using "fromCallable()".');
        }
        foreach ([
            'synthetic' => 'isSynthetic',
            'factory' => 'getFactory',
            'file' => 'getFile',
            'arguments' => 'getArguments',
            'properties' => 'getProperties',
            'configurator' => 'getConfigurator',
            'calls' => 'getMethodCalls',
        ] as $key => $method) {
            if ($this->definition
                ->{$method}()) {
                throw new InvalidArgumentException(\sprintf('The configuration key "%s" is unsupported when using "fromCallable()".', $key));
            }
        }
        $this->definition
            ->setFactory([
            'Closure',
            'fromCallable',
        ]);
        if (\is_string($callable) && 1 === substr_count($callable, ':')) {
            $parts = explode(':', $callable);
            throw new InvalidArgumentException(\sprintf('Invalid callable "%s": the "service:method" notation is not available when using PHP-based DI configuration. Use "[service(\'%s\'), \'%s\']" instead.', $callable, $parts[0], $parts[1]));
        }
        if ($callable instanceof Expression) {
            $callable = '@=' . $callable;
        }
        $this->definition
            ->setArguments([
            static::processValue($callable, true),
        ]);
        if ('Closure' !== ($this->definition
            ->getClass() ?? 'Closure')) {
            $this->definition
                ->setLazy(true);
        }
        else {
            $this->definition
                ->setClass('Closure');
        }
        return new FromCallableConfigurator($this, $this->definition);
    }

}

Members

Title Sort descending Modifiers Object type Summary
FromCallableTrait::fromCallable final public function
RSS feed
Powered by Drupal