<?php namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Symfony\Component\Config\Loader\ParamConfigurator; use Symfony\Component\DependencyInjection\Argument\AbstractArgument; use Symfony\Component\DependencyInjection\Argument\IteratorArgument; use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\DependencyInjection\Loader\UndefinedExtensionHandler; use Symfony\Component\ExpressionLanguage\Expression; class ContainerConfigurator extends AbstractConfigurator { public const FACTORY = 'container'; private array $instanceof; private int $anonymousCount = 0; public function __construct( private ContainerBuilder $container, private PhpFileLoader $loader, array &$instanceof, private string $path, private string $file, private ?string $env = null, ) { $this->instanceof = &$instanceof; } final public function extension(string $namespace, array $config, bool $prepend = false): void { if ($prepend) { $this->container->prependExtensionConfig($namespace, static::processValue($config)); return; } if (!$this->container->hasExtension($namespace)) { $extensions = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions())); throw new InvalidArgumentException(UndefinedExtensionHandler::getErrorMessage($namespace, $this->file, $namespace, $extensions)); } $this->container->loadFromExtension($namespace, static::processValue($config)); } final public function import(string $resource, ?string $type = null, bool|string $ignoreErrors = false): void { $this->loader->setCurrentDir(\dirname($this->path)); $this->loader->import($resource, $type, $ignoreErrors, $this->file); } final public function parameters(): ParametersConfigurator { return new ParametersConfigurator($this->container); } final public function services(): ServicesConfigurator { return new ServicesConfigurator($this->container, $this->loader, $this->instanceof, $this->path, $this->anonymousCount); } final public function env(): ?string { return $this->env; } final public function withPath(string $path): static { $clone = clone $this; $clone->path = $clone->file = $path; $clone->loader->setCurrentDir(\dirname($path)); return $clone; } } function param(string $name): ParamConfigurator { return new ParamConfigurator($name); } function service(string $serviceId): ReferenceConfigurator { return new ReferenceConfigurator($serviceId); } function inline_service(?string $class = null): InlineServiceConfigurator { return new InlineServiceConfigurator(new Definition($class)); } function service_locator(array $values): ServiceLocatorArgument { $values = AbstractConfigurator::processValue($values, true); return new ServiceLocatorArgument($values); } function iterator(array $values): IteratorArgument { return new IteratorArgument(AbstractConfigurator::processValue($values, true)); } function tagged_iterator(string $tag, ?string $indexAttribute = null, ?string $defaultIndexMethod = null, ?string $defaultPriorityMethod = null, string|array $exclude = [], bool $excludeSelf = true): TaggedIteratorArgument { return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod, (array) $exclude, $excludeSelf); } function tagged_locator(string $tag, ?string $indexAttribute = null, ?string $defaultIndexMethod = null, ?string $defaultPriorityMethod = null, string|array $exclude = [], bool $excludeSelf = true): ServiceLocatorArgument { return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true, $defaultPriorityMethod, (array) $exclude, $excludeSelf)); } function expr(string $expression): Expression { return new Expression($expression); } function abstract_arg(string $description): AbstractArgument { return new AbstractArgument($description); } function env(string $name): EnvConfigurator { return new EnvConfigurator($name); } function service_closure(string $serviceId): ClosureReferenceConfigurator { return new ClosureReferenceConfigurator($serviceId); } function closure(string|array|ReferenceConfigurator|Expression $callable): InlineServiceConfigurator { return (new InlineServiceConfigurator(new Definition('Closure'))) ->factory(['Closure', 'fromCallable']) ->args([$callable]); }