This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/ContainerBuilder.php

1621 lines
53 KiB
PHP
Raw Normal View History

2010-01-04 14:26:20 +00:00
<?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;
2010-01-04 14:26:20 +00:00
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\Config\Resource\ComposerResource;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Config\Resource\FileExistenceResource;
2018-07-26 10:03:18 +01:00
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\GlobResource;
use Symfony\Component\Config\Resource\ReflectionClassResource;
2018-07-26 10:03:18 +01:00
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocator;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
2010-12-14 23:25:33 +00:00
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2013-03-29 23:21:12 +00:00
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
2010-01-04 14:26:20 +00:00
/**
* ContainerBuilder is a DI container that provides an API to easily describe services.
2010-01-04 14:26:20 +00:00
*
* @author Fabien Potencier <fabien@symfony.com>
2010-01-04 14:26:20 +00:00
*/
2010-08-05 06:34:53 +01:00
class ContainerBuilder extends Container implements TaggedContainerInterface
2010-01-04 14:26:20 +00:00
{
/**
* @var ExtensionInterface[]
*/
2019-01-16 20:35:37 +00:00
private $extensions = [];
/**
* @var ExtensionInterface[]
*/
2019-01-16 20:35:37 +00:00
private $extensionsByNs = [];
/**
* @var Definition[]
*/
2019-01-16 20:35:37 +00:00
private $definitions = [];
/**
* @var Alias[]
*/
2019-01-16 20:35:37 +00:00
private $aliasDefinitions = [];
/**
* @var ResourceInterface[]
*/
2019-01-16 20:35:37 +00:00
private $resources = [];
2019-01-16 20:35:37 +00:00
private $extensionConfigs = [];
/**
* @var Compiler
*/
private $compiler;
2010-12-14 23:25:33 +00:00
private $trackResources;
2013-03-29 23:21:12 +00:00
/**
* @var InstantiatorInterface|null
*/
private $proxyInstantiator;
/**
* @var ExpressionLanguage|null
*/
private $expressionLanguage;
/**
* @var ExpressionFunctionProviderInterface[]
*/
2019-01-16 20:35:37 +00:00
private $expressionLanguageProviders = [];
/**
2015-09-28 12:00:47 +01:00
* @var string[] with tag names used by findTaggedServiceIds
*/
2019-01-16 20:35:37 +00:00
private $usedTags = [];
/**
* @var string[][] a map of env var names to their placeholders
*/
2019-01-16 20:35:37 +00:00
private $envPlaceholders = [];
/**
* @var int[] a map of env vars to their resolution counter
*/
2019-01-16 20:35:37 +00:00
private $envCounters = [];
/**
* @var string[] the list of vendor directories
*/
private $vendors;
2019-01-16 20:35:37 +00:00
private $autoconfiguredInstanceof = [];
2019-01-16 20:35:37 +00:00
private $removedIds = [];
private $removedBindingIds = [];
2019-01-16 20:35:37 +00:00
private static $internalTypes = [
'int' => true,
'float' => true,
'string' => true,
'bool' => true,
'resource' => true,
'object' => true,
'array' => true,
'null' => true,
'callable' => true,
'iterable' => true,
'mixed' => true,
2019-01-16 20:35:37 +00:00
];
public function __construct(ParameterBagInterface $parameterBag = null)
{
parent::__construct($parameterBag);
$this->trackResources = interface_exists('Symfony\Component\Config\Resource\ResourceInterface');
$this->setDefinition('service_container', (new Definition(ContainerInterface::class))->setSynthetic(true)->setPublic(true));
$this->setAlias(PsrContainerInterface::class, new Alias('service_container', false));
$this->setAlias(ContainerInterface::class, new Alias('service_container', false));
}
/**
* @var \ReflectionClass[] a list of class reflectors
*/
private $classReflectors;
/**
* Sets the track resources flag.
*
* If you are not using the loaders and therefore don't want
* to depend on the Config component, set this flag to false.
*/
public function setResourceTracking(bool $track)
{
$this->trackResources = $track;
}
/**
* Checks if resources are tracked.
*
2017-10-28 19:51:46 +01:00
* @return bool true If resources are tracked, false otherwise
*/
public function isTrackingResources()
{
return $this->trackResources;
}
2013-03-29 23:21:12 +00:00
/**
* Sets the instantiator to be used when fetching proxies.
*/
public function setProxyInstantiator(InstantiatorInterface $proxyInstantiator)
{
$this->proxyInstantiator = $proxyInstantiator;
}
public function registerExtension(ExtensionInterface $extension)
{
$this->extensions[$extension->getAlias()] = $extension;
if (false !== $extension->getNamespace()) {
$this->extensionsByNs[$extension->getNamespace()] = $extension;
}
}
/**
* Returns an extension by alias or namespace.
*
* @return ExtensionInterface An extension instance
*
* @throws LogicException if the extension is not registered
*/
public function getExtension(string $name)
{
if (isset($this->extensions[$name])) {
return $this->extensions[$name];
}
if (isset($this->extensionsByNs[$name])) {
return $this->extensionsByNs[$name];
}
throw new LogicException(sprintf('Container extension "%s" is not registered', $name));
}
/**
* Returns all registered extensions.
*
* @return ExtensionInterface[] An array of ExtensionInterface
*/
public function getExtensions()
{
return $this->extensions;
}
2011-02-13 18:06:41 +00:00
/**
* Checks if we have an extension.
*
2014-11-30 13:33:44 +00:00
* @return bool If the extension exists
2011-02-13 18:06:41 +00:00
*/
public function hasExtension(string $name)
{
return isset($this->extensions[$name]) || isset($this->extensionsByNs[$name]);
}
/**
* Returns an array of resources loaded to build this configuration.
*
* @return ResourceInterface[] An array of resources
*/
public function getResources()
{
return array_values($this->resources);
}
/**
* @return $this
*/
public function addResource(ResourceInterface $resource)
{
if (!$this->trackResources) {
return $this;
}
if ($resource instanceof GlobResource && $this->inVendors($resource->getPrefix())) {
return $this;
}
$this->resources[(string) $resource] = $resource;
return $this;
}
/**
* Sets the resources for this configuration.
2012-12-11 10:49:22 +00:00
*
* @param ResourceInterface[] $resources An array of resources
2012-12-11 10:49:22 +00:00
*
* @return $this
*/
2011-09-16 12:43:09 +01:00
public function setResources(array $resources)
{
if (!$this->trackResources) {
return $this;
}
2011-09-16 12:43:09 +01:00
$this->resources = $resources;
return $this;
}
/**
* Adds the object class hierarchy as resources.
*
* @param object|string $object An object instance or class name
*
* @return $this
*/
public function addObjectResource($object)
2013-03-29 23:21:12 +00:00
{
if ($this->trackResources) {
if (\is_object($object)) {
$object = \get_class($object);
}
if (!isset($this->classReflectors[$object])) {
$this->classReflectors[$object] = new \ReflectionClass($object);
}
$class = $this->classReflectors[$object];
foreach ($class->getInterfaceNames() as $name) {
if (null === $interface = &$this->classReflectors[$name]) {
$interface = new \ReflectionClass($name);
}
$file = $interface->getFileName();
if (false !== $file && file_exists($file)) {
$this->fileExists($file);
}
}
do {
$file = $class->getFileName();
if (false !== $file && file_exists($file)) {
$this->fileExists($file);
}
foreach ($class->getTraitNames() as $name) {
$this->addObjectResource($name);
}
} while ($class = $class->getParentClass());
2013-03-29 23:21:12 +00:00
}
return $this;
}
/**
* Retrieves the requested reflection class and registers it for resource tracking.
*
* @throws \ReflectionException when a parent class/interface/trait is not found and $throw is true
*
* @final
*/
public function getReflectionClass(?string $class, bool $throw = true): ?\ReflectionClass
{
if (!$class = $this->getParameterBag()->resolveValue($class)) {
return null;
}
if (isset(self::$internalTypes[$class])) {
return null;
}
$resource = null;
try {
if (isset($this->classReflectors[$class])) {
$classReflector = $this->classReflectors[$class];
} elseif (class_exists(ClassExistenceResource::class)) {
$resource = new ClassExistenceResource($class, false);
$classReflector = $resource->isFresh(0) ? false : new \ReflectionClass($class);
} else {
$classReflector = class_exists($class) ? new \ReflectionClass($class) : false;
}
} catch (\ReflectionException $e) {
if ($throw) {
throw $e;
}
$classReflector = false;
}
if ($this->trackResources) {
if (!$classReflector) {
$this->addResource($resource ?: new ClassExistenceResource($class, false));
} elseif (!$classReflector->isInternal()) {
$path = $classReflector->getFileName();
if (!$this->inVendors($path)) {
$this->addResource(new ReflectionClassResource($classReflector, $this->vendors));
}
}
$this->classReflectors[$class] = $classReflector;
}
return $classReflector ?: null;
}
/**
* Checks whether the requested file or directory exists and registers the result for resource tracking.
*
* @param string $path The file or directory path for which to check the existence
* @param bool|string $trackContents Whether to track contents of the given resource. If a string is passed,
* it will be used as pattern for tracking contents of the requested directory
*
* @final
*/
public function fileExists(string $path, $trackContents = true): bool
{
$exists = file_exists($path);
if (!$this->trackResources || $this->inVendors($path)) {
return $exists;
}
if (!$exists) {
$this->addResource(new FileExistenceResource($path));
return $exists;
}
if (is_dir($path)) {
if ($trackContents) {
$this->addResource(new DirectoryResource($path, \is_string($trackContents) ? $trackContents : null));
} else {
$this->addResource(new GlobResource($path, '/*', false));
}
} elseif ($trackContents) {
$this->addResource(new FileResource($path));
}
return $exists;
}
/**
* Loads the configuration for an extension.
*
2010-07-20 15:40:57 +01:00
* @param string $extension The extension alias or namespace
* @param array $values An array of values that customizes the extension
*
* @return $this
*
* @throws BadMethodCallException When this ContainerBuilder is compiled
* @throws \LogicException if the extension is not registered
*/
public function loadFromExtension(string $extension, array $values = null)
{
if ($this->isCompiled()) {
throw new BadMethodCallException('Cannot load from an extension on a compiled container.');
}
if (\func_num_args() < 2) {
2019-01-16 20:35:37 +00:00
$values = [];
}
$namespace = $this->getExtension($extension)->getAlias();
$this->extensionConfigs[$namespace][] = $values;
return $this;
}
2010-12-14 23:25:33 +00:00
/**
* Adds a compiler pass.
2010-12-14 23:25:33 +00:00
*
2019-08-07 16:24:11 +01:00
* @param string $type The type of compiler pass
* @param int $priority Used to sort the passes
*
* @return $this
2010-12-14 23:25:33 +00:00
*/
public function addCompilerPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
2010-12-14 23:25:33 +00:00
{
$this->getCompiler()->addPass($pass, $type, $priority);
$this->addObjectResource($pass);
return $this;
2010-12-14 23:25:33 +00:00
}
/**
* Returns the compiler pass config which can then be modified.
2010-12-14 23:25:33 +00:00
*
* @return PassConfig The compiler pass config
2010-12-14 23:25:33 +00:00
*/
2010-12-29 19:12:24 +00:00
public function getCompilerPassConfig()
2010-12-14 23:25:33 +00:00
{
return $this->getCompiler()->getPassConfig();
}
/**
* Returns the compiler.
*
* @return Compiler The compiler
*/
public function getCompiler()
{
if (null === $this->compiler) {
$this->compiler = new Compiler();
}
return $this->compiler;
2010-12-14 23:25:33 +00:00
}
/**
* Sets a service.
*
* @param object $service The service instance
*
* @throws BadMethodCallException When this ContainerBuilder is compiled
*/
public function set(string $id, $service)
2010-01-04 14:26:20 +00:00
{
if ($this->isCompiled() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
2017-03-22 21:08:21 +00:00
// setting a synthetic service on a compiled container is alright
throw new BadMethodCallException(sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a compiled container is not allowed.', $id));
}
unset($this->definitions[$id], $this->aliasDefinitions[$id], $this->removedIds[$id]);
2015-09-04 20:54:37 +01:00
parent::set($id, $service);
2010-01-04 14:26:20 +00:00
}
/**
* Removes a service definition.
*/
public function removeDefinition(string $id)
{
if (isset($this->definitions[$id])) {
unset($this->definitions[$id]);
$this->removedIds[$id] = true;
}
}
/**
* Returns true if the given service is defined.
*
2012-05-15 21:19:31 +01:00
* @param string $id The service identifier
*
2014-11-30 13:33:44 +00:00
* @return bool true if the service is defined, false otherwise
*/
2010-06-27 17:28:29 +01:00
public function has($id)
{
$id = (string) $id;
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || parent::has($id);
}
/**
* Gets a service.
*
2014-11-30 13:33:44 +00:00
* @param string $id The service identifier
* @param int $invalidBehavior The behavior when the service does not exist
*
* @return object The associated service
*
* @throws InvalidArgumentException when no definitions are available
* @throws ServiceCircularReferenceException When a circular reference is detected
* @throws ServiceNotFoundException When the service is not defined
2013-03-29 23:21:12 +00:00
* @throws \Exception
*
* @see Reference
*/
public function get($id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
2010-01-04 14:26:20 +00:00
{
if ($this->isCompiled() && isset($this->removedIds[$id = (string) $id]) && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior) {
return parent::get($id);
2017-09-19 21:53:21 +01:00
}
return $this->doGet($id, $invalidBehavior);
}
private function doGet(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = null, bool $isConstructorArgument = false)
2010-01-04 14:26:20 +00:00
{
if (isset($inlineServices[$id])) {
return $inlineServices[$id];
}
if (null === $inlineServices) {
$isConstructorArgument = true;
2019-01-16 20:35:37 +00:00
$inlineServices = [];
}
try {
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
return parent::get($id, $invalidBehavior);
}
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
return $service;
}
} catch (ServiceCircularReferenceException $e) {
if ($isConstructorArgument) {
throw $e;
}
}
2014-03-27 18:14:17 +00:00
2016-08-21 13:01:27 +01:00
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
$alias = $this->aliasDefinitions[$id];
if ($alias->isDeprecated()) {
@trigger_error($alias->getDeprecationMessage($id), E_USER_DEPRECATED);
2017-10-27 14:58:53 +01:00
}
return $this->doGet((string) $alias, $invalidBehavior, $inlineServices, $isConstructorArgument);
}
try {
$definition = $this->getDefinition($id);
} catch (ServiceNotFoundException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE < $invalidBehavior) {
2014-04-16 08:15:58 +01:00
return;
}
throw $e;
}
if ($definition->hasErrors() && $e = $definition->getErrors()) {
throw new RuntimeException(reset($e));
}
if ($isConstructorArgument) {
$this->loading[$id] = true;
}
try {
return $this->createService($definition, $inlineServices, $isConstructorArgument, $id);
2015-09-28 01:26:11 +01:00
} finally {
if ($isConstructorArgument) {
unset($this->loading[$id]);
}
}
2010-01-04 14:26:20 +00:00
}
/**
* Merges a ContainerBuilder with the current ContainerBuilder configuration.
*
* Service definitions overrides the current defined ones.
*
* But for parameters, they are overridden by the current ones. It allows
* the parameters passed to the container constructor to have precedence
* over the loaded ones.
*
2019-01-16 20:41:24 +00:00
* $container = new ContainerBuilder(new ParameterBag(['foo' => 'bar']));
2018-08-28 22:39:52 +01:00
* $loader = new LoaderXXX($container);
* $loader->load('resource_name');
* $container->register('foo', 'stdClass');
*
* In the above example, even if the loaded resource defines a foo
* parameter, the value will still be 'bar' as defined in the ContainerBuilder
* constructor.
2011-02-13 18:06:41 +00:00
*
* @throws BadMethodCallException When this ContainerBuilder is compiled
*/
public function merge(self $container)
2010-01-04 14:26:20 +00:00
{
if ($this->isCompiled()) {
throw new BadMethodCallException('Cannot merge on a compiled container.');
}
$this->addDefinitions($container->getDefinitions());
$this->addAliases($container->getAliases());
$this->getParameterBag()->add($container->getParameterBag()->all());
if ($this->trackResources) {
foreach ($container->getResources() as $resource) {
$this->addResource($resource);
}
}
foreach ($this->extensions as $name => $extension) {
if (!isset($this->extensionConfigs[$name])) {
2019-01-16 20:35:37 +00:00
$this->extensionConfigs[$name] = [];
}
$this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name));
}
if ($this->getParameterBag() instanceof EnvPlaceholderParameterBag && $container->getParameterBag() instanceof EnvPlaceholderParameterBag) {
$envPlaceholders = $container->getParameterBag()->getEnvPlaceholders();
$this->getParameterBag()->mergeEnvPlaceholders($container->getParameterBag());
} else {
2019-01-16 20:35:37 +00:00
$envPlaceholders = [];
}
foreach ($container->envCounters as $env => $count) {
if (!$count && !isset($envPlaceholders[$env])) {
continue;
}
if (!isset($this->envCounters[$env])) {
$this->envCounters[$env] = $count;
} else {
$this->envCounters[$env] += $count;
}
}
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
2017-04-27 16:48:07 +01:00
foreach ($container->getAutoconfiguredInstanceof() as $interface => $childDefinition) {
if (isset($this->autoconfiguredInstanceof[$interface])) {
2017-04-20 18:21:47 +01:00
throw new InvalidArgumentException(sprintf('"%s" has already been autoconfigured and merge() does not support merging autoconfiguration for the same class/interface.', $interface));
}
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
2017-04-27 16:48:07 +01:00
$this->autoconfiguredInstanceof[$interface] = $childDefinition;
}
}
/**
* Returns the configuration array for the given extension.
*
* @return array An array of configuration
*/
public function getExtensionConfig(string $name)
{
if (!isset($this->extensionConfigs[$name])) {
2019-01-16 20:35:37 +00:00
$this->extensionConfigs[$name] = [];
}
return $this->extensionConfigs[$name];
}
/**
* Prepends a config array to the configs of the given extension.
*/
public function prependExtensionConfig(string $name, array $config)
{
if (!isset($this->extensionConfigs[$name])) {
2019-01-16 20:35:37 +00:00
$this->extensionConfigs[$name] = [];
}
array_unshift($this->extensionConfigs[$name], $config);
}
/**
* Compiles the container.
*
* This method passes the container to compiler
* passes whose job is to manipulate and optimize
* the container.
*
* The main compiler passes roughly do four things:
*
* * The extension configurations are merged;
* * Parameter values are resolved;
* * The parameter bag is frozen;
* * Extension loading is disabled.
*
* @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current
* env vars or be replaced by uniquely identifiable placeholders.
* Set to "true" when you want to use the current ContainerBuilder
* directly, keep to "false" when the container is dumped instead.
*/
2017-06-20 19:51:06 +01:00
public function compile(bool $resolveEnvPlaceholders = false)
{
$compiler = $this->getCompiler();
if ($this->trackResources) {
foreach ($compiler->getPassConfig()->getPasses() as $pass) {
$this->addObjectResource($pass);
}
2013-11-09 11:47:52 +00:00
}
$bag = $this->getParameterBag();
if ($resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag) {
$compiler->addPass(new ResolveEnvPlaceholdersPass(), PassConfig::TYPE_AFTER_REMOVING, -1000);
}
2013-11-09 11:47:52 +00:00
$compiler->compile($this);
2013-03-29 23:21:12 +00:00
foreach ($this->definitions as $id => $definition) {
if ($this->trackResources && $definition->isLazy()) {
$this->getReflectionClass($definition->getClass());
2013-03-29 23:21:12 +00:00
}
}
2019-01-16 20:35:37 +00:00
$this->extensionConfigs = [];
if ($bag instanceof EnvPlaceholderParameterBag) {
if ($resolveEnvPlaceholders) {
$this->parameterBag = new ParameterBag($this->resolveEnvPlaceholders($bag->all(), true));
}
$this->envPlaceholders = $bag->getEnvPlaceholders();
}
parent::compile();
foreach ($this->definitions + $this->aliasDefinitions as $id => $definition) {
if (!$definition->isPublic() || $definition->isPrivate()) {
$this->removedIds[$id] = true;
}
}
2010-01-04 14:26:20 +00:00
}
/**
* {@inheritdoc}
*/
public function getServiceIds()
2010-01-04 14:26:20 +00:00
{
return array_map('strval', array_unique(array_merge(array_keys($this->getDefinitions()), array_keys($this->aliasDefinitions), parent::getServiceIds())));
2010-01-04 14:26:20 +00:00
}
/**
* Gets removed service or alias ids.
*
* @return array
*/
public function getRemovedIds()
{
return $this->removedIds;
}
/**
* Adds the service aliases.
*/
public function addAliases(array $aliases)
{
foreach ($aliases as $alias => $id) {
$this->setAlias($alias, $id);
}
}
/**
* Sets the service aliases.
*/
public function setAliases(array $aliases)
{
2019-01-16 20:35:37 +00:00
$this->aliasDefinitions = [];
$this->addAliases($aliases);
}
/**
* Sets an alias for an existing service.
*
2014-11-30 13:33:44 +00:00
* @param string $alias The alias to create
* @param string|Alias $id The service to alias
*
* @return Alias
*
* @throws InvalidArgumentException if the id is not a string or an Alias
* @throws InvalidArgumentException if the alias is for itself
*/
public function setAlias(string $alias, $id)
2010-01-04 14:26:20 +00:00
{
if ('' === $alias || '\\' === $alias[-1] || \strlen($alias) !== strcspn($alias, "\0\r\n'")) {
2019-04-02 18:55:56 +01:00
throw new InvalidArgumentException(sprintf('Invalid alias id: "%s"', $alias));
}
if (\is_string($id)) {
$id = new Alias($id);
2011-12-18 13:42:59 +00:00
} elseif (!$id instanceof Alias) {
throw new InvalidArgumentException('$id must be a string, or an Alias object.');
2011-01-07 14:44:29 +00:00
}
if ($alias === (string) $id) {
2013-04-10 11:24:37 +01:00
throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias));
}
unset($this->definitions[$alias], $this->removedIds[$alias]);
return $this->aliasDefinitions[$alias] = $id;
2010-01-04 14:26:20 +00:00
}
/**
* Removes an alias.
*
* @param string $alias The alias to remove
*/
public function removeAlias(string $alias)
{
if (isset($this->aliasDefinitions[$alias])) {
unset($this->aliasDefinitions[$alias]);
$this->removedIds[$alias] = true;
}
}
/**
* Returns true if an alias exists under the given identifier.
*
2014-11-30 13:33:44 +00:00
* @return bool true if the alias exists, false otherwise
*/
public function hasAlias(string $id)
2010-01-04 14:26:20 +00:00
{
return isset($this->aliasDefinitions[$id]);
2010-01-04 14:26:20 +00:00
}
/**
* Gets all defined aliases.
*
* @return Alias[] An array of aliases
*/
public function getAliases()
2010-01-04 14:26:20 +00:00
{
return $this->aliasDefinitions;
2010-01-04 14:26:20 +00:00
}
/**
* Gets an alias.
*
2012-08-03 10:22:09 +01:00
* @return Alias An Alias instance
*
* @throws InvalidArgumentException if the alias does not exist
*/
public function getAlias(string $id)
{
if (!isset($this->aliasDefinitions[$id])) {
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
}
2010-01-04 14:26:20 +00:00
return $this->aliasDefinitions[$id];
}
2010-01-04 14:26:20 +00:00
/**
* Registers a service definition.
*
* This methods allows for simple registration of service definition
* with a fluid interface.
*
* @return Definition A Definition instance
*/
public function register(string $id, string $class = null)
2010-01-04 14:26:20 +00:00
{
2015-04-04 18:31:25 +01:00
return $this->setDefinition($id, new Definition($class));
2010-01-04 14:26:20 +00:00
}
/**
* Registers an autowired service definition.
*
* This method implements a shortcut for using setDefinition() with
* an autowired definition.
*
* @return Definition The created definition
*/
public function autowire(string $id, string $class = null)
{
return $this->setDefinition($id, (new Definition($class))->setAutowired(true));
}
/**
* Adds the service definitions.
*
* @param Definition[] $definitions An array of service definitions
*/
public function addDefinitions(array $definitions)
2010-01-04 14:26:20 +00:00
{
foreach ($definitions as $id => $definition) {
$this->setDefinition($id, $definition);
}
2010-01-04 14:26:20 +00:00
}
/**
* Sets the service definitions.
*
* @param Definition[] $definitions An array of service definitions
*/
public function setDefinitions(array $definitions)
{
2019-01-16 20:35:37 +00:00
$this->definitions = [];
$this->addDefinitions($definitions);
}
/**
* Gets all service definitions.
*
* @return Definition[] An array of Definition instances
*/
public function getDefinitions()
2010-01-04 14:26:20 +00:00
{
return $this->definitions;
}
2010-01-04 14:26:20 +00:00
/**
* Sets a service definition.
*
* @return Definition the service definition
*
* @throws BadMethodCallException When this ContainerBuilder is compiled
*/
public function setDefinition(string $id, Definition $definition)
{
if ($this->isCompiled()) {
throw new BadMethodCallException('Adding definition to a compiled container is not allowed');
}
if ('' === $id || '\\' === $id[-1] || \strlen($id) !== strcspn($id, "\0\r\n'")) {
2019-04-02 18:55:56 +01:00
throw new InvalidArgumentException(sprintf('Invalid service id: "%s"', $id));
}
unset($this->aliasDefinitions[$id], $this->removedIds[$id]);
2010-01-04 14:26:20 +00:00
return $this->definitions[$id] = $definition;
2010-01-04 14:26:20 +00:00
}
/**
* Returns true if a service definition exists under the given identifier.
*
2014-11-30 13:33:44 +00:00
* @return bool true if the service definition exists, false otherwise
*/
public function hasDefinition(string $id)
2010-01-04 14:26:20 +00:00
{
return isset($this->definitions[$id]);
2010-01-04 14:26:20 +00:00
}
/**
* Gets a service definition.
*
* @return Definition A Definition instance
*
* @throws ServiceNotFoundException if the service definition does not exist
*/
public function getDefinition(string $id)
2010-01-04 14:26:20 +00:00
{
2016-08-21 13:01:27 +01:00
if (!isset($this->definitions[$id])) {
throw new ServiceNotFoundException($id);
}
2010-01-04 14:26:20 +00:00
return $this->definitions[$id];
2010-01-04 14:26:20 +00:00
}
/**
* Gets a service definition by id or alias.
*
* The method "unaliases" recursively to return a Definition instance.
*
* @return Definition A Definition instance
*
* @throws ServiceNotFoundException if the service definition does not exist
*/
public function findDefinition(string $id)
{
2019-01-16 20:35:37 +00:00
$seen = [];
while (isset($this->aliasDefinitions[$id])) {
$id = (string) $this->aliasDefinitions[$id];
if (isset($seen[$id])) {
2017-12-07 17:06:18 +00:00
$seen = array_values($seen);
$seen = \array_slice($seen, array_search($id, $seen));
2017-12-07 17:06:18 +00:00
$seen[] = $id;
throw new ServiceCircularReferenceException($id, $seen);
}
2017-12-07 17:06:18 +00:00
$seen[$id] = $id;
}
return $this->getDefinition($id);
}
/**
* Creates a service for a service definition.
*
* @return object The service described by the service definition
*
2014-11-30 13:33:44 +00:00
* @throws RuntimeException When the factory definition is incomplete
* @throws RuntimeException When the service is a synthetic service
* @throws InvalidArgumentException When configure callable is not callable
*/
private function createService(Definition $definition, array &$inlineServices, bool $isConstructorArgument = false, string $id = null, bool $tryProxy = true)
2010-01-04 14:26:20 +00:00
{
if (null === $id && isset($inlineServices[$h = spl_object_hash($definition)])) {
return $inlineServices[$h];
}
if ($definition instanceof ChildDefinition) {
throw new RuntimeException(sprintf('Constructing service "%s" from a parent definition is not supported at build time.', $id));
}
if ($definition->isSynthetic()) {
throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id));
}
if ($definition->isDeprecated()) {
@trigger_error($definition->getDeprecationMessage($id), E_USER_DEPRECATED);
}
if ($tryProxy && $definition->isLazy() && !$tryProxy = !($proxy = $this->proxyInstantiator) || $proxy instanceof RealServiceInstantiator) {
$proxy = $proxy->instantiateProxy(
$this,
$definition,
$id, function () use ($definition, &$inlineServices, $id) {
return $this->createService($definition, $inlineServices, true, $id, false);
}
);
$this->shareService($definition, $proxy, $id, $inlineServices);
2013-03-29 23:21:12 +00:00
return $proxy;
}
$parameterBag = $this->getParameterBag();
if (null !== $definition->getFile()) {
require_once $parameterBag->resolveValue($definition->getFile());
2010-01-04 14:26:20 +00:00
}
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())), $inlineServices, $isConstructorArgument);
if (null !== $factory = $definition->getFactory()) {
if (\is_array($factory)) {
2019-01-16 20:35:37 +00:00
$factory = [$this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlineServices, $isConstructorArgument), $factory[1]];
} elseif (!\is_string($factory)) {
throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
}
}
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
return $this->services[$id];
}
if (null !== $factory) {
$service = $factory(...$arguments);
if (!$definition->isDeprecated() && \is_array($factory) && \is_string($factory[0])) {
$r = new \ReflectionClass($factory[0]);
if (0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
@trigger_error(sprintf('The "%s" service relies on the deprecated "%s" factory class. It should either be deprecated or its factory upgraded.', $id, $r->name), E_USER_DEPRECATED);
}
}
} else {
2018-10-30 16:39:32 +00:00
$r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
$service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
@trigger_error(sprintf('The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name), E_USER_DEPRECATED);
}
}
2010-01-04 14:26:20 +00:00
$lastWitherIndex = null;
foreach ($definition->getMethodCalls() as $k => $call) {
if ($call[2] ?? false) {
$lastWitherIndex = $k;
}
}
if (null === $lastWitherIndex && ($tryProxy || !$definition->isLazy())) {
// share only if proxying failed, or if not a proxy, and if no withers are found
$this->shareService($definition, $service, $id, $inlineServices);
}
2010-01-04 14:26:20 +00:00
$properties = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())), $inlineServices);
foreach ($properties as $name => $value) {
$service->$name = $value;
}
foreach ($definition->getMethodCalls() as $k => $call) {
$service = $this->callMethod($service, $call, $inlineServices);
if ($lastWitherIndex === $k && ($tryProxy || !$definition->isLazy())) {
// share only if proxying failed, or if not a proxy, and this is the last wither
$this->shareService($definition, $service, $id, $inlineServices);
}
}
if ($callable = $definition->getConfigurator()) {
if (\is_array($callable)) {
$callable[0] = $parameterBag->resolveValue($callable[0]);
if ($callable[0] instanceof Reference) {
$callable[0] = $this->doGet((string) $callable[0], $callable[0]->getInvalidBehavior(), $inlineServices);
} elseif ($callable[0] instanceof Definition) {
$callable[0] = $this->createService($callable[0], $inlineServices);
}
}
if (!\is_callable($callable)) {
throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', \get_class($service)));
}
$callable($service);
}
return $service;
2010-01-04 14:26:20 +00:00
}
/**
* Replaces service references by the real service instance and evaluates expressions.
*
2012-05-15 21:19:31 +01:00
* @param mixed $value A value
*
* @return mixed The same value with all service references replaced by
* the real service instances and all expressions evaluated
*/
public function resolveServices($value)
{
return $this->doResolveServices($value);
}
private function doResolveServices($value, array &$inlineServices = [], bool $isConstructorArgument = false)
2010-01-04 14:26:20 +00:00
{
if (\is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->doResolveServices($v, $inlineServices, $isConstructorArgument);
}
} elseif ($value instanceof ServiceClosureArgument) {
$reference = $value->getValues()[0];
$value = function () use ($reference) {
return $this->resolveServices($reference);
};
} elseif ($value instanceof IteratorArgument) {
$value = new RewindableGenerator(function () use ($value) {
foreach ($value->getValues() as $k => $v) {
foreach (self::getServiceConditionals($v) as $s) {
if (!$this->has($s)) {
continue 2;
}
}
foreach (self::getInitializedConditionals($v) as $s) {
2017-09-19 21:53:21 +01:00
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
continue 2;
}
}
yield $k => $this->resolveServices($v);
}
}, function () use ($value) {
$count = 0;
foreach ($value->getValues() as $v) {
foreach (self::getServiceConditionals($v) as $s) {
if (!$this->has($s)) {
continue 2;
}
}
foreach (self::getInitializedConditionals($v) as $s) {
2017-09-19 21:53:21 +01:00
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE)) {
continue 2;
}
}
++$count;
}
return $count;
});
} elseif ($value instanceof ServiceLocatorArgument) {
$refs = $types = [];
foreach ($value->getValues() as $k => $v) {
if ($v) {
2019-01-16 20:35:37 +00:00
$refs[$k] = [$v];
$types[$k] = $v instanceof TypedReference ? $v->getType() : '?';
}
}
$value = new ServiceLocator(\Closure::fromCallable([$this, 'resolveServices']), $refs, $types);
} elseif ($value instanceof Reference) {
$value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices, $isConstructorArgument);
} elseif ($value instanceof Definition) {
$value = $this->createService($value, $inlineServices, $isConstructorArgument);
} elseif ($value instanceof Parameter) {
$value = $this->getParameter((string) $value);
} elseif ($value instanceof Expression) {
2019-01-16 20:35:37 +00:00
$value = $this->getExpressionLanguage()->evaluate($value, ['container' => $this]);
}
return $value;
2010-01-04 14:26:20 +00:00
}
/**
2010-08-05 06:34:53 +01:00
* Returns service ids for a given tag.
*
* Example:
*
2019-01-16 20:41:24 +00:00
* $container->register('foo')->addTag('my.tag', ['hello' => 'world']);
*
2018-08-28 22:39:52 +01:00
* $serviceIds = $container->findTaggedServiceIds('my.tag');
* foreach ($serviceIds as $serviceId => $tags) {
* foreach ($tags as $tag) {
* echo $tag['hello'];
* }
* }
*
* @return array An array of tags with the tagged service as key, holding a list of attribute arrays
*/
public function findTaggedServiceIds(string $name, bool $throwOnAbstract = false)
{
$this->usedTags[] = $name;
2019-01-16 20:35:37 +00:00
$tags = [];
foreach ($this->getDefinitions() as $id => $definition) {
if ($definition->hasTag($name)) {
if ($throwOnAbstract && $definition->isAbstract()) {
throw new InvalidArgumentException(sprintf('The service "%s" tagged "%s" must not be abstract.', $id, $name));
}
2010-08-05 06:34:53 +01:00
$tags[$id] = $definition->getTag($name);
}
}
2010-08-05 06:34:53 +01:00
return $tags;
}
/**
* Returns all tags the defined services use.
*
* @return array An array of tags
*/
public function findTags()
{
2019-01-16 20:35:37 +00:00
$tags = [];
foreach ($this->getDefinitions() as $id => $definition) {
$tags = array_merge(array_keys($definition->getTags()), $tags);
}
return array_unique($tags);
}
/**
2015-09-28 12:00:47 +01:00
* Returns all tags not queried by findTaggedServiceIds.
*
2015-09-28 12:00:47 +01:00
* @return string[] An array of tags
*/
public function findUnusedTags()
{
2015-09-28 12:00:47 +01:00
return array_values(array_diff($this->findTags(), $this->usedTags));
}
public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
{
$this->expressionLanguageProviders[] = $provider;
}
/**
* @return ExpressionFunctionProviderInterface[]
*/
public function getExpressionLanguageProviders()
{
return $this->expressionLanguageProviders;
}
/**
* Returns a ChildDefinition that will be used for autoconfiguring the interface/class.
*
* @return ChildDefinition
*/
public function registerForAutoconfiguration(string $interface)
{
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
2017-04-27 16:48:07 +01:00
if (!isset($this->autoconfiguredInstanceof[$interface])) {
$this->autoconfiguredInstanceof[$interface] = new ChildDefinition('');
}
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
2017-04-27 16:48:07 +01:00
return $this->autoconfiguredInstanceof[$interface];
}
/**
* Registers an autowiring alias that only binds to a specific argument name.
*
* The argument name is derived from $name if provided (from $id otherwise)
* using camel case: "foo.bar" or "foo_bar" creates an alias bound to
* "$fooBar"-named arguments with $type as type-hint. Such arguments will
* receive the service $id when autowiring is used.
*/
public function registerAliasForArgument(string $id, string $type, string $name = null): Alias
{
$name = lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $name ?? $id))));
if (!preg_match('/^[a-zA-Z_\x7f-\xff]/', $name)) {
throw new InvalidArgumentException(sprintf('Invalid argument name "%s" for service "%s": the first character must be a letter.', $name, $id));
}
return $this->setAlias($type.' $'.$name, $id);
}
/**
* Returns an array of ChildDefinition[] keyed by interface.
*
* @return ChildDefinition[]
*/
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
2017-04-27 16:48:07 +01:00
public function getAutoconfiguredInstanceof()
{
Not allowing autoconfigure, instanceofConditionals or defaults for ChildDefinition Also, not allowing arguments or method calls for autoconfigure. This is a safety mechanism, since we don't have merging logic. It will allow us to add this in the future if we want to. The reason is that parent-child definitions are a different mechanism for "inheritance" than instanceofConditionas and defaults... creating some edge cases when trying to figure out which settings "win". For example: Suppose a child and parent definitions are defined in different YAML files. The child receives public: false from its _defaults, and the parent receives public: true from its _defaults. Should the final child definition be public: true (so the parent overrides the child, even though it only came from _defaults) or public: false (where the child wins... even though it was only set from its _defaults). Or, if the parent is explicitly set to public: true, should that override the public: false of the child (which it got from its _defaults)? On one hand, the parent is being explicitly set. On the other hand, the child is explicitly in a file settings _defaults public to false. There's no correct answer. There are also problems with instanceof. The importance goes: defaults < instanceof < service definition But how does parent-child relationships fit into that? If a child has public: false from an _instanceof, but the parent explicitly sets public: true, which wins? Should we assume the parent definition wins because it's explicitly set? Or would the _instanceof win, because that's being explicitly applied to the child definition's class by an _instanceof that lives in the same file as that class (whereas the parent definition may live in a different file). Because of this, @nicolas-grekas and I (we also talked a bit to Fabien) decided that the complexity was growing too much. The solution is to not allow any of these new feature to be used by ChildDefinition objects. In other words, when you want some sort of "inheritance" for your service, you should *either* giving your service a parent *or* using defaults and instanceof. And instead of silently not applying defaults and instanceof to child definitions, I think it's better to scream that it's not supported.
2017-04-27 16:48:07 +01:00
return $this->autoconfiguredInstanceof;
}
/**
* Resolves env parameter placeholders in a string or an array.
*
* @param mixed $value The value to resolve
* @param string|true|null $format A sprintf() format returning the replacement for each env var name or
* null to resolve back to the original "%env(VAR)%" format or
* true to resolve to the actual values of the referenced env vars
* @param array &$usedEnvs Env vars found while resolving are added to this array
*
* @return mixed The value with env parameters resolved if a string or an array is passed
*/
public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs = null)
{
if (null === $format) {
$format = '%%env(%s)%%';
}
$bag = $this->getParameterBag();
if (true === $format) {
$value = $bag->resolveValue($value);
}
if ($value instanceof Definition) {
$value = (array) $value;
}
if (\is_array($value)) {
2019-01-16 20:35:37 +00:00
$result = [];
foreach ($value as $k => $v) {
$result[\is_string($k) ? $this->resolveEnvPlaceholders($k, $format, $usedEnvs) : $k] = $this->resolveEnvPlaceholders($v, $format, $usedEnvs);
}
return $result;
}
if (!\is_string($value) || 38 > \strlen($value)) {
return $value;
}
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
$completed = false;
foreach ($envPlaceholders as $env => $placeholders) {
foreach ($placeholders as $placeholder) {
if (false !== stripos($value, $placeholder)) {
if (true === $format) {
$resolved = $bag->escapeValue($this->getEnv($env));
} else {
$resolved = sprintf($format, $env);
}
if ($placeholder === $value) {
$value = $resolved;
$completed = true;
} else {
if (!\is_string($resolved) && !is_numeric($resolved)) {
throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type %s inside string value "%s".', $env, \gettype($resolved), $this->resolveEnvPlaceholders($value)));
}
$value = str_ireplace($placeholder, $resolved, $value);
}
$usedEnvs[$env] = $env;
$this->envCounters[$env] = isset($this->envCounters[$env]) ? 1 + $this->envCounters[$env] : 1;
if ($completed) {
break 2;
}
}
}
}
return $value;
}
/**
* Get statistics about env usage.
*
* @return int[] The number of time each env vars has been resolved
*/
public function getEnvCounters()
{
$bag = $this->getParameterBag();
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag->getEnvPlaceholders() : $this->envPlaceholders;
foreach ($envPlaceholders as $env => $placeholders) {
if (!isset($this->envCounters[$env])) {
$this->envCounters[$env] = 0;
}
}
return $this->envCounters;
}
/**
* @final
*/
public function log(CompilerPassInterface $pass, string $message)
{
2018-06-23 13:49:00 +01:00
$this->getCompiler()->log($pass, $this->resolveEnvPlaceholders($message));
}
/**
* Gets removed binding ids.
*
* @return array
*
* @internal
*/
public function getRemovedBindingIds()
{
return $this->removedBindingIds;
}
/**
* Removes bindings for a service.
*
* @internal
*/
public function removeBindings(string $id)
{
if ($this->hasDefinition($id)) {
foreach ($this->getDefinition($id)->getBindings() as $key => $binding) {
list(, $bindingId) = $binding->getValues();
$this->removedBindingIds[(int) $bindingId] = true;
}
}
}
2011-02-13 18:06:41 +00:00
/**
* Returns the Service Conditionals.
*
* @param mixed $value An array of conditionals to return
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return array An array of Service conditionals
*
* @internal
2011-02-13 18:06:41 +00:00
*/
2012-07-09 13:50:58 +01:00
public static function getServiceConditionals($value)
{
2019-01-16 20:35:37 +00:00
$services = [];
if (\is_array($value)) {
foreach ($value as $v) {
$services = array_unique(array_merge($services, self::getServiceConditionals($v)));
}
} elseif ($value instanceof Reference && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $value->getInvalidBehavior()) {
$services[] = (string) $value;
}
2010-01-04 14:26:20 +00:00
return $services;
2010-01-04 14:26:20 +00:00
}
/**
* Returns the initialized conditionals.
*
* @param mixed $value An array of conditionals to return
*
* @return array An array of uninitialized conditionals
*
* @internal
*/
public static function getInitializedConditionals($value)
{
2019-01-16 20:35:37 +00:00
$services = [];
if (\is_array($value)) {
foreach ($value as $v) {
$services = array_unique(array_merge($services, self::getInitializedConditionals($v)));
}
} elseif ($value instanceof Reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()) {
$services[] = (string) $value;
}
return $services;
}
/**
* Computes a reasonably unique hash of a value.
*
* @param mixed $value A serializable value
*
* @return string
*/
public static function hash($value)
{
$hash = substr(base64_encode(hash('sha256', serialize($value), true)), 0, 7);
2019-01-16 20:35:37 +00:00
return str_replace(['/', '+'], ['.', '_'], $hash);
}
/**
* {@inheritdoc}
*/
protected function getEnv($name)
{
$value = parent::getEnv($name);
2016-10-22 17:25:15 +01:00
$bag = $this->getParameterBag();
if (!\is_string($value) || !$bag instanceof EnvPlaceholderParameterBag) {
return $value;
}
$envPlaceholders = $bag->getEnvPlaceholders();
if (isset($envPlaceholders[$name][$value])) {
$bag = new ParameterBag($bag->all());
return $bag->unescapeValue($bag->get("env($name)"));
}
foreach ($envPlaceholders as $env => $placeholders) {
if (isset($placeholders[$value])) {
return $this->getEnv($env);
}
}
2016-10-22 17:25:15 +01:00
$this->resolving["env($name)"] = true;
try {
return $bag->unescapeValue($this->resolveEnvPlaceholders($bag->escapeValue($value), true));
} finally {
unset($this->resolving["env($name)"]);
}
}
private function callMethod($service, array $call, array &$inlineServices)
{
foreach (self::getServiceConditionals($call[1]) as $s) {
if (!$this->has($s)) {
return $service;
}
}
foreach (self::getInitializedConditionals($call[1]) as $s) {
if (!$this->doGet($s, ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE, $inlineServices)) {
return $service;
}
}
$result = $service->{$call[0]}(...$this->doResolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1])), $inlineServices));
return empty($call[2]) ? $service : $result;
}
2013-03-29 23:21:12 +00:00
/**
2014-12-21 17:00:50 +00:00
* Shares a given service in the container.
2013-03-29 23:21:12 +00:00
*
* @param object $service
2013-03-29 23:21:12 +00:00
*/
private function shareService(Definition $definition, $service, ?string $id, array &$inlineServices)
2013-03-29 23:21:12 +00:00
{
$inlineServices[null !== $id ? $id : spl_object_hash($definition)] = $service;
if (null !== $id && $definition->isShared()) {
$this->services[$id] = $service;
unset($this->loading[$id]);
2013-03-29 23:21:12 +00:00
}
}
private function getExpressionLanguage()
{
if (null === $this->expressionLanguage) {
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
throw new LogicException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
}
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
}
return $this->expressionLanguage;
}
private function inVendors(string $path)
{
if (null === $this->vendors) {
$resource = new ComposerResource();
$this->vendors = $resource->getVendors();
$this->addResource($resource);
}
$path = realpath($path) ?: $path;
foreach ($this->vendors as $vendor) {
if (0 === strpos($path, $vendor) && false !== strpbrk(substr($path, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
return true;
}
}
return false;
}
2010-01-04 14:26:20 +00:00
}