Update website
This commit is contained in:
parent
bb4b0f9be8
commit
011b183e28
4263 changed files with 3014 additions and 720369 deletions
|
@ -1,49 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
use Symfony\Component\Config\Loader\Loader;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
/**
|
||||
* ClosureLoader loads service definitions from a PHP closure.
|
||||
*
|
||||
* The Closure has access to the container as its first argument.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class ClosureLoader extends Loader
|
||||
{
|
||||
private $container;
|
||||
|
||||
public function __construct(ContainerBuilder $container, string $env = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
parent::__construct($env);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($resource, string $type = null)
|
||||
{
|
||||
return $resource($this->container, $this->env);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
return $resource instanceof \Closure;
|
||||
}
|
||||
}
|
|
@ -1,119 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\Config\Loader\ParamConfigurator;
|
||||
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
||||
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Parameter;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\ExpressionLanguage\Expression;
|
||||
|
||||
abstract class AbstractConfigurator
|
||||
{
|
||||
public const FACTORY = 'unknown';
|
||||
|
||||
/**
|
||||
* @var callable(mixed, bool)|null
|
||||
*/
|
||||
public static $valuePreProcessor;
|
||||
|
||||
/** @internal */
|
||||
protected $definition;
|
||||
|
||||
public function __call(string $method, array $args)
|
||||
{
|
||||
if (method_exists($this, 'set'.$method)) {
|
||||
return $this->{'set'.$method}(...$args);
|
||||
}
|
||||
|
||||
throw new \BadMethodCallException(sprintf('Call to undefined method "%s::%s()".', static::class, $method));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
|
||||
}
|
||||
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that a value is valid, optionally replacing Definition and Reference configurators by their configure value.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param bool $allowServices whether Definition and Reference are allowed; by default, only scalars and arrays are
|
||||
*
|
||||
* @return mixed the value, optionally cast to a Definition/Reference
|
||||
*/
|
||||
public static function processValue($value, $allowServices = false)
|
||||
{
|
||||
if (\is_array($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$value[$k] = static::processValue($v, $allowServices);
|
||||
}
|
||||
|
||||
return self::$valuePreProcessor ? (self::$valuePreProcessor)($value, $allowServices) : $value;
|
||||
}
|
||||
|
||||
if (self::$valuePreProcessor) {
|
||||
$value = (self::$valuePreProcessor)($value, $allowServices);
|
||||
}
|
||||
|
||||
if ($value instanceof ReferenceConfigurator) {
|
||||
$reference = new Reference($value->id, $value->invalidBehavior);
|
||||
|
||||
return $value instanceof ClosureReferenceConfigurator ? new ServiceClosureArgument($reference) : $reference;
|
||||
}
|
||||
|
||||
if ($value instanceof InlineServiceConfigurator) {
|
||||
$def = $value->definition;
|
||||
$value->definition = null;
|
||||
|
||||
return $def;
|
||||
}
|
||||
|
||||
if ($value instanceof ParamConfigurator) {
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
if ($value instanceof self) {
|
||||
throw new InvalidArgumentException(sprintf('"%s()" can be used only at the root of service configuration files.', $value::FACTORY));
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
case null === $value:
|
||||
case \is_scalar($value):
|
||||
return $value;
|
||||
|
||||
case $value instanceof ArgumentInterface:
|
||||
case $value instanceof Definition:
|
||||
case $value instanceof Expression:
|
||||
case $value instanceof Parameter:
|
||||
case $value instanceof AbstractArgument:
|
||||
case $value instanceof Reference:
|
||||
if ($allowServices) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Cannot use values of type "%s" in service configuration files.', get_debug_type($value)));
|
||||
}
|
||||
}
|
|
@ -1,115 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
||||
|
||||
abstract class AbstractServiceConfigurator extends AbstractConfigurator
|
||||
{
|
||||
protected $parent;
|
||||
protected $id;
|
||||
private $defaultTags = [];
|
||||
|
||||
public function __construct(ServicesConfigurator $parent, Definition $definition, string $id = null, array $defaultTags = [])
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->definition = $definition;
|
||||
$this->id = $id;
|
||||
$this->defaultTags = $defaultTags;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
// default tags should be added last
|
||||
foreach ($this->defaultTags as $name => $attributes) {
|
||||
foreach ($attributes as $attribute) {
|
||||
$this->definition->addTag($name, $attribute);
|
||||
}
|
||||
}
|
||||
$this->defaultTags = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a service.
|
||||
*/
|
||||
final public function set(?string $id, string $class = null): ServiceConfigurator
|
||||
{
|
||||
$this->__destruct();
|
||||
|
||||
return $this->parent->set($id, $class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an alias.
|
||||
*/
|
||||
final public function alias(string $id, string $referencedId): AliasConfigurator
|
||||
{
|
||||
$this->__destruct();
|
||||
|
||||
return $this->parent->alias($id, $referencedId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a PSR-4 namespace using a glob pattern.
|
||||
*/
|
||||
final public function load(string $namespace, string $resource): PrototypeConfigurator
|
||||
{
|
||||
$this->__destruct();
|
||||
|
||||
return $this->parent->load($namespace, $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an already defined service definition.
|
||||
*
|
||||
* @throws ServiceNotFoundException if the service definition does not exist
|
||||
*/
|
||||
final public function get(string $id): ServiceConfigurator
|
||||
{
|
||||
$this->__destruct();
|
||||
|
||||
return $this->parent->get($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an already defined service definition or alias.
|
||||
*/
|
||||
final public function remove(string $id): ServicesConfigurator
|
||||
{
|
||||
$this->__destruct();
|
||||
|
||||
return $this->parent->remove($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a stack of decorator services.
|
||||
*
|
||||
* @param InlineServiceConfigurator[]|ReferenceConfigurator[] $services
|
||||
*/
|
||||
final public function stack(string $id, array $services): AliasConfigurator
|
||||
{
|
||||
$this->__destruct();
|
||||
|
||||
return $this->parent->stack($id, $services);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a service.
|
||||
*/
|
||||
final public function __invoke(string $id, string $class = null): ServiceConfigurator
|
||||
{
|
||||
$this->__destruct();
|
||||
|
||||
return $this->parent->set($id, $class);
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class AliasConfigurator extends AbstractServiceConfigurator
|
||||
{
|
||||
use Traits\DeprecateTrait;
|
||||
use Traits\PublicTrait;
|
||||
|
||||
public const FACTORY = 'alias';
|
||||
|
||||
public function __construct(ServicesConfigurator $parent, Alias $alias)
|
||||
{
|
||||
$this->parent = $parent;
|
||||
$this->definition = $alias;
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
class ClosureReferenceConfigurator extends ReferenceConfigurator
|
||||
{
|
||||
}
|
|
@ -1,212 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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\ExpressionLanguage\Expression;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class ContainerConfigurator extends AbstractConfigurator
|
||||
{
|
||||
public const FACTORY = 'container';
|
||||
|
||||
private $container;
|
||||
private $loader;
|
||||
private $instanceof;
|
||||
private $path;
|
||||
private $file;
|
||||
private $anonymousCount = 0;
|
||||
private $env;
|
||||
|
||||
public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file, string $env = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->loader = $loader;
|
||||
$this->instanceof = &$instanceof;
|
||||
$this->path = $path;
|
||||
$this->file = $file;
|
||||
$this->env = $env;
|
||||
}
|
||||
|
||||
final public function extension(string $namespace, array $config)
|
||||
{
|
||||
if (!$this->container->hasExtension($namespace)) {
|
||||
$extensions = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
|
||||
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $this->file, $namespace, $extensions ? implode('", "', $extensions) : 'none'));
|
||||
}
|
||||
|
||||
$this->container->loadFromExtension($namespace, static::processValue($config));
|
||||
}
|
||||
|
||||
final public function import(string $resource, string $type = null, $ignoreErrors = false)
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current environment to be able to write conditional configuration.
|
||||
*/
|
||||
final public function env(): ?string
|
||||
{
|
||||
return $this->env;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return static
|
||||
*/
|
||||
final public function withPath(string $path): self
|
||||
{
|
||||
$clone = clone $this;
|
||||
$clone->path = $clone->file = $path;
|
||||
$clone->loader->setCurrentDir(\dirname($path));
|
||||
|
||||
return $clone;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parameter.
|
||||
*/
|
||||
function param(string $name): ParamConfigurator
|
||||
{
|
||||
return new ParamConfigurator($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a service reference.
|
||||
*
|
||||
* @deprecated since Symfony 5.1, use service() instead.
|
||||
*/
|
||||
function ref(string $id): ReferenceConfigurator
|
||||
{
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', '"%s()" is deprecated, use "service()" instead.', __FUNCTION__);
|
||||
|
||||
return new ReferenceConfigurator($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a reference to a service.
|
||||
*/
|
||||
function service(string $serviceId): ReferenceConfigurator
|
||||
{
|
||||
return new ReferenceConfigurator($serviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an inline service.
|
||||
*
|
||||
* @deprecated since Symfony 5.1, use inline_service() instead.
|
||||
*/
|
||||
function inline(string $class = null): InlineServiceConfigurator
|
||||
{
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', '"%s()" is deprecated, use "inline_service()" instead.', __FUNCTION__);
|
||||
|
||||
return new InlineServiceConfigurator(new Definition($class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an inline service.
|
||||
*/
|
||||
function inline_service(string $class = null): InlineServiceConfigurator
|
||||
{
|
||||
return new InlineServiceConfigurator(new Definition($class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a service locator.
|
||||
*
|
||||
* @param ReferenceConfigurator[] $values
|
||||
*/
|
||||
function service_locator(array $values): ServiceLocatorArgument
|
||||
{
|
||||
return new ServiceLocatorArgument(AbstractConfigurator::processValue($values, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a lazy iterator.
|
||||
*
|
||||
* @param ReferenceConfigurator[] $values
|
||||
*/
|
||||
function iterator(array $values): IteratorArgument
|
||||
{
|
||||
return new IteratorArgument(AbstractConfigurator::processValue($values, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a lazy iterator by tag name.
|
||||
*/
|
||||
function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null): TaggedIteratorArgument
|
||||
{
|
||||
return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, false, $defaultPriorityMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a service locator by tag name.
|
||||
*/
|
||||
function tagged_locator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, string $defaultPriorityMethod = null): ServiceLocatorArgument
|
||||
{
|
||||
return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true, $defaultPriorityMethod));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an expression.
|
||||
*/
|
||||
function expr(string $expression): Expression
|
||||
{
|
||||
return new Expression($expression);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an abstract argument.
|
||||
*/
|
||||
function abstract_arg(string $description): AbstractArgument
|
||||
{
|
||||
return new AbstractArgument($description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an environment variable reference.
|
||||
*/
|
||||
function env(string $name): EnvConfigurator
|
||||
{
|
||||
return new EnvConfigurator($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a closure service reference.
|
||||
*/
|
||||
function service_closure(string $serviceId): ClosureReferenceConfigurator
|
||||
{
|
||||
return new ClosureReferenceConfigurator($serviceId);
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class DefaultsConfigurator extends AbstractServiceConfigurator
|
||||
{
|
||||
use Traits\AutoconfigureTrait;
|
||||
use Traits\AutowireTrait;
|
||||
use Traits\BindTrait;
|
||||
use Traits\PublicTrait;
|
||||
|
||||
public const FACTORY = 'defaults';
|
||||
|
||||
private $path;
|
||||
|
||||
public function __construct(ServicesConfigurator $parent, Definition $definition, string $path = null)
|
||||
{
|
||||
parent::__construct($parent, $definition, null, []);
|
||||
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a tag for this definition.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException when an invalid tag name or attribute is provided
|
||||
*/
|
||||
final public function tag(string $name, array $attributes = []): self
|
||||
{
|
||||
if ('' === $name) {
|
||||
throw new InvalidArgumentException('The tag name in "_defaults" must be a non-empty string.');
|
||||
}
|
||||
|
||||
foreach ($attributes as $attribute => $value) {
|
||||
if (null !== $value && !\is_scalar($value)) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type.', $name, $attribute));
|
||||
}
|
||||
}
|
||||
|
||||
$this->definition->addTag($name, $attributes);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines an instanceof-conditional to be applied to following service definitions.
|
||||
*/
|
||||
final public function instanceof(string $fqcn): InstanceofConfigurator
|
||||
{
|
||||
return $this->parent->instanceof($fqcn);
|
||||
}
|
||||
}
|
|
@ -1,224 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\Config\Loader\ParamConfigurator;
|
||||
|
||||
class EnvConfigurator extends ParamConfigurator
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $stack;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->stack = explode(':', $name);
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return '%env('.implode(':', $this->stack).')%';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function __call(string $name, array $arguments): self
|
||||
{
|
||||
$processor = strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1_\2', $name));
|
||||
|
||||
$this->custom($processor, ...$arguments);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function custom(string $processor, ...$args): self
|
||||
{
|
||||
array_unshift($this->stack, $processor, ...$args);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function base64(): self
|
||||
{
|
||||
array_unshift($this->stack, 'base64');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function bool(): self
|
||||
{
|
||||
array_unshift($this->stack, 'bool');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function not(): self
|
||||
{
|
||||
array_unshift($this->stack, 'not');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function const(): self
|
||||
{
|
||||
array_unshift($this->stack, 'const');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function csv(): self
|
||||
{
|
||||
array_unshift($this->stack, 'csv');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function file(): self
|
||||
{
|
||||
array_unshift($this->stack, 'file');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function float(): self
|
||||
{
|
||||
array_unshift($this->stack, 'float');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function int(): self
|
||||
{
|
||||
array_unshift($this->stack, 'int');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function json(): self
|
||||
{
|
||||
array_unshift($this->stack, 'json');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function key(string $key): self
|
||||
{
|
||||
array_unshift($this->stack, 'key', $key);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function url(): self
|
||||
{
|
||||
array_unshift($this->stack, 'url');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function queryString(): self
|
||||
{
|
||||
array_unshift($this->stack, 'query_string');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function resolve(): self
|
||||
{
|
||||
array_unshift($this->stack, 'resolve');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function default(string $fallbackParam): self
|
||||
{
|
||||
array_unshift($this->stack, 'default', $fallbackParam);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function string(): self
|
||||
{
|
||||
array_unshift($this->stack, 'string');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function trim(): self
|
||||
{
|
||||
array_unshift($this->stack, 'trim');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function require(): self
|
||||
{
|
||||
array_unshift($this->stack, 'require');
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class InlineServiceConfigurator extends AbstractConfigurator
|
||||
{
|
||||
use Traits\ArgumentTrait;
|
||||
use Traits\AutowireTrait;
|
||||
use Traits\BindTrait;
|
||||
use Traits\CallTrait;
|
||||
use Traits\ConfiguratorTrait;
|
||||
use Traits\FactoryTrait;
|
||||
use Traits\FileTrait;
|
||||
use Traits\LazyTrait;
|
||||
use Traits\ParentTrait;
|
||||
use Traits\PropertyTrait;
|
||||
use Traits\TagTrait;
|
||||
|
||||
public const FACTORY = 'service';
|
||||
|
||||
private $id = '[inline]';
|
||||
private $allowParent = true;
|
||||
private $path = null;
|
||||
|
||||
public function __construct(Definition $definition)
|
||||
{
|
||||
$this->definition = $definition;
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class InstanceofConfigurator extends AbstractServiceConfigurator
|
||||
{
|
||||
use Traits\AutowireTrait;
|
||||
use Traits\BindTrait;
|
||||
use Traits\CallTrait;
|
||||
use Traits\ConfiguratorTrait;
|
||||
use Traits\LazyTrait;
|
||||
use Traits\PropertyTrait;
|
||||
use Traits\PublicTrait;
|
||||
use Traits\ShareTrait;
|
||||
use Traits\TagTrait;
|
||||
|
||||
public const FACTORY = 'instanceof';
|
||||
|
||||
private $path;
|
||||
|
||||
public function __construct(ServicesConfigurator $parent, Definition $definition, string $id, string $path = null)
|
||||
{
|
||||
parent::__construct($parent, $definition, $id, []);
|
||||
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines an instanceof-conditional to be applied to following service definitions.
|
||||
*/
|
||||
final public function instanceof(string $fqcn): self
|
||||
{
|
||||
return $this->parent->instanceof($fqcn);
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\ExpressionLanguage\Expression;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class ParametersConfigurator extends AbstractConfigurator
|
||||
{
|
||||
public const FACTORY = 'parameters';
|
||||
|
||||
private $container;
|
||||
|
||||
public function __construct(ContainerBuilder $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parameter.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function set(string $name, $value): self
|
||||
{
|
||||
if ($value instanceof Expression) {
|
||||
throw new InvalidArgumentException(sprintf('Using an expression in parameter "%s" is not allowed.', $name));
|
||||
}
|
||||
|
||||
$this->container->setParameter($name, static::processValue($value, true));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a parameter.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function __invoke(string $name, $value): self
|
||||
{
|
||||
return $this->set($name, $value);
|
||||
}
|
||||
}
|
|
@ -1,87 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class PrototypeConfigurator extends AbstractServiceConfigurator
|
||||
{
|
||||
use Traits\AbstractTrait;
|
||||
use Traits\ArgumentTrait;
|
||||
use Traits\AutoconfigureTrait;
|
||||
use Traits\AutowireTrait;
|
||||
use Traits\BindTrait;
|
||||
use Traits\CallTrait;
|
||||
use Traits\ConfiguratorTrait;
|
||||
use Traits\DeprecateTrait;
|
||||
use Traits\FactoryTrait;
|
||||
use Traits\LazyTrait;
|
||||
use Traits\ParentTrait;
|
||||
use Traits\PropertyTrait;
|
||||
use Traits\PublicTrait;
|
||||
use Traits\ShareTrait;
|
||||
use Traits\TagTrait;
|
||||
|
||||
public const FACTORY = 'load';
|
||||
|
||||
private $loader;
|
||||
private $resource;
|
||||
private $excludes;
|
||||
private $allowParent;
|
||||
|
||||
public function __construct(ServicesConfigurator $parent, PhpFileLoader $loader, Definition $defaults, string $namespace, string $resource, bool $allowParent)
|
||||
{
|
||||
$definition = new Definition();
|
||||
if (!$defaults->isPublic() || !$defaults->isPrivate()) {
|
||||
$definition->setPublic($defaults->isPublic());
|
||||
}
|
||||
$definition->setAutowired($defaults->isAutowired());
|
||||
$definition->setAutoconfigured($defaults->isAutoconfigured());
|
||||
// deep clone, to avoid multiple process of the same instance in the passes
|
||||
$definition->setBindings(unserialize(serialize($defaults->getBindings())));
|
||||
$definition->setChanges([]);
|
||||
|
||||
$this->loader = $loader;
|
||||
$this->resource = $resource;
|
||||
$this->allowParent = $allowParent;
|
||||
|
||||
parent::__construct($parent, $definition, $namespace, $defaults->getTags());
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
parent::__destruct();
|
||||
|
||||
if ($this->loader) {
|
||||
$this->loader->registerClasses($this->definition, $this->id, $this->resource, $this->excludes);
|
||||
}
|
||||
$this->loader = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Excludes files from registration using glob patterns.
|
||||
*
|
||||
* @param string[]|string $excludes
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function exclude($excludes): self
|
||||
{
|
||||
$this->excludes = (array) $excludes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class ReferenceConfigurator extends AbstractConfigurator
|
||||
{
|
||||
/** @internal */
|
||||
protected $id;
|
||||
|
||||
/** @internal */
|
||||
protected $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
||||
|
||||
public function __construct(string $id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
final public function ignoreOnInvalid(): self
|
||||
{
|
||||
$this->invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
final public function nullOnInvalid(): self
|
||||
{
|
||||
$this->invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
final public function ignoreOnUninitialized(): self
|
||||
{
|
||||
$this->invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class ServiceConfigurator extends AbstractServiceConfigurator
|
||||
{
|
||||
use Traits\AbstractTrait;
|
||||
use Traits\ArgumentTrait;
|
||||
use Traits\AutoconfigureTrait;
|
||||
use Traits\AutowireTrait;
|
||||
use Traits\BindTrait;
|
||||
use Traits\CallTrait;
|
||||
use Traits\ClassTrait;
|
||||
use Traits\ConfiguratorTrait;
|
||||
use Traits\DecorateTrait;
|
||||
use Traits\DeprecateTrait;
|
||||
use Traits\FactoryTrait;
|
||||
use Traits\FileTrait;
|
||||
use Traits\LazyTrait;
|
||||
use Traits\ParentTrait;
|
||||
use Traits\PropertyTrait;
|
||||
use Traits\PublicTrait;
|
||||
use Traits\ShareTrait;
|
||||
use Traits\SyntheticTrait;
|
||||
use Traits\TagTrait;
|
||||
|
||||
public const FACTORY = 'services';
|
||||
|
||||
private $container;
|
||||
private $instanceof;
|
||||
private $allowParent;
|
||||
private $path;
|
||||
private $destructed = false;
|
||||
|
||||
public function __construct(ContainerBuilder $container, array $instanceof, bool $allowParent, ServicesConfigurator $parent, Definition $definition, ?string $id, array $defaultTags, string $path = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->instanceof = $instanceof;
|
||||
$this->allowParent = $allowParent;
|
||||
$this->path = $path;
|
||||
|
||||
parent::__construct($parent, $definition, $id, $defaultTags);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->destructed) {
|
||||
return;
|
||||
}
|
||||
$this->destructed = true;
|
||||
|
||||
parent::__destruct();
|
||||
|
||||
$this->container->removeBindings($this->id);
|
||||
$this->container->setDefinition($this->id, $this->definition->setInstanceofConditionals($this->instanceof));
|
||||
}
|
||||
}
|
|
@ -1,192 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
||||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class ServicesConfigurator extends AbstractConfigurator
|
||||
{
|
||||
public const FACTORY = 'services';
|
||||
|
||||
private $defaults;
|
||||
private $container;
|
||||
private $loader;
|
||||
private $instanceof;
|
||||
private $path;
|
||||
private $anonymousHash;
|
||||
private $anonymousCount;
|
||||
|
||||
public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path = null, int &$anonymousCount = 0)
|
||||
{
|
||||
$this->defaults = new Definition();
|
||||
$this->container = $container;
|
||||
$this->loader = $loader;
|
||||
$this->instanceof = &$instanceof;
|
||||
$this->path = $path;
|
||||
$this->anonymousHash = ContainerBuilder::hash($path ?: mt_rand());
|
||||
$this->anonymousCount = &$anonymousCount;
|
||||
$instanceof = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a set of defaults for following service definitions.
|
||||
*/
|
||||
final public function defaults(): DefaultsConfigurator
|
||||
{
|
||||
return new DefaultsConfigurator($this, $this->defaults = new Definition(), $this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines an instanceof-conditional to be applied to following service definitions.
|
||||
*/
|
||||
final public function instanceof(string $fqcn): InstanceofConfigurator
|
||||
{
|
||||
$this->instanceof[$fqcn] = $definition = new ChildDefinition('');
|
||||
|
||||
return new InstanceofConfigurator($this, $definition, $fqcn, $this->path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a service.
|
||||
*
|
||||
* @param string|null $id The service id, or null to create an anonymous service
|
||||
* @param string|null $class The class of the service, or null when $id is also the class name
|
||||
*/
|
||||
final public function set(?string $id, string $class = null): ServiceConfigurator
|
||||
{
|
||||
$defaults = $this->defaults;
|
||||
$definition = new Definition();
|
||||
|
||||
if (null === $id) {
|
||||
if (!$class) {
|
||||
throw new \LogicException('Anonymous services must have a class name.');
|
||||
}
|
||||
|
||||
$id = sprintf('.%d_%s', ++$this->anonymousCount, preg_replace('/^.*\\\\/', '', $class).'~'.$this->anonymousHash);
|
||||
} elseif (!$defaults->isPublic() || !$defaults->isPrivate()) {
|
||||
$definition->setPublic($defaults->isPublic() && !$defaults->isPrivate());
|
||||
}
|
||||
|
||||
$definition->setAutowired($defaults->isAutowired());
|
||||
$definition->setAutoconfigured($defaults->isAutoconfigured());
|
||||
// deep clone, to avoid multiple process of the same instance in the passes
|
||||
$definition->setBindings(unserialize(serialize($defaults->getBindings())));
|
||||
$definition->setChanges([]);
|
||||
|
||||
$configurator = new ServiceConfigurator($this->container, $this->instanceof, true, $this, $definition, $id, $defaults->getTags(), $this->path);
|
||||
|
||||
return null !== $class ? $configurator->class($class) : $configurator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an already defined service definition or alias.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function remove(string $id): self
|
||||
{
|
||||
$this->container->removeDefinition($id);
|
||||
$this->container->removeAlias($id);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an alias.
|
||||
*/
|
||||
final public function alias(string $id, string $referencedId): AliasConfigurator
|
||||
{
|
||||
$ref = static::processValue($referencedId, true);
|
||||
$alias = new Alias((string) $ref);
|
||||
if (!$this->defaults->isPublic() || !$this->defaults->isPrivate()) {
|
||||
$alias->setPublic($this->defaults->isPublic());
|
||||
}
|
||||
$this->container->setAlias($id, $alias);
|
||||
|
||||
return new AliasConfigurator($this, $alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a PSR-4 namespace using a glob pattern.
|
||||
*/
|
||||
final public function load(string $namespace, string $resource): PrototypeConfigurator
|
||||
{
|
||||
return new PrototypeConfigurator($this, $this->loader, $this->defaults, $namespace, $resource, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an already defined service definition.
|
||||
*
|
||||
* @throws ServiceNotFoundException if the service definition does not exist
|
||||
*/
|
||||
final public function get(string $id): ServiceConfigurator
|
||||
{
|
||||
$definition = $this->container->getDefinition($id);
|
||||
|
||||
return new ServiceConfigurator($this->container, $definition->getInstanceofConditionals(), true, $this, $definition, $id, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a stack of decorator services.
|
||||
*
|
||||
* @param InlineServiceConfigurator[]|ReferenceConfigurator[] $services
|
||||
*/
|
||||
final public function stack(string $id, array $services): AliasConfigurator
|
||||
{
|
||||
foreach ($services as $i => $service) {
|
||||
if ($service instanceof InlineServiceConfigurator) {
|
||||
$definition = $service->definition->setInstanceofConditionals($this->instanceof);
|
||||
|
||||
$changes = $definition->getChanges();
|
||||
$definition->setAutowired((isset($changes['autowired']) ? $definition : $this->defaults)->isAutowired());
|
||||
$definition->setAutoconfigured((isset($changes['autoconfigured']) ? $definition : $this->defaults)->isAutoconfigured());
|
||||
$definition->setBindings(array_merge($this->defaults->getBindings(), $definition->getBindings()));
|
||||
$definition->setChanges($changes);
|
||||
|
||||
$services[$i] = $definition;
|
||||
} elseif (!$service instanceof ReferenceConfigurator) {
|
||||
throw new InvalidArgumentException(sprintf('"%s()" expects a list of definitions as returned by "%s()" or "%s()", "%s" given at index "%s" for service "%s".', __METHOD__, InlineServiceConfigurator::FACTORY, ReferenceConfigurator::FACTORY, $service instanceof AbstractConfigurator ? $service::FACTORY.'()' : get_debug_type($service), $i, $id));
|
||||
}
|
||||
}
|
||||
|
||||
$alias = $this->alias($id, '');
|
||||
$alias->definition = $this->set($id)
|
||||
->parent('')
|
||||
->args($services)
|
||||
->tag('container.stack')
|
||||
->definition;
|
||||
|
||||
return $alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a service.
|
||||
*/
|
||||
final public function __invoke(string $id, string $class = null): ServiceConfigurator
|
||||
{
|
||||
return $this->set($id, $class);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->loader->registerAliasesForSinglyImplementedInterfaces();
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait AbstractTrait
|
||||
{
|
||||
/**
|
||||
* Whether this definition is abstract, that means it merely serves as a
|
||||
* template for other definitions.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function abstract(bool $abstract = true): self
|
||||
{
|
||||
$this->definition->setAbstract($abstract);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait ArgumentTrait
|
||||
{
|
||||
/**
|
||||
* Sets the arguments to pass to the service constructor/factory method.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function args(array $arguments): self
|
||||
{
|
||||
$this->definition->setArguments(static::processValue($arguments, true));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets one argument to pass to the service constructor/factory method.
|
||||
*
|
||||
* @param string|int $key
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function arg($key, $value): self
|
||||
{
|
||||
$this->definition->setArgument($key, static::processValue($value, true));
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
trait AutoconfigureTrait
|
||||
{
|
||||
/**
|
||||
* Sets whether or not instanceof conditionals should be prepended with a global set.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException when a parent is already set
|
||||
*/
|
||||
final public function autoconfigure(bool $autoconfigured = true): self
|
||||
{
|
||||
$this->definition->setAutoconfigured($autoconfigured);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait AutowireTrait
|
||||
{
|
||||
/**
|
||||
* Enables/disables autowiring.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function autowire(bool $autowired = true): self
|
||||
{
|
||||
$this->definition->setAutowired($autowired);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\DefaultsConfigurator;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\InstanceofConfigurator;
|
||||
|
||||
trait BindTrait
|
||||
{
|
||||
/**
|
||||
* Sets bindings.
|
||||
*
|
||||
* Bindings map $named or FQCN arguments to values that should be
|
||||
* injected in the matching parameters (of the constructor, of methods
|
||||
* called and of controller actions).
|
||||
*
|
||||
* @param string $nameOrFqcn A parameter name with its "$" prefix, or an FQCN
|
||||
* @param mixed $valueOrRef The value or reference to bind
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function bind(string $nameOrFqcn, $valueOrRef): self
|
||||
{
|
||||
$valueOrRef = static::processValue($valueOrRef, true);
|
||||
$bindings = $this->definition->getBindings();
|
||||
$type = $this instanceof DefaultsConfigurator ? BoundArgument::DEFAULTS_BINDING : ($this instanceof InstanceofConfigurator ? BoundArgument::INSTANCEOF_BINDING : BoundArgument::SERVICE_BINDING);
|
||||
$bindings[$nameOrFqcn] = new BoundArgument($valueOrRef, true, $type, $this->path ?? null);
|
||||
$this->definition->setBindings($bindings);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
trait CallTrait
|
||||
{
|
||||
/**
|
||||
* Adds a method to call after service initialization.
|
||||
*
|
||||
* @param string $method The method name to call
|
||||
* @param array $arguments An array of arguments to pass to the method call
|
||||
* @param bool $returnsClone Whether the call returns the service instance or not
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException on empty $method param
|
||||
*/
|
||||
final public function call(string $method, array $arguments = [], bool $returnsClone = false): self
|
||||
{
|
||||
$this->definition->addMethodCall($method, static::processValue($arguments, true), $returnsClone);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait ClassTrait
|
||||
{
|
||||
/**
|
||||
* Sets the service class.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function class(?string $class): self
|
||||
{
|
||||
$this->definition->setClass($class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait ConfiguratorTrait
|
||||
{
|
||||
/**
|
||||
* Sets a configurator to call after the service is fully initialized.
|
||||
*
|
||||
* @param string|array $configurator A PHP callable reference
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function configurator($configurator): self
|
||||
{
|
||||
$this->definition->setConfigurator(static::processValue($configurator, true));
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
trait DecorateTrait
|
||||
{
|
||||
/**
|
||||
* Sets the service that this service is decorating.
|
||||
*
|
||||
* @param string|null $id The decorated service id, use null to remove decoration
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
|
||||
*/
|
||||
final public function decorate(?string $id, string $renamedId = null, int $priority = 0, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE): self
|
||||
{
|
||||
$this->definition->setDecoratedService($id, $renamedId, $priority, $invalidBehavior);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
trait DeprecateTrait
|
||||
{
|
||||
/**
|
||||
* Whether this definition is deprecated, that means it should not be called anymore.
|
||||
*
|
||||
* @param string $package The name of the composer package that is triggering the deprecation
|
||||
* @param string $version The version of the package that introduced the deprecation
|
||||
* @param string $message The deprecation message to use
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException when the message template is invalid
|
||||
*/
|
||||
final public function deprecate(/* string $package, string $version, string $message */): self
|
||||
{
|
||||
$args = \func_get_args();
|
||||
$package = $version = $message = '';
|
||||
|
||||
if (\func_num_args() < 3) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'The signature of method "%s()" requires 3 arguments: "string $package, string $version, string $message", not defining them is deprecated.', __METHOD__);
|
||||
|
||||
$message = (string) ($args[0] ?? null);
|
||||
} else {
|
||||
$package = (string) $args[0];
|
||||
$version = (string) $args[1];
|
||||
$message = (string) $args[2];
|
||||
}
|
||||
|
||||
$this->definition->setDeprecated($package, $version, $message);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
|
||||
|
||||
trait FactoryTrait
|
||||
{
|
||||
/**
|
||||
* Sets a factory.
|
||||
*
|
||||
* @param string|array|ReferenceConfigurator $factory A PHP callable reference
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function factory($factory): self
|
||||
{
|
||||
if (\is_string($factory) && 1 === substr_count($factory, ':')) {
|
||||
$factoryParts = explode(':', $factory);
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Invalid factory "%s": the "service:method" notation is not available when using PHP-based DI configuration. Use "[service(\'%s\'), \'%s\']" instead.', $factory, $factoryParts[0], $factoryParts[1]));
|
||||
}
|
||||
|
||||
$this->definition->setFactory(static::processValue($factory, true));
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait FileTrait
|
||||
{
|
||||
/**
|
||||
* Sets a file to require before creating the service.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function file(string $file): self
|
||||
{
|
||||
$this->definition->setFile($file);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait LazyTrait
|
||||
{
|
||||
/**
|
||||
* Sets the lazy flag of this service.
|
||||
*
|
||||
* @param bool|string $lazy A FQCN to derivate the lazy proxy from or `true` to make it extend from the definition's class
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function lazy($lazy = true): self
|
||||
{
|
||||
$this->definition->setLazy((bool) $lazy);
|
||||
if (\is_string($lazy)) {
|
||||
$this->definition->addTag('proxy', ['interface' => $lazy]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
trait ParentTrait
|
||||
{
|
||||
/**
|
||||
* Sets the Definition to inherit from.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws InvalidArgumentException when parent cannot be set
|
||||
*/
|
||||
final public function parent(string $parent): self
|
||||
{
|
||||
if (!$this->allowParent) {
|
||||
throw new InvalidArgumentException(sprintf('A parent cannot be defined when either "_instanceof" or "_defaults" are also defined for service prototype "%s".', $this->id));
|
||||
}
|
||||
|
||||
if ($this->definition instanceof ChildDefinition) {
|
||||
$this->definition->setParent($parent);
|
||||
} else {
|
||||
// cast Definition to ChildDefinition
|
||||
$definition = serialize($this->definition);
|
||||
$definition = substr_replace($definition, '53', 2, 2);
|
||||
$definition = substr_replace($definition, 'Child', 44, 0);
|
||||
$definition = unserialize($definition);
|
||||
|
||||
$this->definition = $definition->setParent($parent);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait PropertyTrait
|
||||
{
|
||||
/**
|
||||
* Sets a specific property.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function property(string $name, $value): self
|
||||
{
|
||||
$this->definition->setProperty($name, static::processValue($value, true));
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait PublicTrait
|
||||
{
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
final public function public(): self
|
||||
{
|
||||
$this->definition->setPublic(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
final public function private(): self
|
||||
{
|
||||
$this->definition->setPublic(false);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait ShareTrait
|
||||
{
|
||||
/**
|
||||
* Sets if the service must be shared or not.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function share(bool $shared = true): self
|
||||
{
|
||||
$this->definition->setShared($shared);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
trait SyntheticTrait
|
||||
{
|
||||
/**
|
||||
* Sets whether this definition is synthetic, that is not constructed by the
|
||||
* container, but dynamically injected.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function synthetic(bool $synthetic = true): self
|
||||
{
|
||||
$this->definition->setSynthetic($synthetic);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
trait TagTrait
|
||||
{
|
||||
/**
|
||||
* Adds a tag for this definition.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
final public function tag(string $name, array $attributes = []): self
|
||||
{
|
||||
if ('' === $name) {
|
||||
throw new InvalidArgumentException(sprintf('The tag name for service "%s" must be a non-empty string.', $this->id));
|
||||
}
|
||||
|
||||
foreach ($attributes as $attribute => $value) {
|
||||
if (!\is_scalar($value) && null !== $value) {
|
||||
throw new InvalidArgumentException(sprintf('A tag attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s".', $this->id, $name, $attribute));
|
||||
}
|
||||
}
|
||||
|
||||
$this->definition->addTag($name, $attributes);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
/**
|
||||
* DirectoryLoader is a recursive loader to go through directories.
|
||||
*
|
||||
* @author Sebastien Lavoie <seb@wemakecustom.com>
|
||||
*/
|
||||
class DirectoryLoader extends FileLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($file, string $type = null)
|
||||
{
|
||||
$file = rtrim($file, '/');
|
||||
$path = $this->locator->locate($file);
|
||||
$this->container->fileExists($path, false);
|
||||
|
||||
foreach (scandir($path) as $dir) {
|
||||
if ('.' !== $dir[0]) {
|
||||
if (is_dir($path.'/'.$dir)) {
|
||||
$dir .= '/'; // append / to allow recursion
|
||||
}
|
||||
|
||||
$this->setCurrentDir($path);
|
||||
|
||||
$this->import($dir, null, false, $path);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
if ('directory' === $type) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return null === $type && \is_string($resource) && str_ends_with($resource, '/');
|
||||
}
|
||||
}
|
|
@ -1,247 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
|
||||
use Symfony\Component\Config\Exception\LoaderLoadException;
|
||||
use Symfony\Component\Config\FileLocatorInterface;
|
||||
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
|
||||
use Symfony\Component\Config\Loader\Loader;
|
||||
use Symfony\Component\Config\Resource\GlobResource;
|
||||
use Symfony\Component\DependencyInjection\Attribute\When;
|
||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||
use Symfony\Component\DependencyInjection\Compiler\RegisterAutoconfigureAttributesPass;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* FileLoader is the abstract class used by all built-in loaders that are file based.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
abstract class FileLoader extends BaseFileLoader
|
||||
{
|
||||
public const ANONYMOUS_ID_REGEXP = '/^\.\d+_[^~]*+~[._a-zA-Z\d]{7}$/';
|
||||
|
||||
protected $container;
|
||||
protected $isLoadingInstanceof = false;
|
||||
protected $instanceof = [];
|
||||
protected $interfaces = [];
|
||||
protected $singlyImplemented = [];
|
||||
protected $autoRegisterAliasesForSinglyImplementedInterfaces = true;
|
||||
|
||||
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
|
||||
parent::__construct($locator, $env);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param bool|string $ignoreErrors Whether errors should be ignored; pass "not_found" to ignore only when the loaded resource is not found
|
||||
*/
|
||||
public function import($resource, string $type = null, $ignoreErrors = false, string $sourceResource = null, $exclude = null)
|
||||
{
|
||||
$args = \func_get_args();
|
||||
|
||||
if ($ignoreNotFound = 'not_found' === $ignoreErrors) {
|
||||
$args[2] = false;
|
||||
} elseif (!\is_bool($ignoreErrors)) {
|
||||
throw new \TypeError(sprintf('Invalid argument $ignoreErrors provided to "%s::import()": boolean or "not_found" expected, "%s" given.', static::class, get_debug_type($ignoreErrors)));
|
||||
}
|
||||
|
||||
try {
|
||||
return parent::import(...$args);
|
||||
} catch (LoaderLoadException $e) {
|
||||
if (!$ignoreNotFound || !($prev = $e->getPrevious()) instanceof FileLocatorFileNotFoundException) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
foreach ($prev->getTrace() as $frame) {
|
||||
if ('import' === ($frame['function'] ?? null) && is_a($frame['class'] ?? '', Loader::class, true)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (__FILE__ !== $frame['file']) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of classes as services using PSR-4 for discovery.
|
||||
*
|
||||
* @param Definition $prototype A definition to use as template
|
||||
* @param string $namespace The namespace prefix of classes in the scanned directory
|
||||
* @param string $resource The directory to look for classes, glob-patterns allowed
|
||||
* @param string|string[]|null $exclude A globbed path of files to exclude or an array of globbed paths of files to exclude
|
||||
*/
|
||||
public function registerClasses(Definition $prototype, string $namespace, string $resource, $exclude = null)
|
||||
{
|
||||
if (!str_ends_with($namespace, '\\')) {
|
||||
throw new InvalidArgumentException(sprintf('Namespace prefix must end with a "\\": "%s".', $namespace));
|
||||
}
|
||||
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+\\\\)++$/', $namespace)) {
|
||||
throw new InvalidArgumentException(sprintf('Namespace is not a valid PSR-4 prefix: "%s".', $namespace));
|
||||
}
|
||||
|
||||
$autoconfigureAttributes = new RegisterAutoconfigureAttributesPass();
|
||||
$autoconfigureAttributes = $autoconfigureAttributes->accept($prototype) ? $autoconfigureAttributes : null;
|
||||
$classes = $this->findClasses($namespace, $resource, (array) $exclude, $autoconfigureAttributes);
|
||||
// prepare for deep cloning
|
||||
$serializedPrototype = serialize($prototype);
|
||||
|
||||
foreach ($classes as $class => $errorMessage) {
|
||||
if (null === $errorMessage && $autoconfigureAttributes && $this->env) {
|
||||
$r = $this->container->getReflectionClass($class);
|
||||
$attribute = null;
|
||||
foreach ($r->getAttributes(When::class) as $attribute) {
|
||||
if ($this->env === $attribute->newInstance()->env) {
|
||||
$attribute = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (null !== $attribute) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (interface_exists($class, false)) {
|
||||
$this->interfaces[] = $class;
|
||||
} else {
|
||||
$this->setDefinition($class, $definition = unserialize($serializedPrototype));
|
||||
if (null !== $errorMessage) {
|
||||
$definition->addError($errorMessage);
|
||||
|
||||
continue;
|
||||
}
|
||||
foreach (class_implements($class, false) as $interface) {
|
||||
$this->singlyImplemented[$interface] = ($this->singlyImplemented[$interface] ?? $class) !== $class ? false : $class;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->autoRegisterAliasesForSinglyImplementedInterfaces) {
|
||||
$this->registerAliasesForSinglyImplementedInterfaces();
|
||||
}
|
||||
}
|
||||
|
||||
public function registerAliasesForSinglyImplementedInterfaces()
|
||||
{
|
||||
foreach ($this->interfaces as $interface) {
|
||||
if (!empty($this->singlyImplemented[$interface]) && !$this->container->has($interface)) {
|
||||
$this->container->setAlias($interface, $this->singlyImplemented[$interface]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->interfaces = $this->singlyImplemented = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a definition in the container with its instanceof-conditionals.
|
||||
*/
|
||||
protected function setDefinition(string $id, Definition $definition)
|
||||
{
|
||||
$this->container->removeBindings($id);
|
||||
|
||||
if ($this->isLoadingInstanceof) {
|
||||
if (!$definition instanceof ChildDefinition) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, get_debug_type($definition)));
|
||||
}
|
||||
$this->instanceof[$id] = $definition;
|
||||
} else {
|
||||
$this->container->setDefinition($id, $definition->setInstanceofConditionals($this->instanceof));
|
||||
}
|
||||
}
|
||||
|
||||
private function findClasses(string $namespace, string $pattern, array $excludePatterns, ?RegisterAutoconfigureAttributesPass $autoconfigureAttributes): array
|
||||
{
|
||||
$parameterBag = $this->container->getParameterBag();
|
||||
|
||||
$excludePaths = [];
|
||||
$excludePrefix = null;
|
||||
$excludePatterns = $parameterBag->unescapeValue($parameterBag->resolveValue($excludePatterns));
|
||||
foreach ($excludePatterns as $excludePattern) {
|
||||
foreach ($this->glob($excludePattern, true, $resource, true, true) as $path => $info) {
|
||||
if (null === $excludePrefix) {
|
||||
$excludePrefix = $resource->getPrefix();
|
||||
}
|
||||
|
||||
// normalize Windows slashes and remove trailing slashes
|
||||
$excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true;
|
||||
}
|
||||
}
|
||||
|
||||
$pattern = $parameterBag->unescapeValue($parameterBag->resolveValue($pattern));
|
||||
$classes = [];
|
||||
$extRegexp = '/\\.php$/';
|
||||
$prefixLen = null;
|
||||
foreach ($this->glob($pattern, true, $resource, false, false, $excludePaths) as $path => $info) {
|
||||
if (null === $prefixLen) {
|
||||
$prefixLen = \strlen($resource->getPrefix());
|
||||
|
||||
if ($excludePrefix && !str_starts_with($excludePrefix, $resource->getPrefix())) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid "exclude" pattern when importing classes for "%s": make sure your "exclude" pattern (%s) is a subset of the "resource" pattern (%s).', $namespace, $excludePattern, $pattern));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($excludePaths[str_replace('\\', '/', $path)])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!preg_match($extRegexp, $path, $m) || !$info->isReadable()) {
|
||||
continue;
|
||||
}
|
||||
$class = $namespace.ltrim(str_replace('/', '\\', substr($path, $prefixLen, -\strlen($m[0]))), '\\');
|
||||
|
||||
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$r = $this->container->getReflectionClass($class);
|
||||
} catch (\ReflectionException $e) {
|
||||
$classes[$class] = $e->getMessage();
|
||||
continue;
|
||||
}
|
||||
// check to make sure the expected class exists
|
||||
if (!$r) {
|
||||
throw new InvalidArgumentException(sprintf('Expected to find class "%s" in file "%s" while importing services from resource "%s", but it was not found! Check the namespace prefix used with the resource.', $class, $path, $pattern));
|
||||
}
|
||||
|
||||
if ($r->isInstantiable() || $r->isInterface()) {
|
||||
$classes[$class] = null;
|
||||
}
|
||||
|
||||
if ($autoconfigureAttributes && !$r->isInstantiable()) {
|
||||
$autoconfigureAttributes->processClass($this->container, $r);
|
||||
}
|
||||
}
|
||||
|
||||
// track only for new & removed files
|
||||
if ($resource instanceof GlobResource) {
|
||||
$this->container->addResource($resource);
|
||||
} else {
|
||||
foreach ($resource as $path) {
|
||||
$this->container->fileExists($path, false);
|
||||
}
|
||||
}
|
||||
|
||||
return $classes;
|
||||
}
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
/**
|
||||
* GlobFileLoader loads files from a glob pattern.
|
||||
*
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class GlobFileLoader extends FileLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($resource, string $type = null)
|
||||
{
|
||||
foreach ($this->glob($resource, false, $globResource) as $path => $info) {
|
||||
$this->import($path);
|
||||
}
|
||||
|
||||
$this->container->addResource($globResource);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
return 'glob' === $type;
|
||||
}
|
||||
}
|
|
@ -1,105 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
use Symfony\Component\Config\Util\XmlUtils;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* IniFileLoader loads parameters from INI files.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class IniFileLoader extends FileLoader
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($resource, string $type = null)
|
||||
{
|
||||
$path = $this->locator->locate($resource);
|
||||
|
||||
$this->container->fileExists($path);
|
||||
|
||||
// first pass to catch parsing errors
|
||||
$result = parse_ini_file($path, true);
|
||||
if (false === $result || [] === $result) {
|
||||
throw new InvalidArgumentException(sprintf('The "%s" file is not valid.', $resource));
|
||||
}
|
||||
|
||||
// real raw parsing
|
||||
$result = parse_ini_file($path, true, \INI_SCANNER_RAW);
|
||||
|
||||
if (isset($result['parameters']) && \is_array($result['parameters'])) {
|
||||
foreach ($result['parameters'] as $key => $value) {
|
||||
$this->container->setParameter($key, $this->phpize($value));
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->env && \is_array($result['parameters@'.$this->env] ?? null)) {
|
||||
foreach ($result['parameters@'.$this->env] as $key => $value) {
|
||||
$this->container->setParameter($key, $this->phpize($value));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
if (!\is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && 'ini' === pathinfo($resource, \PATHINFO_EXTENSION)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 'ini' === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note that the following features are not supported:
|
||||
* * strings with escaped quotes are not supported "foo\"bar";
|
||||
* * string concatenation ("foo" "bar").
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function phpize(string $value)
|
||||
{
|
||||
// trim on the right as comments removal keep whitespaces
|
||||
if ($value !== $v = rtrim($value)) {
|
||||
$value = '""' === substr_replace($v, '', 1, -1) ? substr($v, 1, -1) : $v;
|
||||
}
|
||||
$lowercaseValue = strtolower($value);
|
||||
|
||||
switch (true) {
|
||||
case \defined($value):
|
||||
return \constant($value);
|
||||
case 'yes' === $lowercaseValue || 'on' === $lowercaseValue:
|
||||
return true;
|
||||
case 'no' === $lowercaseValue || 'off' === $lowercaseValue || 'none' === $lowercaseValue:
|
||||
return false;
|
||||
case isset($value[1]) && (
|
||||
("'" === $value[0] && "'" === $value[\strlen($value) - 1]) ||
|
||||
('"' === $value[0] && '"' === $value[\strlen($value) - 1])
|
||||
):
|
||||
// quoted string
|
||||
return substr($value, 1, -1);
|
||||
default:
|
||||
return XmlUtils::phpize($value);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,211 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
|
||||
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
|
||||
use Symfony\Component\Config\Builder\ConfigBuilderInterface;
|
||||
use Symfony\Component\Config\FileLocatorInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\When;
|
||||
use Symfony\Component\DependencyInjection\Container;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
|
||||
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
/**
|
||||
* PhpFileLoader loads service definitions from a PHP file.
|
||||
*
|
||||
* The PHP file is required and the $container variable can be
|
||||
* used within the file to change the container.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class PhpFileLoader extends FileLoader
|
||||
{
|
||||
protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
|
||||
private $generator;
|
||||
|
||||
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null, ConfigBuilderGeneratorInterface $generator = null)
|
||||
{
|
||||
parent::__construct($container, $locator, $env);
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($resource, string $type = null)
|
||||
{
|
||||
// the container and loader variables are exposed to the included file below
|
||||
$container = $this->container;
|
||||
$loader = $this;
|
||||
|
||||
$path = $this->locator->locate($resource);
|
||||
$this->setCurrentDir(\dirname($path));
|
||||
$this->container->fileExists($path);
|
||||
|
||||
// the closure forbids access to the private scope in the included file
|
||||
$load = \Closure::bind(function ($path, $env) use ($container, $loader, $resource, $type) {
|
||||
return include $path;
|
||||
}, $this, ProtectedPhpFileLoader::class);
|
||||
|
||||
try {
|
||||
$callback = $load($path, $this->env);
|
||||
|
||||
if (\is_object($callback) && \is_callable($callback)) {
|
||||
$this->executeCallback($callback, new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource, $this->env), $path);
|
||||
}
|
||||
} finally {
|
||||
$this->instanceof = [];
|
||||
$this->registerAliasesForSinglyImplementedInterfaces();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
if (!\is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && 'php' === pathinfo($resource, \PATHINFO_EXTENSION)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 'php' === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the parameters to the $callback and execute it.
|
||||
*/
|
||||
private function executeCallback(callable $callback, ContainerConfigurator $containerConfigurator, string $path)
|
||||
{
|
||||
if (!$callback instanceof \Closure) {
|
||||
$callback = \Closure::fromCallable($callback);
|
||||
}
|
||||
|
||||
$arguments = [];
|
||||
$configBuilders = [];
|
||||
$r = new \ReflectionFunction($callback);
|
||||
|
||||
if (\PHP_VERSION_ID >= 80000) {
|
||||
$attribute = null;
|
||||
foreach ($r->getAttributes(When::class) as $attribute) {
|
||||
if ($this->env === $attribute->newInstance()->env) {
|
||||
$attribute = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (null !== $attribute) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($r->getParameters() as $parameter) {
|
||||
$reflectionType = $parameter->getType();
|
||||
if (!$reflectionType instanceof \ReflectionNamedType) {
|
||||
throw new \InvalidArgumentException(sprintf('Could not resolve argument "$%s" for "%s". You must typehint it (for example with "%s" or "%s").', $parameter->getName(), $path, ContainerConfigurator::class, ContainerBuilder::class));
|
||||
}
|
||||
$type = $reflectionType->getName();
|
||||
|
||||
switch ($type) {
|
||||
case ContainerConfigurator::class:
|
||||
$arguments[] = $containerConfigurator;
|
||||
break;
|
||||
case ContainerBuilder::class:
|
||||
$arguments[] = $this->container;
|
||||
break;
|
||||
case FileLoader::class:
|
||||
case self::class:
|
||||
$arguments[] = $this;
|
||||
break;
|
||||
default:
|
||||
try {
|
||||
$configBuilder = $this->configBuilder($type);
|
||||
} catch (InvalidArgumentException|\LogicException $e) {
|
||||
throw new \InvalidArgumentException(sprintf('Could not resolve argument "%s" for "%s".', $type.' $'.$parameter->getName(), $path), 0, $e);
|
||||
}
|
||||
$configBuilders[] = $configBuilder;
|
||||
$arguments[] = $configBuilder;
|
||||
}
|
||||
}
|
||||
|
||||
// Force load ContainerConfigurator to make env(), param() etc available.
|
||||
class_exists(ContainerConfigurator::class);
|
||||
|
||||
$callback(...$arguments);
|
||||
|
||||
/** @var ConfigBuilderInterface $configBuilder */
|
||||
foreach ($configBuilders as $configBuilder) {
|
||||
$containerConfigurator->extension($configBuilder->getExtensionAlias(), $configBuilder->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $namespace FQCN string for a class implementing ConfigBuilderInterface
|
||||
*/
|
||||
private function configBuilder(string $namespace): ConfigBuilderInterface
|
||||
{
|
||||
if (!class_exists(ConfigBuilderGenerator::class)) {
|
||||
throw new \LogicException('You cannot use the config builder as the Config component is not installed. Try running "composer require symfony/config".');
|
||||
}
|
||||
|
||||
if (null === $this->generator) {
|
||||
throw new \LogicException('You cannot use the ConfigBuilders without providing a class implementing ConfigBuilderGeneratorInterface.');
|
||||
}
|
||||
|
||||
// If class exists and implements ConfigBuilderInterface
|
||||
if (class_exists($namespace) && is_subclass_of($namespace, ConfigBuilderInterface::class)) {
|
||||
return new $namespace();
|
||||
}
|
||||
|
||||
// If it does not start with Symfony\Config\ we dont know how to handle this
|
||||
if ('Symfony\\Config\\' !== substr($namespace, 0, 15)) {
|
||||
throw new InvalidArgumentException(sprintf('Could not find or generate class "%s".', $namespace));
|
||||
}
|
||||
|
||||
// Try to get the extension alias
|
||||
$alias = Container::underscore(substr($namespace, 15, -6));
|
||||
|
||||
if (false !== strpos($alias, '\\')) {
|
||||
throw new InvalidArgumentException('You can only use "root" ConfigBuilders from "Symfony\\Config\\" namespace. Nested classes like "Symfony\\Config\\Framework\\CacheConfig" cannot be used.');
|
||||
}
|
||||
|
||||
if (!$this->container->hasExtension($alias)) {
|
||||
$extensions = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
|
||||
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s". Looked for namespace "%s", found "%s".', $namespace, $alias, $extensions ? implode('", "', $extensions) : 'none'));
|
||||
}
|
||||
|
||||
$extension = $this->container->getExtension($alias);
|
||||
if (!$extension instanceof ConfigurationExtensionInterface) {
|
||||
throw new \LogicException(sprintf('You cannot use the config builder for "%s" because the extension does not implement "%s".', $namespace, ConfigurationExtensionInterface::class));
|
||||
}
|
||||
|
||||
$configuration = $extension->getConfiguration([], $this->container);
|
||||
$loader = $this->generator->build($configuration);
|
||||
|
||||
return $loader();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class ProtectedPhpFileLoader extends PhpFileLoader
|
||||
{
|
||||
}
|
|
@ -1,782 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
use Symfony\Component\Config\Util\XmlUtils;
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
|
||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\ExpressionLanguage\Expression;
|
||||
|
||||
/**
|
||||
* XmlFileLoader loads XML files service definitions.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class XmlFileLoader extends FileLoader
|
||||
{
|
||||
public const NS = 'http://symfony.com/schema/dic/services';
|
||||
|
||||
protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($resource, string $type = null)
|
||||
{
|
||||
$path = $this->locator->locate($resource);
|
||||
|
||||
$xml = $this->parseFileToDOM($path);
|
||||
|
||||
$this->container->fileExists($path);
|
||||
|
||||
$this->loadXml($xml, $path);
|
||||
|
||||
if ($this->env) {
|
||||
$xpath = new \DOMXPath($xml);
|
||||
$xpath->registerNamespace('container', self::NS);
|
||||
foreach ($xpath->query(sprintf('//container:when[@env="%s"]', $this->env)) ?: [] as $root) {
|
||||
$env = $this->env;
|
||||
$this->env = null;
|
||||
try {
|
||||
$this->loadXml($xml, $path, $root);
|
||||
} finally {
|
||||
$this->env = $env;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function loadXml(\DOMDocument $xml, string $path, \DOMNode $root = null): void
|
||||
{
|
||||
$defaults = $this->getServiceDefaults($xml, $path, $root);
|
||||
|
||||
// anonymous services
|
||||
$this->processAnonymousServices($xml, $path, $root);
|
||||
|
||||
// imports
|
||||
$this->parseImports($xml, $path, $root);
|
||||
|
||||
// parameters
|
||||
$this->parseParameters($xml, $path, $root);
|
||||
|
||||
// extensions
|
||||
$this->loadFromExtensions($xml, $root);
|
||||
|
||||
// services
|
||||
try {
|
||||
$this->parseDefinitions($xml, $path, $defaults, $root);
|
||||
} finally {
|
||||
$this->instanceof = [];
|
||||
$this->registerAliasesForSinglyImplementedInterfaces();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
if (!\is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && 'xml' === pathinfo($resource, \PATHINFO_EXTENSION)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 'xml' === $type;
|
||||
}
|
||||
|
||||
private function parseParameters(\DOMDocument $xml, string $file, \DOMNode $root = null)
|
||||
{
|
||||
if ($parameters = $this->getChildren($root ?? $xml->documentElement, 'parameters')) {
|
||||
$this->container->getParameterBag()->add($this->getArgumentsAsPhp($parameters[0], 'parameter', $file));
|
||||
}
|
||||
}
|
||||
|
||||
private function parseImports(\DOMDocument $xml, string $file, \DOMNode $root = null)
|
||||
{
|
||||
$xpath = new \DOMXPath($xml);
|
||||
$xpath->registerNamespace('container', self::NS);
|
||||
|
||||
if (false === $imports = $xpath->query('.//container:imports/container:import', $root)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$defaultDirectory = \dirname($file);
|
||||
foreach ($imports as $import) {
|
||||
$this->setCurrentDir($defaultDirectory);
|
||||
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, XmlUtils::phpize($import->getAttribute('ignore-errors')) ?: false, $file);
|
||||
}
|
||||
}
|
||||
|
||||
private function parseDefinitions(\DOMDocument $xml, string $file, Definition $defaults, \DOMNode $root = null)
|
||||
{
|
||||
$xpath = new \DOMXPath($xml);
|
||||
$xpath->registerNamespace('container', self::NS);
|
||||
|
||||
if (false === $services = $xpath->query('.//container:services/container:service|.//container:services/container:prototype|.//container:services/container:stack', $root)) {
|
||||
return;
|
||||
}
|
||||
$this->setCurrentDir(\dirname($file));
|
||||
|
||||
$this->instanceof = [];
|
||||
$this->isLoadingInstanceof = true;
|
||||
$instanceof = $xpath->query('.//container:services/container:instanceof', $root);
|
||||
foreach ($instanceof as $service) {
|
||||
$this->setDefinition((string) $service->getAttribute('id'), $this->parseDefinition($service, $file, new Definition()));
|
||||
}
|
||||
|
||||
$this->isLoadingInstanceof = false;
|
||||
foreach ($services as $service) {
|
||||
if ('stack' === $service->tagName) {
|
||||
$service->setAttribute('parent', '-');
|
||||
$definition = $this->parseDefinition($service, $file, $defaults)
|
||||
->setTags(array_merge_recursive(['container.stack' => [[]]], $defaults->getTags()))
|
||||
;
|
||||
$this->setDefinition($id = (string) $service->getAttribute('id'), $definition);
|
||||
$stack = [];
|
||||
|
||||
foreach ($this->getChildren($service, 'service') as $k => $frame) {
|
||||
$k = $frame->getAttribute('id') ?: $k;
|
||||
$frame->setAttribute('id', $id.'" at index "'.$k);
|
||||
|
||||
if ($alias = $frame->getAttribute('alias')) {
|
||||
$this->validateAlias($frame, $file);
|
||||
$stack[$k] = new Reference($alias);
|
||||
} else {
|
||||
$stack[$k] = $this->parseDefinition($frame, $file, $defaults)
|
||||
->setInstanceofConditionals($this->instanceof);
|
||||
}
|
||||
}
|
||||
|
||||
$definition->setArguments($stack);
|
||||
} elseif (null !== $definition = $this->parseDefinition($service, $file, $defaults)) {
|
||||
if ('prototype' === $service->tagName) {
|
||||
$excludes = array_column($this->getChildren($service, 'exclude'), 'nodeValue');
|
||||
if ($service->hasAttribute('exclude')) {
|
||||
if (\count($excludes) > 0) {
|
||||
throw new InvalidArgumentException('You cannot use both the attribute "exclude" and <exclude> tags at the same time.');
|
||||
}
|
||||
$excludes = [$service->getAttribute('exclude')];
|
||||
}
|
||||
$this->registerClasses($definition, (string) $service->getAttribute('namespace'), (string) $service->getAttribute('resource'), $excludes);
|
||||
} else {
|
||||
$this->setDefinition((string) $service->getAttribute('id'), $definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getServiceDefaults(\DOMDocument $xml, string $file, \DOMNode $root = null): Definition
|
||||
{
|
||||
$xpath = new \DOMXPath($xml);
|
||||
$xpath->registerNamespace('container', self::NS);
|
||||
|
||||
if (null === $defaultsNode = $xpath->query('.//container:services/container:defaults', $root)->item(0)) {
|
||||
return new Definition();
|
||||
}
|
||||
|
||||
$defaultsNode->setAttribute('id', '<defaults>');
|
||||
|
||||
return $this->parseDefinition($defaultsNode, $file, new Definition());
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an individual Definition.
|
||||
*/
|
||||
private function parseDefinition(\DOMElement $service, string $file, Definition $defaults): ?Definition
|
||||
{
|
||||
if ($alias = $service->getAttribute('alias')) {
|
||||
$this->validateAlias($service, $file);
|
||||
|
||||
$this->container->setAlias($service->getAttribute('id'), $alias = new Alias($alias));
|
||||
if ($publicAttr = $service->getAttribute('public')) {
|
||||
$alias->setPublic(XmlUtils::phpize($publicAttr));
|
||||
} elseif ($defaults->getChanges()['public'] ?? false) {
|
||||
$alias->setPublic($defaults->isPublic());
|
||||
}
|
||||
|
||||
if ($deprecated = $this->getChildren($service, 'deprecated')) {
|
||||
$message = $deprecated[0]->nodeValue ?: '';
|
||||
$package = $deprecated[0]->getAttribute('package') ?: '';
|
||||
$version = $deprecated[0]->getAttribute('version') ?: '';
|
||||
|
||||
if (!$deprecated[0]->hasAttribute('package')) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "package" of the node "deprecated" in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
if (!$deprecated[0]->hasAttribute('version')) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "version" of the node "deprecated" in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
$alias->setDeprecated($package, $version, $message);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isLoadingInstanceof) {
|
||||
$definition = new ChildDefinition('');
|
||||
} elseif ($parent = $service->getAttribute('parent')) {
|
||||
$definition = new ChildDefinition($parent);
|
||||
} else {
|
||||
$definition = new Definition();
|
||||
}
|
||||
|
||||
if ($defaults->getChanges()['public'] ?? false) {
|
||||
$definition->setPublic($defaults->isPublic());
|
||||
}
|
||||
$definition->setAutowired($defaults->isAutowired());
|
||||
$definition->setAutoconfigured($defaults->isAutoconfigured());
|
||||
$definition->setChanges([]);
|
||||
|
||||
foreach (['class', 'public', 'shared', 'synthetic', 'abstract'] as $key) {
|
||||
if ($value = $service->getAttribute($key)) {
|
||||
$method = 'set'.$key;
|
||||
$definition->$method($value = XmlUtils::phpize($value));
|
||||
}
|
||||
}
|
||||
|
||||
if ($value = $service->getAttribute('lazy')) {
|
||||
$definition->setLazy((bool) $value = XmlUtils::phpize($value));
|
||||
if (\is_string($value)) {
|
||||
$definition->addTag('proxy', ['interface' => $value]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($value = $service->getAttribute('autowire')) {
|
||||
$definition->setAutowired(XmlUtils::phpize($value));
|
||||
}
|
||||
|
||||
if ($value = $service->getAttribute('autoconfigure')) {
|
||||
$definition->setAutoconfigured(XmlUtils::phpize($value));
|
||||
}
|
||||
|
||||
if ($files = $this->getChildren($service, 'file')) {
|
||||
$definition->setFile($files[0]->nodeValue);
|
||||
}
|
||||
|
||||
if ($deprecated = $this->getChildren($service, 'deprecated')) {
|
||||
$message = $deprecated[0]->nodeValue ?: '';
|
||||
$package = $deprecated[0]->getAttribute('package') ?: '';
|
||||
$version = $deprecated[0]->getAttribute('version') ?: '';
|
||||
|
||||
if ('' === $package) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "package" of the node "deprecated" in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
if ('' === $version) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "version" of the node "deprecated" in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
$definition->setDeprecated($package, $version, $message);
|
||||
}
|
||||
|
||||
$definition->setArguments($this->getArgumentsAsPhp($service, 'argument', $file, $definition instanceof ChildDefinition));
|
||||
$definition->setProperties($this->getArgumentsAsPhp($service, 'property', $file));
|
||||
|
||||
if ($factories = $this->getChildren($service, 'factory')) {
|
||||
$factory = $factories[0];
|
||||
if ($function = $factory->getAttribute('function')) {
|
||||
$definition->setFactory($function);
|
||||
} else {
|
||||
if ($childService = $factory->getAttribute('service')) {
|
||||
$class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
|
||||
} else {
|
||||
$class = $factory->hasAttribute('class') ? $factory->getAttribute('class') : null;
|
||||
}
|
||||
|
||||
$definition->setFactory([$class, $factory->getAttribute('method') ?: '__invoke']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($configurators = $this->getChildren($service, 'configurator')) {
|
||||
$configurator = $configurators[0];
|
||||
if ($function = $configurator->getAttribute('function')) {
|
||||
$definition->setConfigurator($function);
|
||||
} else {
|
||||
if ($childService = $configurator->getAttribute('service')) {
|
||||
$class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
|
||||
} else {
|
||||
$class = $configurator->getAttribute('class');
|
||||
}
|
||||
|
||||
$definition->setConfigurator([$class, $configurator->getAttribute('method') ?: '__invoke']);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->getChildren($service, 'call') as $call) {
|
||||
$definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument', $file), XmlUtils::phpize($call->getAttribute('returns-clone')));
|
||||
}
|
||||
|
||||
$tags = $this->getChildren($service, 'tag');
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
$parameters = [];
|
||||
$tagName = $tag->nodeValue;
|
||||
foreach ($tag->attributes as $name => $node) {
|
||||
if ('name' === $name && '' === $tagName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (str_contains($name, '-') && !str_contains($name, '_') && !\array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
|
||||
$parameters[$normalizedName] = XmlUtils::phpize($node->nodeValue);
|
||||
}
|
||||
// keep not normalized key
|
||||
$parameters[$name] = XmlUtils::phpize($node->nodeValue);
|
||||
}
|
||||
|
||||
if ('' === $tagName && '' === $tagName = $tag->getAttribute('name')) {
|
||||
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', $service->getAttribute('id'), $file));
|
||||
}
|
||||
|
||||
$definition->addTag($tagName, $parameters);
|
||||
}
|
||||
|
||||
$definition->setTags(array_merge_recursive($definition->getTags(), $defaults->getTags()));
|
||||
|
||||
$bindings = $this->getArgumentsAsPhp($service, 'bind', $file);
|
||||
$bindingType = $this->isLoadingInstanceof ? BoundArgument::INSTANCEOF_BINDING : BoundArgument::SERVICE_BINDING;
|
||||
foreach ($bindings as $argument => $value) {
|
||||
$bindings[$argument] = new BoundArgument($value, true, $bindingType, $file);
|
||||
}
|
||||
|
||||
// deep clone, to avoid multiple process of the same instance in the passes
|
||||
$bindings = array_merge(unserialize(serialize($defaults->getBindings())), $bindings);
|
||||
|
||||
if ($bindings) {
|
||||
$definition->setBindings($bindings);
|
||||
}
|
||||
|
||||
if ($decorates = $service->getAttribute('decorates')) {
|
||||
$decorationOnInvalid = $service->getAttribute('decoration-on-invalid') ?: 'exception';
|
||||
if ('exception' === $decorationOnInvalid) {
|
||||
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
||||
} elseif ('ignore' === $decorationOnInvalid) {
|
||||
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
||||
} elseif ('null' === $decorationOnInvalid) {
|
||||
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
|
||||
} else {
|
||||
throw new InvalidArgumentException(sprintf('Invalid value "%s" for attribute "decoration-on-invalid" on service "%s". Did you mean "exception", "ignore" or "null" in "%s"?', $decorationOnInvalid, $service->getAttribute('id'), $file));
|
||||
}
|
||||
|
||||
$renameId = $service->hasAttribute('decoration-inner-name') ? $service->getAttribute('decoration-inner-name') : null;
|
||||
$priority = $service->hasAttribute('decoration-priority') ? $service->getAttribute('decoration-priority') : 0;
|
||||
|
||||
$definition->setDecoratedService($decorates, $renameId, $priority, $invalidBehavior);
|
||||
}
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an XML file to a \DOMDocument.
|
||||
*
|
||||
* @throws InvalidArgumentException When loading of XML file returns error
|
||||
*/
|
||||
private function parseFileToDOM(string $file): \DOMDocument
|
||||
{
|
||||
try {
|
||||
$dom = XmlUtils::loadFile($file, [$this, 'validateSchema']);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
throw new InvalidArgumentException(sprintf('Unable to parse file "%s": ', $file).$e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
$this->validateExtensions($dom, $file);
|
||||
|
||||
return $dom;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes anonymous services.
|
||||
*/
|
||||
private function processAnonymousServices(\DOMDocument $xml, string $file, \DOMNode $root = null)
|
||||
{
|
||||
$definitions = [];
|
||||
$count = 0;
|
||||
$suffix = '~'.ContainerBuilder::hash($file);
|
||||
|
||||
$xpath = new \DOMXPath($xml);
|
||||
$xpath->registerNamespace('container', self::NS);
|
||||
|
||||
// anonymous services as arguments/properties
|
||||
if (false !== $nodes = $xpath->query('.//container:argument[@type="service"][not(@id)]|.//container:property[@type="service"][not(@id)]|.//container:bind[not(@id)]|.//container:factory[not(@service)]|.//container:configurator[not(@service)]', $root)) {
|
||||
foreach ($nodes as $node) {
|
||||
if ($services = $this->getChildren($node, 'service')) {
|
||||
// give it a unique name
|
||||
$id = sprintf('.%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $services[0]->getAttribute('class')).$suffix);
|
||||
$node->setAttribute('id', $id);
|
||||
$node->setAttribute('service', $id);
|
||||
|
||||
$definitions[$id] = [$services[0], $file];
|
||||
$services[0]->setAttribute('id', $id);
|
||||
|
||||
// anonymous services are always private
|
||||
// we could not use the constant false here, because of XML parsing
|
||||
$services[0]->setAttribute('public', 'false');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// anonymous services "in the wild"
|
||||
if (false !== $nodes = $xpath->query('.//container:services/container:service[not(@id)]', $root)) {
|
||||
foreach ($nodes as $node) {
|
||||
throw new InvalidArgumentException(sprintf('Top-level services must have "id" attribute, none found in "%s" at line %d.', $file, $node->getLineNo()));
|
||||
}
|
||||
}
|
||||
|
||||
// resolve definitions
|
||||
uksort($definitions, 'strnatcmp');
|
||||
foreach (array_reverse($definitions) as $id => [$domElement, $file]) {
|
||||
if (null !== $definition = $this->parseDefinition($domElement, $file, new Definition())) {
|
||||
$this->setDefinition($id, $definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getArgumentsAsPhp(\DOMElement $node, string $name, string $file, bool $isChildDefinition = false): array
|
||||
{
|
||||
$arguments = [];
|
||||
foreach ($this->getChildren($node, $name) as $arg) {
|
||||
if ($arg->hasAttribute('name')) {
|
||||
$arg->setAttribute('key', $arg->getAttribute('name'));
|
||||
}
|
||||
|
||||
// this is used by ChildDefinition to overwrite a specific
|
||||
// argument of the parent definition
|
||||
if ($arg->hasAttribute('index')) {
|
||||
$key = ($isChildDefinition ? 'index_' : '').$arg->getAttribute('index');
|
||||
} elseif (!$arg->hasAttribute('key')) {
|
||||
// Append an empty argument, then fetch its key to overwrite it later
|
||||
$arguments[] = null;
|
||||
$keys = array_keys($arguments);
|
||||
$key = array_pop($keys);
|
||||
} else {
|
||||
$key = $arg->getAttribute('key');
|
||||
}
|
||||
|
||||
$onInvalid = $arg->getAttribute('on-invalid');
|
||||
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
||||
if ('ignore' == $onInvalid) {
|
||||
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
||||
} elseif ('ignore_uninitialized' == $onInvalid) {
|
||||
$invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE;
|
||||
} elseif ('null' == $onInvalid) {
|
||||
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
|
||||
}
|
||||
|
||||
switch ($arg->getAttribute('type')) {
|
||||
case 'service':
|
||||
if ('' === $arg->getAttribute('id')) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service" has no or empty "id" attribute in "%s".', $name, $file));
|
||||
}
|
||||
|
||||
$arguments[$key] = new Reference($arg->getAttribute('id'), $invalidBehavior);
|
||||
break;
|
||||
case 'expression':
|
||||
if (!class_exists(Expression::class)) {
|
||||
throw new \LogicException('The type="expression" attribute cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".');
|
||||
}
|
||||
|
||||
$arguments[$key] = new Expression($arg->nodeValue);
|
||||
break;
|
||||
case 'collection':
|
||||
$arguments[$key] = $this->getArgumentsAsPhp($arg, $name, $file);
|
||||
break;
|
||||
case 'iterator':
|
||||
$arg = $this->getArgumentsAsPhp($arg, $name, $file);
|
||||
try {
|
||||
$arguments[$key] = new IteratorArgument($arg);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references in "%s".', $name, $file));
|
||||
}
|
||||
break;
|
||||
case 'service_closure':
|
||||
if ('' === $arg->getAttribute('id')) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service_closure" has no or empty "id" attribute in "%s".', $name, $file));
|
||||
}
|
||||
|
||||
$arguments[$key] = new ServiceClosureArgument(new Reference($arg->getAttribute('id'), $invalidBehavior));
|
||||
break;
|
||||
case 'service_locator':
|
||||
$arg = $this->getArgumentsAsPhp($arg, $name, $file);
|
||||
try {
|
||||
$arguments[$key] = new ServiceLocatorArgument($arg);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service_locator" only accepts maps of type="service" references in "%s".', $name, $file));
|
||||
}
|
||||
break;
|
||||
case 'tagged':
|
||||
case 'tagged_iterator':
|
||||
case 'tagged_locator':
|
||||
$type = $arg->getAttribute('type');
|
||||
$forLocator = 'tagged_locator' === $type;
|
||||
|
||||
if (!$arg->getAttribute('tag')) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="%s" has no or empty "tag" attribute in "%s".', $name, $type, $file));
|
||||
}
|
||||
|
||||
$arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by') ?: null, $arg->getAttribute('default-index-method') ?: null, $forLocator, $arg->getAttribute('default-priority-method') ?: null);
|
||||
|
||||
if ($forLocator) {
|
||||
$arguments[$key] = new ServiceLocatorArgument($arguments[$key]);
|
||||
}
|
||||
break;
|
||||
case 'binary':
|
||||
if (false === $value = base64_decode($arg->nodeValue)) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="binary" is not a valid base64 encoded string.', $name));
|
||||
}
|
||||
$arguments[$key] = $value;
|
||||
break;
|
||||
case 'abstract':
|
||||
$arguments[$key] = new AbstractArgument($arg->nodeValue);
|
||||
break;
|
||||
case 'string':
|
||||
$arguments[$key] = $arg->nodeValue;
|
||||
break;
|
||||
case 'constant':
|
||||
$arguments[$key] = \constant(trim($arg->nodeValue));
|
||||
break;
|
||||
default:
|
||||
$arguments[$key] = XmlUtils::phpize($arg->nodeValue);
|
||||
}
|
||||
}
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get child elements by name.
|
||||
*
|
||||
* @return \DOMElement[]
|
||||
*/
|
||||
private function getChildren(\DOMNode $node, string $name): array
|
||||
{
|
||||
$children = [];
|
||||
foreach ($node->childNodes as $child) {
|
||||
if ($child instanceof \DOMElement && $child->localName === $name && self::NS === $child->namespaceURI) {
|
||||
$children[] = $child;
|
||||
}
|
||||
}
|
||||
|
||||
return $children;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a documents XML schema.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws RuntimeException When extension references a non-existent XSD file
|
||||
*/
|
||||
public function validateSchema(\DOMDocument $dom)
|
||||
{
|
||||
$schemaLocations = ['http://symfony.com/schema/dic/services' => str_replace('\\', '/', __DIR__.'/schema/dic/services/services-1.0.xsd')];
|
||||
|
||||
if ($element = $dom->documentElement->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')) {
|
||||
$items = preg_split('/\s+/', $element);
|
||||
for ($i = 0, $nb = \count($items); $i < $nb; $i += 2) {
|
||||
if (!$this->container->hasExtension($items[$i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (($extension = $this->container->getExtension($items[$i])) && false !== $extension->getXsdValidationBasePath()) {
|
||||
$ns = $extension->getNamespace();
|
||||
$path = str_replace([$ns, str_replace('http://', 'https://', $ns)], str_replace('\\', '/', $extension->getXsdValidationBasePath()).'/', $items[$i + 1]);
|
||||
|
||||
if (!is_file($path)) {
|
||||
throw new RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s".', get_debug_type($extension), $path));
|
||||
}
|
||||
|
||||
$schemaLocations[$items[$i]] = $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$tmpfiles = [];
|
||||
$imports = '';
|
||||
foreach ($schemaLocations as $namespace => $location) {
|
||||
$parts = explode('/', $location);
|
||||
$locationstart = 'file:///';
|
||||
if (0 === stripos($location, 'phar://')) {
|
||||
$tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
|
||||
if ($tmpfile) {
|
||||
copy($location, $tmpfile);
|
||||
$tmpfiles[] = $tmpfile;
|
||||
$parts = explode('/', str_replace('\\', '/', $tmpfile));
|
||||
} else {
|
||||
array_shift($parts);
|
||||
$locationstart = 'phar:///';
|
||||
}
|
||||
} elseif ('\\' === \DIRECTORY_SEPARATOR && str_starts_with($location, '\\\\')) {
|
||||
$locationstart = '';
|
||||
}
|
||||
$drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
|
||||
$location = $locationstart.$drive.implode('/', array_map('rawurlencode', $parts));
|
||||
|
||||
$imports .= sprintf(' <xsd:import namespace="%s" schemaLocation="%s" />'."\n", $namespace, $location);
|
||||
}
|
||||
|
||||
$source = <<<EOF
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<xsd:schema xmlns="http://symfony.com/schema"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
||||
targetNamespace="http://symfony.com/schema"
|
||||
elementFormDefault="qualified">
|
||||
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
|
||||
$imports
|
||||
</xsd:schema>
|
||||
EOF
|
||||
;
|
||||
|
||||
if ($this->shouldEnableEntityLoader()) {
|
||||
$disableEntities = libxml_disable_entity_loader(false);
|
||||
$valid = @$dom->schemaValidateSource($source);
|
||||
libxml_disable_entity_loader($disableEntities);
|
||||
} else {
|
||||
$valid = @$dom->schemaValidateSource($source);
|
||||
}
|
||||
foreach ($tmpfiles as $tmpfile) {
|
||||
@unlink($tmpfile);
|
||||
}
|
||||
|
||||
return $valid;
|
||||
}
|
||||
|
||||
private function shouldEnableEntityLoader(): bool
|
||||
{
|
||||
// Version prior to 8.0 can be enabled without deprecation
|
||||
if (\PHP_VERSION_ID < 80000) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static $dom, $schema;
|
||||
if (null === $dom) {
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadXML('<?xml version="1.0"?><test/>');
|
||||
|
||||
$tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
|
||||
register_shutdown_function(static function () use ($tmpfile) {
|
||||
@unlink($tmpfile);
|
||||
});
|
||||
$schema = '<?xml version="1.0" encoding="utf-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
<xsd:include schemaLocation="file:///'.rawurlencode(str_replace('\\', '/', $tmpfile)).'" />
|
||||
</xsd:schema>';
|
||||
file_put_contents($tmpfile, '<?xml version="1.0" encoding="utf-8"?>
|
||||
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
<xsd:element name="test" type="testType" />
|
||||
<xsd:complexType name="testType"/>
|
||||
</xsd:schema>');
|
||||
}
|
||||
|
||||
return !@$dom->schemaValidateSource($schema);
|
||||
}
|
||||
|
||||
private function validateAlias(\DOMElement $alias, string $file)
|
||||
{
|
||||
foreach ($alias->attributes as $name => $node) {
|
||||
if (!\in_array($name, ['alias', 'id', 'public'])) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid attribute "%s" defined for alias "%s" in "%s".', $name, $alias->getAttribute('id'), $file));
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($alias->childNodes as $child) {
|
||||
if (!$child instanceof \DOMElement || self::NS !== $child->namespaceURI) {
|
||||
continue;
|
||||
}
|
||||
if (!\in_array($child->localName, ['deprecated'], true)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid child element "%s" defined for alias "%s" in "%s".', $child->localName, $alias->getAttribute('id'), $file));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates an extension.
|
||||
*
|
||||
* @throws InvalidArgumentException When no extension is found corresponding to a tag
|
||||
*/
|
||||
private function validateExtensions(\DOMDocument $dom, string $file)
|
||||
{
|
||||
foreach ($dom->documentElement->childNodes as $node) {
|
||||
if (!$node instanceof \DOMElement || 'http://symfony.com/schema/dic/services' === $node->namespaceURI) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// can it be handled by an extension?
|
||||
if (!$this->container->hasExtension($node->namespaceURI)) {
|
||||
$extensionNamespaces = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getNamespace(); }, $this->container->getExtensions()));
|
||||
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $node->tagName, $file, $node->namespaceURI, $extensionNamespaces ? implode('", "', $extensionNamespaces) : 'none'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads from an extension.
|
||||
*/
|
||||
private function loadFromExtensions(\DOMDocument $xml)
|
||||
{
|
||||
foreach ($xml->documentElement->childNodes as $node) {
|
||||
if (!$node instanceof \DOMElement || self::NS === $node->namespaceURI) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$values = static::convertDomElementToArray($node);
|
||||
if (!\is_array($values)) {
|
||||
$values = [];
|
||||
}
|
||||
|
||||
$this->container->loadFromExtension($node->namespaceURI, $values);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a \DOMElement object to a PHP array.
|
||||
*
|
||||
* The following rules applies during the conversion:
|
||||
*
|
||||
* * Each tag is converted to a key value or an array
|
||||
* if there is more than one "value"
|
||||
*
|
||||
* * The content of a tag is set under a "value" key (<foo>bar</foo>)
|
||||
* if the tag also has some nested tags
|
||||
*
|
||||
* * The attributes are converted to keys (<foo foo="bar"/>)
|
||||
*
|
||||
* * The nested-tags are converted to keys (<foo><foo>bar</foo></foo>)
|
||||
*
|
||||
* @param \DOMElement $element A \DOMElement instance
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function convertDomElementToArray(\DOMElement $element)
|
||||
{
|
||||
return XmlUtils::convertDomElementToArray($element);
|
||||
}
|
||||
}
|
|
@ -1,966 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Loader;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
|
||||
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
|
||||
use Symfony\Component\DependencyInjection\ChildDefinition;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
||||
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\ExpressionLanguage\Expression;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Parser as YamlParser;
|
||||
use Symfony\Component\Yaml\Tag\TaggedValue;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/**
|
||||
* YamlFileLoader loads YAML files service definitions.
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class YamlFileLoader extends FileLoader
|
||||
{
|
||||
private const SERVICE_KEYWORDS = [
|
||||
'alias' => 'alias',
|
||||
'parent' => 'parent',
|
||||
'class' => 'class',
|
||||
'shared' => 'shared',
|
||||
'synthetic' => 'synthetic',
|
||||
'lazy' => 'lazy',
|
||||
'public' => 'public',
|
||||
'abstract' => 'abstract',
|
||||
'deprecated' => 'deprecated',
|
||||
'factory' => 'factory',
|
||||
'file' => 'file',
|
||||
'arguments' => 'arguments',
|
||||
'properties' => 'properties',
|
||||
'configurator' => 'configurator',
|
||||
'calls' => 'calls',
|
||||
'tags' => 'tags',
|
||||
'decorates' => 'decorates',
|
||||
'decoration_inner_name' => 'decoration_inner_name',
|
||||
'decoration_priority' => 'decoration_priority',
|
||||
'decoration_on_invalid' => 'decoration_on_invalid',
|
||||
'autowire' => 'autowire',
|
||||
'autoconfigure' => 'autoconfigure',
|
||||
'bind' => 'bind',
|
||||
];
|
||||
|
||||
private const PROTOTYPE_KEYWORDS = [
|
||||
'resource' => 'resource',
|
||||
'namespace' => 'namespace',
|
||||
'exclude' => 'exclude',
|
||||
'parent' => 'parent',
|
||||
'shared' => 'shared',
|
||||
'lazy' => 'lazy',
|
||||
'public' => 'public',
|
||||
'abstract' => 'abstract',
|
||||
'deprecated' => 'deprecated',
|
||||
'factory' => 'factory',
|
||||
'arguments' => 'arguments',
|
||||
'properties' => 'properties',
|
||||
'configurator' => 'configurator',
|
||||
'calls' => 'calls',
|
||||
'tags' => 'tags',
|
||||
'autowire' => 'autowire',
|
||||
'autoconfigure' => 'autoconfigure',
|
||||
'bind' => 'bind',
|
||||
];
|
||||
|
||||
private const INSTANCEOF_KEYWORDS = [
|
||||
'shared' => 'shared',
|
||||
'lazy' => 'lazy',
|
||||
'public' => 'public',
|
||||
'properties' => 'properties',
|
||||
'configurator' => 'configurator',
|
||||
'calls' => 'calls',
|
||||
'tags' => 'tags',
|
||||
'autowire' => 'autowire',
|
||||
'bind' => 'bind',
|
||||
];
|
||||
|
||||
private const DEFAULTS_KEYWORDS = [
|
||||
'public' => 'public',
|
||||
'tags' => 'tags',
|
||||
'autowire' => 'autowire',
|
||||
'autoconfigure' => 'autoconfigure',
|
||||
'bind' => 'bind',
|
||||
];
|
||||
|
||||
private $yamlParser;
|
||||
|
||||
private $anonymousServicesCount;
|
||||
private $anonymousServicesSuffix;
|
||||
|
||||
protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function load($resource, string $type = null)
|
||||
{
|
||||
$path = $this->locator->locate($resource);
|
||||
|
||||
$content = $this->loadFile($path);
|
||||
|
||||
$this->container->fileExists($path);
|
||||
|
||||
// empty file
|
||||
if (null === $content) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->loadContent($content, $path);
|
||||
|
||||
// per-env configuration
|
||||
if ($this->env && isset($content['when@'.$this->env])) {
|
||||
if (!\is_array($content['when@'.$this->env])) {
|
||||
throw new InvalidArgumentException(sprintf('The "when@%s" key should contain an array in "%s". Check your YAML syntax.', $this->env, $path));
|
||||
}
|
||||
|
||||
$env = $this->env;
|
||||
$this->env = null;
|
||||
try {
|
||||
$this->loadContent($content['when@'.$env], $path);
|
||||
} finally {
|
||||
$this->env = $env;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function loadContent(array $content, string $path)
|
||||
{
|
||||
// imports
|
||||
$this->parseImports($content, $path);
|
||||
|
||||
// parameters
|
||||
if (isset($content['parameters'])) {
|
||||
if (!\is_array($content['parameters'])) {
|
||||
throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in "%s". Check your YAML syntax.', $path));
|
||||
}
|
||||
|
||||
foreach ($content['parameters'] as $key => $value) {
|
||||
$this->container->setParameter($key, $this->resolveServices($value, $path, true));
|
||||
}
|
||||
}
|
||||
|
||||
// extensions
|
||||
$this->loadFromExtensions($content);
|
||||
|
||||
// services
|
||||
$this->anonymousServicesCount = 0;
|
||||
$this->anonymousServicesSuffix = '~'.ContainerBuilder::hash($path);
|
||||
$this->setCurrentDir(\dirname($path));
|
||||
try {
|
||||
$this->parseDefinitions($content, $path);
|
||||
} finally {
|
||||
$this->instanceof = [];
|
||||
$this->registerAliasesForSinglyImplementedInterfaces();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supports($resource, string $type = null)
|
||||
{
|
||||
if (!\is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && \in_array(pathinfo($resource, \PATHINFO_EXTENSION), ['yaml', 'yml'], true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return \in_array($type, ['yaml', 'yml'], true);
|
||||
}
|
||||
|
||||
private function parseImports(array $content, string $file)
|
||||
{
|
||||
if (!isset($content['imports'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\is_array($content['imports'])) {
|
||||
throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in "%s". Check your YAML syntax.', $file));
|
||||
}
|
||||
|
||||
$defaultDirectory = \dirname($file);
|
||||
foreach ($content['imports'] as $import) {
|
||||
if (!\is_array($import)) {
|
||||
$import = ['resource' => $import];
|
||||
}
|
||||
if (!isset($import['resource'])) {
|
||||
throw new InvalidArgumentException(sprintf('An import should provide a resource in "%s". Check your YAML syntax.', $file));
|
||||
}
|
||||
|
||||
$this->setCurrentDir($defaultDirectory);
|
||||
$this->import($import['resource'], $import['type'] ?? null, $import['ignore_errors'] ?? false, $file);
|
||||
}
|
||||
}
|
||||
|
||||
private function parseDefinitions(array $content, string $file, bool $trackBindings = true)
|
||||
{
|
||||
if (!isset($content['services'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!\is_array($content['services'])) {
|
||||
throw new InvalidArgumentException(sprintf('The "services" key should contain an array in "%s". Check your YAML syntax.', $file));
|
||||
}
|
||||
|
||||
if (\array_key_exists('_instanceof', $content['services'])) {
|
||||
$instanceof = $content['services']['_instanceof'];
|
||||
unset($content['services']['_instanceof']);
|
||||
|
||||
if (!\is_array($instanceof)) {
|
||||
throw new InvalidArgumentException(sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', get_debug_type($instanceof), $file));
|
||||
}
|
||||
$this->instanceof = [];
|
||||
$this->isLoadingInstanceof = true;
|
||||
foreach ($instanceof as $id => $service) {
|
||||
if (!$service || !\is_array($service)) {
|
||||
throw new InvalidArgumentException(sprintf('Type definition "%s" must be a non-empty array within "_instanceof" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
if (\is_string($service) && str_starts_with($service, '@')) {
|
||||
throw new InvalidArgumentException(sprintf('Type definition "%s" cannot be an alias within "_instanceof" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
$this->parseDefinition($id, $service, $file, [], false, $trackBindings);
|
||||
}
|
||||
}
|
||||
|
||||
$this->isLoadingInstanceof = false;
|
||||
$defaults = $this->parseDefaults($content, $file);
|
||||
foreach ($content['services'] as $id => $service) {
|
||||
$this->parseDefinition($id, $service, $file, $defaults, false, $trackBindings);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function parseDefaults(array &$content, string $file): array
|
||||
{
|
||||
if (!\array_key_exists('_defaults', $content['services'])) {
|
||||
return [];
|
||||
}
|
||||
$defaults = $content['services']['_defaults'];
|
||||
unset($content['services']['_defaults']);
|
||||
|
||||
if (!\is_array($defaults)) {
|
||||
throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', get_debug_type($defaults), $file));
|
||||
}
|
||||
|
||||
foreach ($defaults as $key => $default) {
|
||||
if (!isset(self::DEFAULTS_KEYWORDS[$key])) {
|
||||
throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".', $key, $file, implode('", "', self::DEFAULTS_KEYWORDS)));
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($defaults['tags'])) {
|
||||
if (!\is_array($tags = $defaults['tags'])) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in "%s". Check your YAML syntax.', $file));
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (!\is_array($tag)) {
|
||||
$tag = ['name' => $tag];
|
||||
}
|
||||
|
||||
if (1 === \count($tag) && \is_array(current($tag))) {
|
||||
$name = key($tag);
|
||||
$tag = current($tag);
|
||||
} else {
|
||||
if (!isset($tag['name'])) {
|
||||
throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in "%s".', $file));
|
||||
}
|
||||
$name = $tag['name'];
|
||||
unset($tag['name']);
|
||||
}
|
||||
|
||||
if (!\is_string($name) || '' === $name) {
|
||||
throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in "%s".', $file));
|
||||
}
|
||||
|
||||
foreach ($tag as $attribute => $value) {
|
||||
if (!\is_scalar($value) && null !== $value) {
|
||||
throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in "%s". Check your YAML syntax.', $name, $attribute, $file));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($defaults['bind'])) {
|
||||
if (!\is_array($defaults['bind'])) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter "bind" in "_defaults" must be an array in "%s". Check your YAML syntax.', $file));
|
||||
}
|
||||
|
||||
foreach ($this->resolveServices($defaults['bind'], $file) as $argument => $value) {
|
||||
$defaults['bind'][$argument] = new BoundArgument($value, true, BoundArgument::DEFAULTS_BINDING, $file);
|
||||
}
|
||||
}
|
||||
|
||||
return $defaults;
|
||||
}
|
||||
|
||||
private function isUsingShortSyntax(array $service): bool
|
||||
{
|
||||
foreach ($service as $key => $value) {
|
||||
if (\is_string($key) && ('' === $key || ('$' !== $key[0] && !str_contains($key, '\\')))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a definition.
|
||||
*
|
||||
* @param array|string|null $service
|
||||
*
|
||||
* @throws InvalidArgumentException When tags are invalid
|
||||
*/
|
||||
private function parseDefinition(string $id, $service, string $file, array $defaults, bool $return = false, bool $trackBindings = true)
|
||||
{
|
||||
if (preg_match('/^_[a-zA-Z0-9_]*$/', $id)) {
|
||||
throw new InvalidArgumentException(sprintf('Service names that start with an underscore are reserved. Rename the "%s" service or define it in XML instead.', $id));
|
||||
}
|
||||
|
||||
if (\is_string($service) && str_starts_with($service, '@')) {
|
||||
$alias = new Alias(substr($service, 1));
|
||||
|
||||
if (isset($defaults['public'])) {
|
||||
$alias->setPublic($defaults['public']);
|
||||
}
|
||||
|
||||
return $return ? $alias : $this->container->setAlias($id, $alias);
|
||||
}
|
||||
|
||||
if (\is_array($service) && $this->isUsingShortSyntax($service)) {
|
||||
$service = ['arguments' => $service];
|
||||
}
|
||||
|
||||
if (null === $service) {
|
||||
$service = [];
|
||||
}
|
||||
|
||||
if (!\is_array($service)) {
|
||||
throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but "%s" found for service "%s" in "%s". Check your YAML syntax.', get_debug_type($service), $id, $file));
|
||||
}
|
||||
|
||||
if (isset($service['stack'])) {
|
||||
if (!\is_array($service['stack'])) {
|
||||
throw new InvalidArgumentException(sprintf('A stack must be an array of definitions, "%s" given for service "%s" in "%s". Check your YAML syntax.', get_debug_type($service), $id, $file));
|
||||
}
|
||||
|
||||
$stack = [];
|
||||
|
||||
foreach ($service['stack'] as $k => $frame) {
|
||||
if (\is_array($frame) && 1 === \count($frame) && !isset(self::SERVICE_KEYWORDS[key($frame)])) {
|
||||
$frame = [
|
||||
'class' => key($frame),
|
||||
'arguments' => current($frame),
|
||||
];
|
||||
}
|
||||
|
||||
if (\is_array($frame) && isset($frame['stack'])) {
|
||||
throw new InvalidArgumentException(sprintf('Service stack "%s" cannot contain another stack in "%s".', $id, $file));
|
||||
}
|
||||
|
||||
$definition = $this->parseDefinition($id.'" at index "'.$k, $frame, $file, $defaults, true);
|
||||
|
||||
if ($definition instanceof Definition) {
|
||||
$definition->setInstanceofConditionals($this->instanceof);
|
||||
}
|
||||
|
||||
$stack[$k] = $definition;
|
||||
}
|
||||
|
||||
if ($diff = array_diff(array_keys($service), ['stack', 'public', 'deprecated'])) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid attribute "%s"; supported ones are "public" and "deprecated" for service "%s" in "%s". Check your YAML syntax.', implode('", "', $diff), $id, $file));
|
||||
}
|
||||
|
||||
$service = [
|
||||
'parent' => '',
|
||||
'arguments' => $stack,
|
||||
'tags' => ['container.stack'],
|
||||
'public' => $service['public'] ?? null,
|
||||
'deprecated' => $service['deprecated'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
$definition = isset($service[0]) && $service[0] instanceof Definition ? array_shift($service) : null;
|
||||
$return = null === $definition ? $return : true;
|
||||
|
||||
$this->checkDefinition($id, $service, $file);
|
||||
|
||||
if (isset($service['alias'])) {
|
||||
$alias = new Alias($service['alias']);
|
||||
|
||||
if (isset($service['public'])) {
|
||||
$alias->setPublic($service['public']);
|
||||
} elseif (isset($defaults['public'])) {
|
||||
$alias->setPublic($defaults['public']);
|
||||
}
|
||||
|
||||
foreach ($service as $key => $value) {
|
||||
if (!\in_array($key, ['alias', 'public', 'deprecated'])) {
|
||||
throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias", "public" and "deprecated".', $key, $id, $file));
|
||||
}
|
||||
|
||||
if ('deprecated' === $key) {
|
||||
$deprecation = \is_array($value) ? $value : ['message' => $value];
|
||||
|
||||
if (!isset($deprecation['package'])) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "package" of the "deprecated" option in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
if (!isset($deprecation['version'])) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "version" of the "deprecated" option in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
$alias->setDeprecated($deprecation['package'] ?? '', $deprecation['version'] ?? '', $deprecation['message'] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
return $return ? $alias : $this->container->setAlias($id, $alias);
|
||||
}
|
||||
|
||||
if (null !== $definition) {
|
||||
// no-op
|
||||
} elseif ($this->isLoadingInstanceof) {
|
||||
$definition = new ChildDefinition('');
|
||||
} elseif (isset($service['parent'])) {
|
||||
if ('' !== $service['parent'] && '@' === $service['parent'][0]) {
|
||||
throw new InvalidArgumentException(sprintf('The value of the "parent" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['parent'], substr($service['parent'], 1)));
|
||||
}
|
||||
|
||||
$definition = new ChildDefinition($service['parent']);
|
||||
} else {
|
||||
$definition = new Definition();
|
||||
}
|
||||
|
||||
if (isset($defaults['public'])) {
|
||||
$definition->setPublic($defaults['public']);
|
||||
}
|
||||
if (isset($defaults['autowire'])) {
|
||||
$definition->setAutowired($defaults['autowire']);
|
||||
}
|
||||
if (isset($defaults['autoconfigure'])) {
|
||||
$definition->setAutoconfigured($defaults['autoconfigure']);
|
||||
}
|
||||
|
||||
$definition->setChanges([]);
|
||||
|
||||
if (isset($service['class'])) {
|
||||
$definition->setClass($service['class']);
|
||||
}
|
||||
|
||||
if (isset($service['shared'])) {
|
||||
$definition->setShared($service['shared']);
|
||||
}
|
||||
|
||||
if (isset($service['synthetic'])) {
|
||||
$definition->setSynthetic($service['synthetic']);
|
||||
}
|
||||
|
||||
if (isset($service['lazy'])) {
|
||||
$definition->setLazy((bool) $service['lazy']);
|
||||
if (\is_string($service['lazy'])) {
|
||||
$definition->addTag('proxy', ['interface' => $service['lazy']]);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($service['public'])) {
|
||||
$definition->setPublic($service['public']);
|
||||
}
|
||||
|
||||
if (isset($service['abstract'])) {
|
||||
$definition->setAbstract($service['abstract']);
|
||||
}
|
||||
|
||||
if (isset($service['deprecated'])) {
|
||||
$deprecation = \is_array($service['deprecated']) ? $service['deprecated'] : ['message' => $service['deprecated']];
|
||||
|
||||
if (!isset($deprecation['package'])) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "package" of the "deprecated" option in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
if (!isset($deprecation['version'])) {
|
||||
trigger_deprecation('symfony/dependency-injection', '5.1', 'Not setting the attribute "version" of the "deprecated" option in "%s" is deprecated.', $file);
|
||||
}
|
||||
|
||||
$definition->setDeprecated($deprecation['package'] ?? '', $deprecation['version'] ?? '', $deprecation['message'] ?? '');
|
||||
}
|
||||
|
||||
if (isset($service['factory'])) {
|
||||
$definition->setFactory($this->parseCallable($service['factory'], 'factory', $id, $file));
|
||||
}
|
||||
|
||||
if (isset($service['file'])) {
|
||||
$definition->setFile($service['file']);
|
||||
}
|
||||
|
||||
if (isset($service['arguments'])) {
|
||||
$definition->setArguments($this->resolveServices($service['arguments'], $file));
|
||||
}
|
||||
|
||||
if (isset($service['properties'])) {
|
||||
$definition->setProperties($this->resolveServices($service['properties'], $file));
|
||||
}
|
||||
|
||||
if (isset($service['configurator'])) {
|
||||
$definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator', $id, $file));
|
||||
}
|
||||
|
||||
if (isset($service['calls'])) {
|
||||
if (!\is_array($service['calls'])) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
|
||||
foreach ($service['calls'] as $k => $call) {
|
||||
if (!\is_array($call) && (!\is_string($k) || !$call instanceof TaggedValue)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid method call for service "%s": expected map or array, "%s" given in "%s".', $id, $call instanceof TaggedValue ? '!'.$call->getTag() : get_debug_type($call), $file));
|
||||
}
|
||||
|
||||
if (\is_string($k)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid method call for service "%s", did you forgot a leading dash before "%s: ..." in "%s"?', $id, $k, $file));
|
||||
}
|
||||
|
||||
if (isset($call['method']) && \is_string($call['method'])) {
|
||||
$method = $call['method'];
|
||||
$args = $call['arguments'] ?? [];
|
||||
$returnsClone = $call['returns_clone'] ?? false;
|
||||
} else {
|
||||
if (1 === \count($call) && \is_string(key($call))) {
|
||||
$method = key($call);
|
||||
$args = $call[$method];
|
||||
|
||||
if ($args instanceof TaggedValue) {
|
||||
if ('returns_clone' !== $args->getTag()) {
|
||||
throw new InvalidArgumentException(sprintf('Unsupported tag "!%s", did you mean "!returns_clone" for service "%s" in "%s"?', $args->getTag(), $id, $file));
|
||||
}
|
||||
|
||||
$returnsClone = true;
|
||||
$args = $args->getValue();
|
||||
} else {
|
||||
$returnsClone = false;
|
||||
}
|
||||
} elseif (empty($call[0])) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid call for service "%s": the method must be defined as the first index of an array or as the only key of a map in "%s".', $id, $file));
|
||||
} else {
|
||||
$method = $call[0];
|
||||
$args = $call[1] ?? [];
|
||||
$returnsClone = $call[2] ?? false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!\is_array($args)) {
|
||||
throw new InvalidArgumentException(sprintf('The second parameter for function call "%s" must be an array of its arguments for service "%s" in "%s". Check your YAML syntax.', $method, $id, $file));
|
||||
}
|
||||
|
||||
$args = $this->resolveServices($args, $file);
|
||||
$definition->addMethodCall($method, $args, $returnsClone);
|
||||
}
|
||||
}
|
||||
|
||||
$tags = $service['tags'] ?? [];
|
||||
if (!\is_array($tags)) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
|
||||
if (isset($defaults['tags'])) {
|
||||
$tags = array_merge($tags, $defaults['tags']);
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
if (!\is_array($tag)) {
|
||||
$tag = ['name' => $tag];
|
||||
}
|
||||
|
||||
if (1 === \count($tag) && \is_array(current($tag))) {
|
||||
$name = key($tag);
|
||||
$tag = current($tag);
|
||||
} else {
|
||||
if (!isset($tag['name'])) {
|
||||
throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".', $id, $file));
|
||||
}
|
||||
$name = $tag['name'];
|
||||
unset($tag['name']);
|
||||
}
|
||||
|
||||
if (!\is_string($name) || '' === $name) {
|
||||
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', $id, $file));
|
||||
}
|
||||
|
||||
foreach ($tag as $attribute => $value) {
|
||||
if (!\is_scalar($value) && null !== $value) {
|
||||
throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in "%s". Check your YAML syntax.', $id, $name, $attribute, $file));
|
||||
}
|
||||
}
|
||||
|
||||
$definition->addTag($name, $tag);
|
||||
}
|
||||
|
||||
if (null !== $decorates = $service['decorates'] ?? null) {
|
||||
if ('' !== $decorates && '@' === $decorates[0]) {
|
||||
throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").', $id, $service['decorates'], substr($decorates, 1)));
|
||||
}
|
||||
|
||||
$decorationOnInvalid = \array_key_exists('decoration_on_invalid', $service) ? $service['decoration_on_invalid'] : 'exception';
|
||||
if ('exception' === $decorationOnInvalid) {
|
||||
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
||||
} elseif ('ignore' === $decorationOnInvalid) {
|
||||
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
||||
} elseif (null === $decorationOnInvalid) {
|
||||
$invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
|
||||
} elseif ('null' === $decorationOnInvalid) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean null (without quotes) in "%s"?', $decorationOnInvalid, $id, $file));
|
||||
} else {
|
||||
throw new InvalidArgumentException(sprintf('Invalid value "%s" for attribute "decoration_on_invalid" on service "%s". Did you mean "exception", "ignore" or null in "%s"?', $decorationOnInvalid, $id, $file));
|
||||
}
|
||||
|
||||
$renameId = $service['decoration_inner_name'] ?? null;
|
||||
$priority = $service['decoration_priority'] ?? 0;
|
||||
|
||||
$definition->setDecoratedService($decorates, $renameId, $priority, $invalidBehavior);
|
||||
}
|
||||
|
||||
if (isset($service['autowire'])) {
|
||||
$definition->setAutowired($service['autowire']);
|
||||
}
|
||||
|
||||
if (isset($defaults['bind']) || isset($service['bind'])) {
|
||||
// deep clone, to avoid multiple process of the same instance in the passes
|
||||
$bindings = $definition->getBindings();
|
||||
$bindings += isset($defaults['bind']) ? unserialize(serialize($defaults['bind'])) : [];
|
||||
|
||||
if (isset($service['bind'])) {
|
||||
if (!\is_array($service['bind'])) {
|
||||
throw new InvalidArgumentException(sprintf('Parameter "bind" must be an array for service "%s" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
|
||||
$bindings = array_merge($bindings, $this->resolveServices($service['bind'], $file));
|
||||
$bindingType = $this->isLoadingInstanceof ? BoundArgument::INSTANCEOF_BINDING : BoundArgument::SERVICE_BINDING;
|
||||
foreach ($bindings as $argument => $value) {
|
||||
if (!$value instanceof BoundArgument) {
|
||||
$bindings[$argument] = new BoundArgument($value, $trackBindings, $bindingType, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$definition->setBindings($bindings);
|
||||
}
|
||||
|
||||
if (isset($service['autoconfigure'])) {
|
||||
$definition->setAutoconfigured($service['autoconfigure']);
|
||||
}
|
||||
|
||||
if (\array_key_exists('namespace', $service) && !\array_key_exists('resource', $service)) {
|
||||
throw new InvalidArgumentException(sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
|
||||
if ($return) {
|
||||
if (\array_key_exists('resource', $service)) {
|
||||
throw new InvalidArgumentException(sprintf('Invalid "resource" attribute found for service "%s" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
|
||||
return $definition;
|
||||
}
|
||||
|
||||
if (\array_key_exists('resource', $service)) {
|
||||
if (!\is_string($service['resource'])) {
|
||||
throw new InvalidArgumentException(sprintf('A "resource" attribute must be of type string for service "%s" in "%s". Check your YAML syntax.', $id, $file));
|
||||
}
|
||||
$exclude = $service['exclude'] ?? null;
|
||||
$namespace = $service['namespace'] ?? $id;
|
||||
$this->registerClasses($definition, $namespace, $service['resource'], $exclude);
|
||||
} else {
|
||||
$this->setDefinition($id, $definition);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a callable.
|
||||
*
|
||||
* @param string|array $callable A callable reference
|
||||
*
|
||||
* @throws InvalidArgumentException When errors occur
|
||||
*
|
||||
* @return string|array|Reference
|
||||
*/
|
||||
private function parseCallable($callable, string $parameter, string $id, string $file)
|
||||
{
|
||||
if (\is_string($callable)) {
|
||||
if ('' !== $callable && '@' === $callable[0]) {
|
||||
if (!str_contains($callable, ':')) {
|
||||
return [$this->resolveServices($callable, $file), '__invoke'];
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s" in "%s").', $parameter, $id, $callable, substr($callable, 1), $file));
|
||||
}
|
||||
|
||||
return $callable;
|
||||
}
|
||||
|
||||
if (\is_array($callable)) {
|
||||
if (isset($callable[0]) && isset($callable[1])) {
|
||||
return [$this->resolveServices($callable[0], $file), $callable[1]];
|
||||
}
|
||||
|
||||
if ('factory' === $parameter && isset($callable[1]) && null === $callable[0]) {
|
||||
return $callable;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Parameter "%s" must contain an array with two elements for service "%s" in "%s". Check your YAML syntax.', $parameter, $id, $file));
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Parameter "%s" must be a string or an array for service "%s" in "%s". Check your YAML syntax.', $parameter, $id, $file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a YAML file.
|
||||
*
|
||||
* @return array|null
|
||||
*
|
||||
* @throws InvalidArgumentException when the given file is not a local file or when it does not exist
|
||||
*/
|
||||
protected function loadFile(string $file)
|
||||
{
|
||||
if (!class_exists(\Symfony\Component\Yaml\Parser::class)) {
|
||||
throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
|
||||
}
|
||||
|
||||
if (!stream_is_local($file)) {
|
||||
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
|
||||
}
|
||||
|
||||
if (!is_file($file)) {
|
||||
throw new InvalidArgumentException(sprintf('The file "%s" does not exist.', $file));
|
||||
}
|
||||
|
||||
if (null === $this->yamlParser) {
|
||||
$this->yamlParser = new YamlParser();
|
||||
}
|
||||
|
||||
try {
|
||||
$configuration = $this->yamlParser->parseFile($file, Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS);
|
||||
} catch (ParseException $e) {
|
||||
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: ', $file).$e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $this->validate($configuration, $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a YAML file.
|
||||
*
|
||||
* @throws InvalidArgumentException When service file is not valid
|
||||
*/
|
||||
private function validate($content, string $file): ?array
|
||||
{
|
||||
if (null === $content) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
if (!\is_array($content)) {
|
||||
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
|
||||
}
|
||||
|
||||
foreach ($content as $namespace => $data) {
|
||||
if (\in_array($namespace, ['imports', 'parameters', 'services']) || 0 === strpos($namespace, 'when@')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$this->container->hasExtension($namespace)) {
|
||||
$extensionNamespaces = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
|
||||
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $file, $namespace, $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'));
|
||||
}
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
private function resolveServices($value, string $file, bool $isParameter = false)
|
||||
{
|
||||
if ($value instanceof TaggedValue) {
|
||||
$argument = $value->getValue();
|
||||
if ('iterator' === $value->getTag()) {
|
||||
if (!\is_array($argument)) {
|
||||
throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts sequences in "%s".', $file));
|
||||
}
|
||||
$argument = $this->resolveServices($argument, $file, $isParameter);
|
||||
try {
|
||||
return new IteratorArgument($argument);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts arrays of "@service" references in "%s".', $file));
|
||||
}
|
||||
}
|
||||
if ('service_closure' === $value->getTag()) {
|
||||
$argument = $this->resolveServices($argument, $file, $isParameter);
|
||||
|
||||
if (!$argument instanceof Reference) {
|
||||
throw new InvalidArgumentException(sprintf('"!service_closure" tag only accepts service references in "%s".', $file));
|
||||
}
|
||||
|
||||
return new ServiceClosureArgument($argument);
|
||||
}
|
||||
if ('service_locator' === $value->getTag()) {
|
||||
if (!\is_array($argument)) {
|
||||
throw new InvalidArgumentException(sprintf('"!service_locator" tag only accepts maps in "%s".', $file));
|
||||
}
|
||||
|
||||
$argument = $this->resolveServices($argument, $file, $isParameter);
|
||||
|
||||
try {
|
||||
return new ServiceLocatorArgument($argument);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw new InvalidArgumentException(sprintf('"!service_locator" tag only accepts maps of "@service" references in "%s".', $file));
|
||||
}
|
||||
}
|
||||
if (\in_array($value->getTag(), ['tagged', 'tagged_iterator', 'tagged_locator'], true)) {
|
||||
$forLocator = 'tagged_locator' === $value->getTag();
|
||||
|
||||
if (\is_array($argument) && isset($argument['tag']) && $argument['tag']) {
|
||||
if ($diff = array_diff(array_keys($argument), ['tag', 'index_by', 'default_index_method', 'default_priority_method'])) {
|
||||
throw new InvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "tag", "index_by", "default_index_method", and "default_priority_method".', $value->getTag(), implode('", "', $diff)));
|
||||
}
|
||||
|
||||
$argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator, $argument['default_priority_method'] ?? null);
|
||||
} elseif (\is_string($argument) && $argument) {
|
||||
$argument = new TaggedIteratorArgument($argument, null, null, $forLocator);
|
||||
} else {
|
||||
throw new InvalidArgumentException(sprintf('"!%s" tags only accept a non empty string or an array with a key "tag" in "%s".', $value->getTag(), $file));
|
||||
}
|
||||
|
||||
if ($forLocator) {
|
||||
$argument = new ServiceLocatorArgument($argument);
|
||||
}
|
||||
|
||||
return $argument;
|
||||
}
|
||||
if ('service' === $value->getTag()) {
|
||||
if ($isParameter) {
|
||||
throw new InvalidArgumentException(sprintf('Using an anonymous service in a parameter is not allowed in "%s".', $file));
|
||||
}
|
||||
|
||||
$isLoadingInstanceof = $this->isLoadingInstanceof;
|
||||
$this->isLoadingInstanceof = false;
|
||||
$instanceof = $this->instanceof;
|
||||
$this->instanceof = [];
|
||||
|
||||
$id = sprintf('.%d_%s', ++$this->anonymousServicesCount, preg_replace('/^.*\\\\/', '', $argument['class'] ?? '').$this->anonymousServicesSuffix);
|
||||
$this->parseDefinition($id, $argument, $file, []);
|
||||
|
||||
if (!$this->container->hasDefinition($id)) {
|
||||
throw new InvalidArgumentException(sprintf('Creating an alias using the tag "!service" is not allowed in "%s".', $file));
|
||||
}
|
||||
|
||||
$this->container->getDefinition($id);
|
||||
|
||||
$this->isLoadingInstanceof = $isLoadingInstanceof;
|
||||
$this->instanceof = $instanceof;
|
||||
|
||||
return new Reference($id);
|
||||
}
|
||||
if ('abstract' === $value->getTag()) {
|
||||
return new AbstractArgument($value->getValue());
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".', $value->getTag()));
|
||||
}
|
||||
|
||||
if (\is_array($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
$value[$k] = $this->resolveServices($v, $file, $isParameter);
|
||||
}
|
||||
} elseif (\is_string($value) && str_starts_with($value, '@=')) {
|
||||
if ($isParameter) {
|
||||
throw new InvalidArgumentException(sprintf('Using expressions in parameters is not allowed in "%s".', $file));
|
||||
}
|
||||
|
||||
if (!class_exists(Expression::class)) {
|
||||
throw new \LogicException('The "@=" expression syntax cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".');
|
||||
}
|
||||
|
||||
return new Expression(substr($value, 2));
|
||||
} elseif (\is_string($value) && str_starts_with($value, '@')) {
|
||||
if (str_starts_with($value, '@@')) {
|
||||
$value = substr($value, 1);
|
||||
$invalidBehavior = null;
|
||||
} elseif (str_starts_with($value, '@!')) {
|
||||
$value = substr($value, 2);
|
||||
$invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE;
|
||||
} elseif (str_starts_with($value, '@?')) {
|
||||
$value = substr($value, 2);
|
||||
$invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
||||
} else {
|
||||
$value = substr($value, 1);
|
||||
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
||||
}
|
||||
|
||||
if (null !== $invalidBehavior) {
|
||||
$value = new Reference($value, $invalidBehavior);
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function loadFromExtensions(array $content)
|
||||
{
|
||||
foreach ($content as $namespace => $values) {
|
||||
if (\in_array($namespace, ['imports', 'parameters', 'services']) || 0 === strpos($namespace, 'when@')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!\is_array($values) && null !== $values) {
|
||||
$values = [];
|
||||
}
|
||||
|
||||
$this->container->loadFromExtension($namespace, $values);
|
||||
}
|
||||
}
|
||||
|
||||
private function checkDefinition(string $id, array $definition, string $file)
|
||||
{
|
||||
if ($this->isLoadingInstanceof) {
|
||||
$keywords = self::INSTANCEOF_KEYWORDS;
|
||||
} elseif (isset($definition['resource']) || isset($definition['namespace'])) {
|
||||
$keywords = self::PROTOTYPE_KEYWORDS;
|
||||
} else {
|
||||
$keywords = self::SERVICE_KEYWORDS;
|
||||
}
|
||||
|
||||
foreach ($definition as $key => $value) {
|
||||
if (!isset($keywords[$key])) {
|
||||
throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for definition "%s" in "%s". Allowed configuration keys are "%s".', $key, $id, $file, implode('", "', $keywords)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue