2010-01-04 14:26:20 +00:00
< ? php
2011-01-15 13:29:43 +00:00
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2011-01-15 13:29:43 +00:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2010-08-20 22:09:55 +01:00
namespace Symfony\Component\DependencyInjection ;
2010-01-04 14:26:20 +00:00
2017-02-16 11:05:13 +00:00
use Psr\Container\ContainerInterface as PsrContainerInterface ;
2016-12-13 17:37:51 +00:00
use Symfony\Component\DependencyInjection\Argument\IteratorArgument ;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator ;
2017-02-26 17:31:03 +00:00
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument ;
2011-01-09 09:53:46 +00:00
use Symfony\Component\DependencyInjection\Compiler\Compiler ;
2010-12-14 23:25:33 +00:00
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface ;
2011-01-16 07:53:28 +00:00
use Symfony\Component\DependencyInjection\Compiler\PassConfig ;
2017-08-18 16:51:40 +01:00
use Symfony\Component\DependencyInjection\Compiler\ResolveEnvPlaceholdersPass ;
2011-12-04 23:51:22 +00:00
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException ;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException ;
use Symfony\Component\DependencyInjection\Exception\LogicException ;
use Symfony\Component\DependencyInjection\Exception\RuntimeException ;
2016-02-07 18:10:24 +00:00
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException ;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException ;
2010-08-20 22:09:55 +01:00
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface ;
2016-08-12 19:34:27 +01:00
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag ;
2017-01-29 18:27:06 +00:00
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag ;
2017-01-26 15:09:57 +00:00
use Symfony\Component\Config\Resource\ClassExistenceResource ;
2017-02-02 08:31:13 +00:00
use Symfony\Component\Config\Resource\ComposerResource ;
2017-01-30 09:47:16 +00:00
use Symfony\Component\Config\Resource\DirectoryResource ;
use Symfony\Component\Config\Resource\FileExistenceResource ;
2011-02-10 15:15:51 +00:00
use Symfony\Component\Config\Resource\FileResource ;
2017-05-03 14:01:12 +01:00
use Symfony\Component\Config\Resource\GlobResource ;
2017-01-26 15:09:57 +00:00
use Symfony\Component\Config\Resource\ReflectionClassResource ;
2011-02-10 15:15:51 +00:00
use Symfony\Component\Config\Resource\ResourceInterface ;
2013-03-29 23:21:12 +00:00
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface ;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator ;
2017-02-16 11:05:13 +00:00
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface ;
2017-03-30 09:29:34 +01:00
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher ;
2013-09-02 11:46:47 +01:00
use Symfony\Component\ExpressionLanguage\Expression ;
2014-09-23 16:21:57 +01:00
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface ;
2010-07-15 14:11:33 +01:00
2010-01-04 14:26:20 +00:00
/**
2010-07-15 14:11:33 +01:00
* ContainerBuilder is a DI container that provides an API to easily describe services .
2010-01-04 14:26:20 +00:00
*
2011-03-06 11:40:06 +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
{
2012-11-01 15:08:59 +00:00
/**
* @ var ExtensionInterface []
*/
private $extensions = array ();
/**
* @ var ExtensionInterface []
*/
private $extensionsByNs = array ();
/**
* @ var Definition []
*/
private $definitions = array ();
/**
* @ var Alias []
*/
2013-06-04 14:12:01 +01:00
private $aliasDefinitions = array ();
2012-11-01 15:08:59 +00:00
/**
* @ var ResourceInterface []
*/
private $resources = array ();
2011-03-11 13:50:46 +00:00
private $extensionConfigs = array ();
2012-11-01 15:08:59 +00:00
/**
* @ var Compiler
*/
2011-03-11 13:50:46 +00:00
private $compiler ;
2010-12-14 23:25:33 +00:00
2017-04-02 14:23:09 +01:00
private $trackResources ;
2012-12-28 09:40:13 +00:00
2013-03-29 23:21:12 +00:00
/**
* @ var InstantiatorInterface | null
*/
private $proxyInstantiator ;
2013-09-02 11:46:47 +01:00
/**
* @ var ExpressionLanguage | null
*/
private $expressionLanguage ;
2014-09-23 16:21:57 +01:00
/**
* @ var ExpressionFunctionProviderInterface []
*/
private $expressionLanguageProviders = array ();
2014-10-29 09:15:52 +00:00
/**
2015-09-28 12:00:47 +01:00
* @ var string [] with tag names used by findTaggedServiceIds
2014-10-29 09:15:52 +00:00
*/
private $usedTags = array ();
2016-08-12 19:34:27 +01:00
/**
2017-01-02 14:07:02 +00:00
* @ var string [][] a map of env var names to their placeholders
2016-08-12 19:34:27 +01:00
*/
private $envPlaceholders = array ();
/**
2017-01-02 14:07:02 +00:00
* @ var int [] a map of env vars to their resolution counter
2016-08-12 19:34:27 +01:00
*/
private $envCounters = array ();
2017-02-02 08:31:13 +00:00
/**
* @ var string [] the list of vendor directories
*/
private $vendors ;
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
private $autoconfiguredInstanceof = array ();
2017-03-31 11:45:36 +01:00
2017-02-16 11:05:13 +00:00
public function __construct ( ParameterBagInterface $parameterBag = null )
{
parent :: __construct ( $parameterBag );
2017-04-04 08:47:41 +01:00
$this -> trackResources = interface_exists ( 'Symfony\Component\Config\Resource\ResourceInterface' );
2017-02-16 11:05:13 +00:00
$this -> setDefinition ( 'service_container' , ( new Definition ( ContainerInterface :: class )) -> setSynthetic ( true ));
$this -> setAlias ( PsrContainerInterface :: class , new Alias ( 'service_container' , false ));
$this -> setAlias ( ContainerInterface :: class , new Alias ( 'service_container' , false ));
}
2017-02-28 14:54:44 +00:00
/**
* @ var \ReflectionClass [] a list of class reflectors
*/
private $classReflectors ;
2012-12-28 09:40:13 +00:00
/**
* 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 .
*
2017-10-28 19:51:46 +01:00
* @ param bool $track True if you want to track resources , false otherwise
2012-12-28 09:40:13 +00:00
*/
public function setResourceTracking ( $track )
{
2014-04-12 18:44:00 +01:00
$this -> trackResources = ( bool ) $track ;
2012-12-28 09:40:13 +00:00
}
/**
* Checks if resources are tracked .
*
2017-10-28 19:51:46 +01:00
* @ return bool true If resources are tracked , false otherwise
2012-12-28 09:40:13 +00:00
*/
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 ;
}
2011-02-15 06:52:11 +00:00
public function registerExtension ( ExtensionInterface $extension )
2010-07-16 08:12:58 +01:00
{
2011-02-15 06:52:11 +00:00
$this -> extensions [ $extension -> getAlias ()] = $extension ;
2011-02-15 03:14:08 +00:00
if ( false !== $extension -> getNamespace ()) {
2011-02-15 06:52:11 +00:00
$this -> extensionsByNs [ $extension -> getNamespace ()] = $extension ;
2011-02-15 03:14:08 +00:00
}
2010-07-16 08:12:58 +01:00
}
/**
* Returns an extension by alias or namespace .
*
* @ param string $name An alias or a namespace
*
* @ return ExtensionInterface An extension instance
2011-07-20 09:50:27 +01:00
*
2012-12-16 12:02:54 +00:00
* @ throws LogicException if the extension is not registered
2010-07-16 08:12:58 +01:00
*/
2011-02-15 06:52:11 +00:00
public function getExtension ( $name )
2010-07-16 08:12:58 +01:00
{
2011-02-15 06:52:11 +00:00
if ( isset ( $this -> extensions [ $name ])) {
return $this -> extensions [ $name ];
2011-02-15 04:58:18 +00:00
}
2011-02-05 18:42:07 +00:00
2011-02-15 06:52:11 +00:00
if ( isset ( $this -> extensionsByNs [ $name ])) {
return $this -> extensionsByNs [ $name ];
2010-07-16 08:12:58 +01:00
}
2011-12-04 23:51:22 +00:00
throw new LogicException ( sprintf ( 'Container extension "%s" is not registered' , $name ));
2010-07-16 08:12:58 +01:00
}
2011-02-05 18:42:07 +00:00
/**
2011-02-15 04:58:18 +00:00
* Returns all registered extensions .
2011-02-05 18:42:07 +00:00
*
2012-11-01 15:08:59 +00:00
* @ return ExtensionInterface [] An array of ExtensionInterface
2011-02-05 18:42:07 +00:00
*/
2011-02-15 06:52:11 +00:00
public function getExtensions ()
2011-02-05 18:42:07 +00:00
{
2011-02-15 06:52:11 +00:00
return $this -> extensions ;
2011-02-05 18:42:07 +00:00
}
2011-02-13 18:06:41 +00:00
/**
* Checks if we have an extension .
*
* @ param string $name The name of the extension
2011-12-13 07:50:54 +00:00
*
2014-11-30 13:33:44 +00:00
* @ return bool If the extension exists
2011-02-13 18:06:41 +00:00
*/
2011-02-15 06:52:11 +00:00
public function hasExtension ( $name )
2010-07-16 08:12:58 +01:00
{
2011-02-15 06:52:11 +00:00
return isset ( $this -> extensions [ $name ]) || isset ( $this -> extensionsByNs [ $name ]);
2011-02-15 04:58:18 +00:00
}
2010-07-15 14:11:33 +01:00
/**
* Returns an array of resources loaded to build this configuration .
*
* @ return ResourceInterface [] An array of resources
*/
public function getResources ()
{
2017-02-01 13:00:02 +00:00
return array_values ( $this -> resources );
2010-07-15 14:11:33 +01:00
}
/**
2016-12-26 07:50:27 +00:00
* @ return $this
2010-07-15 14:11:33 +01:00
*/
public function addResource ( ResourceInterface $resource )
{
2012-12-28 09:40:13 +00:00
if ( ! $this -> trackResources ) {
2017-05-03 14:01:12 +01:00
return $this ;
}
if ( $resource instanceof GlobResource && $this -> inVendors ( $resource -> getPrefix ())) {
2012-12-28 09:40:13 +00:00
return $this ;
}
2017-02-01 13:00:02 +00:00
$this -> resources [( string ) $resource ] = $resource ;
2010-07-15 14:11:33 +01:00
return $this ;
}
2012-11-01 15:08:59 +00:00
/**
* Sets the resources for this configuration .
2012-12-11 10:49:22 +00:00
*
2012-11-01 15:08:59 +00:00
* @ param ResourceInterface [] $resources An array of resources
2012-12-11 10:49:22 +00:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2012-11-01 15:08:59 +00:00
*/
2011-09-16 12:43:09 +01:00
public function setResources ( array $resources )
{
2012-12-28 09:40:13 +00:00
if ( ! $this -> trackResources ) {
return $this ;
}
2011-09-16 12:43:09 +01:00
$this -> resources = $resources ;
return $this ;
}
2010-07-15 14:11:33 +01:00
/**
* Adds the object class hierarchy as resources .
*
2017-01-26 15:09:57 +00:00
* @ param object | string $object An object instance or class name
2011-07-20 09:50:27 +01:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-07-15 14:11:33 +01:00
*/
public function addObjectResource ( $object )
2013-03-29 23:21:12 +00:00
{
if ( $this -> trackResources ) {
2017-01-26 15:09:57 +00:00
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 )) {
2017-02-02 08:31:13 +00:00
$this -> fileExists ( $file );
2017-01-26 15:09:57 +00:00
}
}
do {
$file = $class -> getFileName ();
if ( false !== $file && file_exists ( $file )) {
2017-02-02 08:31:13 +00:00
$this -> fileExists ( $file );
2017-01-26 15:09:57 +00:00
}
foreach ( $class -> getTraitNames () as $name ) {
$this -> addObjectResource ( $name );
}
} while ( $class = $class -> getParentClass ());
2013-03-29 23:21:12 +00:00
}
return $this ;
}
/**
* Adds the given class hierarchy as resources .
*
2016-12-26 07:50:27 +00:00
* @ return $this
2017-01-26 15:09:57 +00:00
*
* @ deprecated since version 3.3 , to be removed in 4.0 . Use addObjectResource () or getReflectionClass () instead .
2013-03-29 23:21:12 +00:00
*/
public function addClassResource ( \ReflectionClass $class )
2010-07-15 14:11:33 +01:00
{
2017-01-26 15:09:57 +00:00
@ trigger_error ( 'The ' . __METHOD__ . '() method is deprecated since version 3.3 and will be removed in 4.0. Use the addObjectResource() or the getReflectionClass() method instead.' , E_USER_DEPRECATED );
return $this -> addObjectResource ( $class -> name );
}
/**
* Retrieves the requested reflection class and registers it for resource tracking .
*
* @ param string $class
2017-07-18 12:08:22 +01:00
* @ param bool $throw
2017-01-26 15:09:57 +00:00
*
* @ return \ReflectionClass | null
*
2017-07-18 12:08:22 +01:00
* @ throws \ReflectionException when a parent class / interface / trait is not found and $throw is true
*
2017-01-26 15:09:57 +00:00
* @ final
*/
2017-07-18 12:08:22 +01:00
public function getReflectionClass ( $class , $throw = true )
2017-01-26 15:09:57 +00:00
{
if ( ! $class = $this -> getParameterBag () -> resolveValue ( $class )) {
return ;
}
$resource = null ;
try {
if ( isset ( $this -> classReflectors [ $class ])) {
$classReflector = $this -> classReflectors [ $class ];
} else {
2017-06-02 18:23:28 +01:00
$resource = new ClassExistenceResource ( $class , false );
$classReflector = $resource -> isFresh ( 0 ) ? false : new \ReflectionClass ( $class );
2017-01-26 15:09:57 +00:00
}
} catch ( \ReflectionException $e ) {
2017-07-18 12:08:22 +01:00
if ( $throw ) {
throw $e ;
}
2017-01-26 15:09:57 +00:00
$classReflector = false ;
}
if ( $this -> trackResources ) {
if ( ! $classReflector ) {
2017-06-02 18:23:28 +01:00
$this -> addResource ( $resource ? : new ClassExistenceResource ( $class , false ));
2017-01-26 15:09:57 +00:00
} elseif ( ! $classReflector -> isInternal ()) {
2017-02-02 08:31:13 +00:00
$path = $classReflector -> getFileName ();
if ( ! $this -> inVendors ( $path )) {
$this -> addResource ( new ReflectionClassResource ( $classReflector , $this -> vendors ));
}
2017-01-26 15:09:57 +00:00
}
$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
*
* @ return bool
*
* @ final
*/
public function fileExists ( $path , $trackContents = true )
{
$exists = file_exists ( $path );
2017-02-02 08:31:13 +00:00
if ( ! $this -> trackResources || $this -> inVendors ( $path )) {
2017-01-26 15:09:57 +00:00
return $exists ;
2012-12-28 09:40:13 +00:00
}
2017-01-26 15:09:57 +00:00
if ( ! $exists ) {
$this -> addResource ( new FileExistenceResource ( $path ));
return $exists ;
}
2017-08-11 20:45:37 +01:00
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 ) {
2017-02-02 08:31:13 +00:00
$this -> addResource ( new FileResource ( $path ));
2017-01-26 15:09:57 +00:00
}
2012-11-16 13:04:00 +00:00
2017-01-26 15:09:57 +00:00
return $exists ;
2010-07-15 14:11:33 +01:00
}
/**
* 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
2010-07-15 14:11:33 +01:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2011-07-20 09:50:27 +01:00
*
2016-08-19 13:18:08 +01:00
* @ throws BadMethodCallException When this ContainerBuilder is compiled
* @ throws \LogicException if the extension is not registered
2010-07-15 14:11:33 +01:00
*/
2011-02-05 18:42:07 +00:00
public function loadFromExtension ( $extension , array $values = array ())
2010-07-15 14:11:33 +01:00
{
2016-08-19 13:18:08 +01:00
if ( $this -> isCompiled ()) {
throw new BadMethodCallException ( 'Cannot load from an extension on a compiled container.' );
2010-07-16 08:15:22 +01:00
}
2010-08-24 15:25:08 +01:00
$namespace = $this -> getExtension ( $extension ) -> getAlias ();
2010-07-15 14:11:33 +01:00
2011-07-09 17:03:05 +01:00
$this -> extensionConfigs [ $namespace ][] = $values ;
2010-07-15 14:11:33 +01:00
return $this ;
}
2010-05-06 12:25:53 +01:00
2010-12-14 23:25:33 +00:00
/**
2011-03-03 12:53:29 +00:00
* Adds a compiler pass .
2010-12-14 23:25:33 +00:00
*
2016-03-05 12:39:12 +00:00
* @ param CompilerPassInterface $pass A compiler pass
* @ param string $type The type of compiler pass
* @ param int $priority Used to sort the passes
2011-07-20 09:50:27 +01:00
*
2016-12-26 07:50:27 +00:00
* @ return $this
2010-12-14 23:25:33 +00:00
*/
2017-05-18 18:20:35 +01:00
public function addCompilerPass ( CompilerPassInterface $pass , $type = PassConfig :: TYPE_BEFORE_OPTIMIZATION /*, int $priority = 0*/ )
2010-12-14 23:25:33 +00:00
{
2016-03-05 12:39:12 +00:00
if ( func_num_args () >= 3 ) {
$priority = func_get_arg ( 2 );
} else {
2016-11-04 13:53:25 +00:00
if ( __CLASS__ !== get_class ( $this )) {
$r = new \ReflectionMethod ( $this , __FUNCTION__ );
if ( __CLASS__ !== $r -> getDeclaringClass () -> getName ()) {
2017-05-18 18:20:35 +01:00
@ trigger_error ( sprintf ( 'Method %s() will have a third `int $priority = 0` argument in version 4.0. Not defining it is deprecated since 3.2.' , __METHOD__ ), E_USER_DEPRECATED );
2016-11-04 13:53:25 +00:00
}
}
2016-03-05 12:39:12 +00:00
$priority = 0 ;
}
$this -> getCompiler () -> addPass ( $pass , $type , $priority );
2011-01-16 07:38:41 +00:00
$this -> addObjectResource ( $pass );
2012-11-16 13:04:00 +00:00
return $this ;
2010-12-14 23:25:33 +00:00
}
/**
2011-03-03 12:53:29 +00:00
* Returns the compiler pass config which can then be modified .
2010-12-14 23:25:33 +00:00
*
2011-03-03 12:53:29 +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
{
2013-09-06 16:04:46 +01:00
return $this -> getCompiler () -> getPassConfig ();
2011-01-09 09:53:46 +00:00
}
/**
2011-03-03 12:53:29 +00:00
* Returns the compiler .
2011-01-09 09:53:46 +00:00
*
2011-03-03 12:53:29 +00:00
* @ return Compiler The compiler
2011-01-09 09:53:46 +00:00
*/
public function getCompiler ()
{
2011-03-08 13:34:07 +00:00
if ( null === $this -> compiler ) {
$this -> compiler = new Compiler ();
}
2011-01-09 09:53:46 +00:00
return $this -> compiler ;
2010-12-14 23:25:33 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Sets a service .
*
* @ param string $id The service identifier
* @ param object $service The service instance
2010-10-02 15:47:35 +01:00
*
2016-08-19 13:18:08 +01:00
* @ throws BadMethodCallException When this ContainerBuilder is compiled
2010-05-06 12:25:53 +01:00
*/
2015-09-04 20:54:37 +01:00
public function set ( $id , $service )
2010-01-04 14:26:20 +00:00
{
2017-01-10 07:04:52 +00:00
$id = $this -> normalizeId ( $id );
2013-03-11 21:43:32 +00:00
2016-08-19 13:18:08 +01: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 ));
2013-02-06 20:42:01 +00:00
}
2013-06-04 14:12:01 +01:00
unset ( $this -> definitions [ $id ], $this -> aliasDefinitions [ $id ]);
2010-05-06 12:25:53 +01:00
2015-09-04 20:54:37 +01:00
parent :: set ( $id , $service );
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
2010-09-02 11:20:20 +01:00
/**
2011-04-19 22:40:18 +01:00
* Removes a service definition .
2010-09-02 11:20:20 +01:00
*
* @ param string $id The service identifier
*/
2011-04-19 22:40:18 +01:00
public function removeDefinition ( $id )
2010-09-02 11:20:20 +01:00
{
2017-01-10 07:04:52 +00:00
unset ( $this -> definitions [ $this -> normalizeId ( $id )]);
2010-09-02 11:20:20 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns true if the given service is defined .
*
2012-05-15 21:19:31 +01:00
* @ param string $id The service identifier
2010-05-06 12:25:53 +01:00
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the service is defined , false otherwise
2010-05-06 12:25:53 +01:00
*/
2010-06-27 17:28:29 +01:00
public function has ( $id )
2010-05-06 12:25:53 +01:00
{
2017-01-10 07:04:52 +00:00
$id = $this -> normalizeId ( $id );
2011-01-03 08:07:06 +00:00
2013-06-04 14:12:01 +01:00
return isset ( $this -> definitions [ $id ]) || isset ( $this -> aliasDefinitions [ $id ]) || parent :: has ( $id );
2010-05-06 12:25:53 +01:00
}
/**
* 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
2010-05-06 12:25:53 +01:00
*
* @ return object The associated service
*
2016-02-07 18:10:24 +00:00
* @ 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
2010-05-06 12:25:53 +01:00
*
* @ see Reference
*/
2010-06-27 17:28:29 +01:00
public function get ( $id , $invalidBehavior = ContainerInterface :: EXCEPTION_ON_INVALID_REFERENCE )
2010-01-04 14:26:20 +00:00
{
2017-01-10 07:04:52 +00:00
$id = $this -> normalizeId ( $id );
2011-01-03 08:07:06 +00:00
2014-03-26 13:45:08 +00:00
if ( $service = parent :: get ( $id , ContainerInterface :: NULL_ON_INVALID_REFERENCE )) {
2014-03-27 18:14:17 +00:00
return $service ;
2014-03-26 13:45:08 +00:00
}
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 ])) {
2017-01-10 10:21:41 +00:00
return $this -> get (( string ) $this -> aliasDefinitions [ $id ], $invalidBehavior );
2014-03-26 13:45:08 +00:00
}
2011-01-03 08:07:06 +00:00
2010-05-07 15:09:11 +01:00
try {
2014-03-26 13:45:08 +00:00
$definition = $this -> getDefinition ( $id );
2016-02-07 18:10:24 +00:00
} catch ( ServiceNotFoundException $e ) {
2013-04-17 12:21:11 +01:00
if ( ContainerInterface :: EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior ) {
2014-04-16 08:15:58 +01:00
return ;
2013-04-17 12:21:11 +01:00
}
throw $e ;
2014-03-26 13:45:08 +00:00
}
2010-05-06 12:25:53 +01:00
2014-03-26 13:45:08 +00:00
$this -> loading [ $id ] = true ;
2010-05-06 12:25:53 +01:00
2014-03-26 13:45:08 +00:00
try {
2017-11-23 08:59:26 +00:00
$service = $this -> createService ( $definition , new \SplObjectStorage (), $id );
2015-09-28 01:26:11 +01:00
} finally {
2014-03-26 13:45:08 +00:00
unset ( $this -> loading [ $id ]);
}
2010-01-04 14:26:20 +00:00
2014-03-26 13:45:08 +00:00
return $service ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
2010-07-15 14:11:33 +01:00
* Merges a ContainerBuilder with the current ContainerBuilder configuration .
2010-05-06 12:25:53 +01:00
*
* 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 .
*
2010-07-15 14:11:33 +01:00
* $container = new ContainerBuilder ( array ( 'foo' => 'bar' ));
2010-05-06 12:25:53 +01:00
* $loader = new LoaderXXX ( $container );
* $loader -> load ( 'resource_name' );
* $container -> register ( 'foo' , new stdClass ());
*
* In the above example , even if the loaded resource defines a foo
2010-07-15 14:11:33 +01:00
* parameter , the value will still be 'bar' as defined in the ContainerBuilder
2010-05-06 12:25:53 +01:00
* constructor .
2011-02-13 18:06:41 +00:00
*
2016-08-19 13:18:08 +01:00
* @ throws BadMethodCallException When this ContainerBuilder is compiled
2010-05-06 12:25:53 +01:00
*/
2010-07-15 14:11:33 +01:00
public function merge ( ContainerBuilder $container )
2010-01-04 14:26:20 +00:00
{
2016-08-19 13:18:08 +01:00
if ( $this -> isCompiled ()) {
throw new BadMethodCallException ( 'Cannot merge on a compiled container.' );
2010-07-16 08:15:22 +01:00
}
2010-07-15 14:11:33 +01:00
$this -> addDefinitions ( $container -> getDefinitions ());
$this -> addAliases ( $container -> getAliases ());
2011-03-11 13:50:46 +00:00
$this -> getParameterBag () -> add ( $container -> getParameterBag () -> all ());
2010-07-15 14:11:33 +01:00
2012-12-28 09:40:13 +00:00
if ( $this -> trackResources ) {
foreach ( $container -> getResources () as $resource ) {
$this -> addResource ( $resource );
}
2010-07-15 14:11:33 +01:00
}
2011-02-15 06:52:11 +00:00
foreach ( $this -> extensions as $name => $extension ) {
2011-02-15 04:58:18 +00:00
if ( ! isset ( $this -> extensionConfigs [ $name ])) {
$this -> extensionConfigs [ $name ] = array ();
2010-07-15 14:11:33 +01:00
}
2011-02-15 04:58:18 +00:00
$this -> extensionConfigs [ $name ] = array_merge ( $this -> extensionConfigs [ $name ], $container -> getExtensionConfig ( $name ));
2010-05-06 12:25:53 +01:00
}
2016-08-12 19:34:27 +01:00
if ( $this -> getParameterBag () instanceof EnvPlaceholderParameterBag && $container -> getParameterBag () instanceof EnvPlaceholderParameterBag ) {
2017-08-28 16:40:54 +01:00
$envPlaceholders = $container -> getParameterBag () -> getEnvPlaceholders ();
2016-08-12 19:34:27 +01:00
$this -> getParameterBag () -> mergeEnvPlaceholders ( $container -> getParameterBag ());
2017-08-28 16:40:54 +01:00
} else {
$envPlaceholders = array ();
2016-08-12 19:34:27 +01:00
}
foreach ( $container -> envCounters as $env => $count ) {
2017-08-28 16:40:54 +01:00
if ( ! $count && ! isset ( $envPlaceholders [ $env ])) {
continue ;
}
2016-08-12 19:34:27 +01:00
if ( ! isset ( $this -> envCounters [ $env ])) {
$this -> envCounters [ $env ] = $count ;
} else {
$this -> envCounters [ $env ] += $count ;
}
}
2017-03-31 11:45:36 +01:00
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 ));
2017-03-31 11:45:36 +01:00
}
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 ;
2017-03-31 11:45:36 +01:00
}
2010-07-15 14:11:33 +01:00
}
/**
2011-02-15 04:58:18 +00:00
* Returns the configuration array for the given extension .
2010-07-15 14:11:33 +01:00
*
2011-02-15 04:58:18 +00:00
* @ param string $name The name of the extension
2011-02-05 18:42:07 +00:00
*
2011-02-15 04:58:18 +00:00
* @ return array An array of configuration
2011-02-05 18:42:07 +00:00
*/
public function getExtensionConfig ( $name )
{
2011-02-15 04:58:18 +00:00
if ( ! isset ( $this -> extensionConfigs [ $name ])) {
2011-03-03 12:53:29 +00:00
$this -> extensionConfigs [ $name ] = array ();
2011-02-05 18:42:07 +00:00
}
return $this -> extensionConfigs [ $name ];
}
2012-09-20 22:20:53 +01:00
/**
* Prepends a config array to the configs of the given extension .
*
2014-11-30 13:33:44 +00:00
* @ param string $name The name of the extension
* @ param array $config The config to set
2012-09-20 22:20:53 +01:00
*/
public function prependExtensionConfig ( $name , array $config )
{
if ( ! isset ( $this -> extensionConfigs [ $name ])) {
$this -> extensionConfigs [ $name ] = array ();
}
array_unshift ( $this -> extensionConfigs [ $name ], $config );
}
2010-07-15 14:11:33 +01:00
/**
2011-01-16 07:12:36 +00:00
* Compiles the container .
2010-07-16 08:15:22 +01:00
*
2011-01-16 07:12:36 +00:00
* 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 :
2010-07-16 08:15:22 +01:00
*
* * The extension configurations are merged ;
* * Parameter values are resolved ;
2011-01-16 07:12:36 +00:00
* * The parameter bag is frozen ;
2010-07-16 08:15:22 +01:00
* * Extension loading is disabled .
2017-01-29 18:27:06 +00:00
*
* @ 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 .
2010-07-15 14:11:33 +01:00
*/
2017-01-29 18:27:06 +00:00
public function compile ( /*$resolveEnvPlaceholders = false*/ )
2010-07-15 14:11:33 +01:00
{
2017-01-29 18:27:06 +00:00
if ( 1 <= func_num_args ()) {
$resolveEnvPlaceholders = func_get_arg ( 0 );
} else {
if ( __CLASS__ !== static :: class ) {
$r = new \ReflectionMethod ( $this , __FUNCTION__ );
if ( __CLASS__ !== $r -> getDeclaringClass () -> getName () && ( 1 > $r -> getNumberOfParameters () || 'resolveEnvPlaceholders' !== $r -> getParameters ()[ 0 ] -> name )) {
@ trigger_error ( sprintf ( 'The %s::compile() method expects a first "$resolveEnvPlaceholders" argument since version 3.3. It will be made mandatory in 4.0.' , static :: class ), E_USER_DEPRECATED );
}
}
$resolveEnvPlaceholders = false ;
}
2013-09-06 16:04:46 +01:00
$compiler = $this -> getCompiler ();
2011-03-08 13:34:07 +00:00
2012-12-28 09:40:13 +00:00
if ( $this -> trackResources ) {
2013-09-06 16:04:46 +01:00
foreach ( $compiler -> getPassConfig () -> getPasses () as $pass ) {
2012-12-28 09:40:13 +00:00
$this -> addObjectResource ( $pass );
}
2013-11-09 11:47:52 +00:00
}
2017-01-29 18:27:06 +00:00
$bag = $this -> getParameterBag ();
if ( $resolveEnvPlaceholders && $bag instanceof EnvPlaceholderParameterBag ) {
2017-08-18 16:51:40 +01:00
$compiler -> addPass ( new ResolveEnvPlaceholdersPass (), PassConfig :: TYPE_AFTER_REMOVING , - 1000 );
2017-01-29 18:27:06 +00:00
}
2013-11-09 11:47:52 +00:00
2013-11-13 21:30:16 +00:00
$compiler -> compile ( $this );
2013-03-29 23:21:12 +00:00
2016-06-22 21:01:50 +01:00
foreach ( $this -> definitions as $id => $definition ) {
2017-01-26 15:09:57 +00:00
if ( $this -> trackResources && $definition -> isLazy ()) {
$this -> getReflectionClass ( $definition -> getClass ());
2013-03-29 23:21:12 +00:00
}
2011-01-27 19:42:41 +00:00
}
2011-02-15 04:58:18 +00:00
$this -> extensionConfigs = array ();
2011-01-16 09:17:38 +00:00
2017-08-16 09:13:36 +01:00
if ( $bag instanceof EnvPlaceholderParameterBag ) {
2017-08-18 16:51:40 +01:00
if ( $resolveEnvPlaceholders ) {
$this -> parameterBag = new ParameterBag ( $this -> resolveEnvPlaceholders ( $bag -> all (), true ));
}
2017-08-16 09:13:36 +01:00
$this -> envPlaceholders = $bag -> getEnvPlaceholders ();
}
2017-08-18 16:51:40 +01:00
parent :: compile ();
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Gets all service ids .
*
* @ return array An array of all defined service ids
*/
public function getServiceIds ()
2010-01-04 14:26:20 +00:00
{
2013-06-04 14:12:01 +01:00
return array_unique ( array_merge ( array_keys ( $this -> getDefinitions ()), array_keys ( $this -> aliasDefinitions ), parent :: getServiceIds ()));
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Adds the service aliases .
*/
public function addAliases ( array $aliases )
2010-02-02 10:45:21 +00:00
{
2010-05-07 15:09:11 +01:00
foreach ( $aliases as $alias => $id ) {
2010-05-06 12:25:53 +01:00
$this -> setAlias ( $alias , $id );
}
2010-02-02 10:45:21 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Sets the service aliases .
*/
public function setAliases ( array $aliases )
2010-01-21 20:28:21 +00:00
{
2013-06-04 14:12:01 +01:00
$this -> aliasDefinitions = array ();
2010-05-06 12:25:53 +01:00
$this -> addAliases ( $aliases );
2010-01-21 20:28:21 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* 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
2012-09-07 23:56:45 +01:00
*
2012-12-16 12:02:54 +00:00
* @ throws InvalidArgumentException if the id is not a string or an Alias
* @ throws InvalidArgumentException if the alias is for itself
2010-05-06 12:25:53 +01:00
*/
public function setAlias ( $alias , $id )
2010-01-04 14:26:20 +00:00
{
2017-01-10 07:04:52 +00:00
$alias = $this -> normalizeId ( $alias );
2011-01-07 14:44:29 +00:00
if ( is_string ( $id )) {
2017-01-10 07:04:52 +00:00
$id = new Alias ( $this -> normalizeId ( $id ));
2011-12-18 13:42:59 +00:00
} elseif ( ! $id instanceof Alias ) {
2011-12-04 23:51:22 +00:00
throw new InvalidArgumentException ( '$id must be a string, or an Alias object.' );
2011-01-07 14:44:29 +00:00
}
2011-01-03 08:07:06 +00:00
2015-04-25 17:37:49 +01: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 ));
2011-02-12 14:43:38 +00:00
}
2010-05-06 12:25:53 +01:00
unset ( $this -> definitions [ $alias ]);
2013-06-04 14:12:01 +01:00
$this -> aliasDefinitions [ $alias ] = $id ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
2010-09-02 11:20:20 +01:00
/**
* Removes an alias .
*
* @ param string $alias The alias to remove
*/
public function removeAlias ( $alias )
{
2017-01-10 07:04:52 +00:00
unset ( $this -> aliasDefinitions [ $this -> normalizeId ( $alias )]);
2010-09-02 11:20:20 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns true if an alias exists under the given identifier .
*
2012-05-15 21:19:31 +01:00
* @ param string $id The service identifier
2010-05-06 12:25:53 +01:00
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the alias exists , false otherwise
2010-05-06 12:25:53 +01:00
*/
public function hasAlias ( $id )
2010-01-04 14:26:20 +00:00
{
2017-01-10 07:04:52 +00:00
return isset ( $this -> aliasDefinitions [ $this -> normalizeId ( $id )]);
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Gets all defined aliases .
*
2012-09-07 23:56:45 +01:00
* @ return Alias [] An array of aliases
2010-05-06 12:25:53 +01:00
*/
public function getAliases ()
2010-01-04 14:26:20 +00:00
{
2013-06-04 14:12:01 +01:00
return $this -> aliasDefinitions ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Gets an alias .
*
2012-05-15 21:19:31 +01:00
* @ param string $id The service identifier
2010-05-06 12:25:53 +01:00
*
2012-08-03 10:22:09 +01:00
* @ return Alias An Alias instance
2010-05-06 12:25:53 +01:00
*
2011-12-04 23:51:22 +00:00
* @ throws InvalidArgumentException if the alias does not exist
2010-05-06 12:25:53 +01:00
*/
public function getAlias ( $id )
{
2017-01-10 07:04:52 +00:00
$id = $this -> normalizeId ( $id );
2011-01-03 08:07:06 +00:00
2015-04-25 17:37:49 +01:00
if ( ! isset ( $this -> aliasDefinitions [ $id ])) {
2011-12-04 23:51:22 +00:00
throw new InvalidArgumentException ( sprintf ( 'The service alias "%s" does not exist.' , $id ));
2010-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2013-06-04 14:12:01 +01:00
return $this -> aliasDefinitions [ $id ];
2010-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
/**
* Registers a service definition .
*
* This methods allows for simple registration of service definition
* with a fluid interface .
*
2017-08-08 18:11:20 +01:00
* @ param string $id The service identifier
* @ param string $class | null The service class
2010-05-06 12:25:53 +01:00
*
* @ return Definition A Definition instance
*/
2010-07-04 17:56:48 +01:00
public function register ( $id , $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
}
2010-05-06 12:25:53 +01:00
2016-11-26 16:34:50 +00:00
/**
* Registers an autowired service definition .
*
* This method implements a shortcut for using setDefinition () with
* an autowired definition .
*
* @ param string $id The service identifier
* @ param null | string $class The service class
*
* @ return Definition The created definition
*/
public function autowire ( $id , $class = null )
{
return $this -> setDefinition ( $id , ( new Definition ( $class )) -> setAutowired ( true ));
}
2010-05-06 12:25:53 +01:00
/**
* 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
{
2010-05-07 15:09:11 +01:00
foreach ( $definitions as $id => $definition ) {
2010-05-06 12:25:53 +01:00
$this -> setDefinition ( $id , $definition );
}
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Sets the service definitions .
*
2012-11-01 15:08:59 +00:00
* @ param Definition [] $definitions An array of service definitions
2010-05-06 12:25:53 +01:00
*/
public function setDefinitions ( array $definitions )
2010-03-01 17:37:22 +00:00
{
2010-05-06 12:25:53 +01:00
$this -> definitions = array ();
$this -> addDefinitions ( $definitions );
2010-03-01 17:37:22 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Gets all service definitions .
*
2012-09-07 23:56:45 +01:00
* @ return Definition [] An array of Definition instances
2010-05-06 12:25:53 +01:00
*/
public function getDefinitions ()
2010-01-04 14:26:20 +00:00
{
2010-05-06 12:25:53 +01:00
return $this -> definitions ;
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
/**
* Sets a service definition .
*
2012-05-15 21:19:31 +01:00
* @ param string $id The service identifier
* @ param Definition $definition A Definition instance
2010-10-02 15:47:35 +01:00
*
2012-09-07 23:56:45 +01:00
* @ return Definition the service definition
*
2016-08-19 13:18:08 +01:00
* @ throws BadMethodCallException When this ContainerBuilder is compiled
2010-05-06 12:25:53 +01:00
*/
public function setDefinition ( $id , Definition $definition )
{
2016-08-19 13:18:08 +01:00
if ( $this -> isCompiled ()) {
throw new BadMethodCallException ( 'Adding definition to a compiled container is not allowed' );
2010-10-02 15:47:35 +01:00
}
2017-01-10 07:04:52 +00:00
$id = $this -> normalizeId ( $id );
2011-01-03 08:07:06 +00:00
2013-06-04 14:12:01 +01:00
unset ( $this -> aliasDefinitions [ $id ]);
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
return $this -> definitions [ $id ] = $definition ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns true if a service definition exists under the given identifier .
*
2012-05-15 21:19:31 +01:00
* @ param string $id The service identifier
2010-05-06 12:25:53 +01:00
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the service definition exists , false otherwise
2010-05-06 12:25:53 +01:00
*/
public function hasDefinition ( $id )
2010-01-04 14:26:20 +00:00
{
2017-01-10 07:04:52 +00:00
return isset ( $this -> definitions [ $this -> normalizeId ( $id )]);
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Gets a service definition .
*
2012-05-15 21:19:31 +01:00
* @ param string $id The service identifier
2010-05-06 12:25:53 +01:00
*
* @ return Definition A Definition instance
*
2016-02-07 18:10:24 +00:00
* @ throws ServiceNotFoundException if the service definition does not exist
2010-05-06 12:25:53 +01:00
*/
public function getDefinition ( $id )
2010-01-04 14:26:20 +00:00
{
2017-01-10 07:04:52 +00:00
$id = $this -> normalizeId ( $id );
2011-01-03 08:07:06 +00:00
2016-08-21 13:01:27 +01:00
if ( ! isset ( $this -> definitions [ $id ])) {
2016-02-07 18:10:24 +00:00
throw new ServiceNotFoundException ( $id );
2010-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
return $this -> definitions [ $id ];
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
2010-07-15 14:11:33 +01:00
/**
* Gets a service definition by id or alias .
*
* The method " unaliases " recursively to return a Definition instance .
*
2012-05-15 21:19:31 +01:00
* @ param string $id The service identifier or alias
2010-07-15 14:11:33 +01:00
*
* @ return Definition A Definition instance
*
2016-02-07 18:10:24 +00:00
* @ throws ServiceNotFoundException if the service definition does not exist
2010-07-15 14:11:33 +01:00
*/
public function findDefinition ( $id )
{
2017-01-10 07:04:52 +00:00
$id = $this -> normalizeId ( $id );
2015-04-25 17:37:49 +01:00
2017-12-06 16:27:14 +00:00
$seen = array ();
2015-04-25 17:37:49 +01:00
while ( isset ( $this -> aliasDefinitions [ $id ])) {
$id = ( string ) $this -> aliasDefinitions [ $id ];
2017-12-06 16:27:14 +00:00
if ( isset ( $seen [ $id ])) {
throw new ServiceCircularReferenceException ( $id , array_keys ( $seen ));
}
$seen [ $id ] = true ;
2010-07-15 14:11:33 +01:00
}
return $this -> getDefinition ( $id );
}
2010-05-06 12:25:53 +01:00
/**
* Creates a service for a service definition .
*
2012-05-15 21:19:31 +01:00
* @ param Definition $definition A service definition instance
* @ param string $id The service identifier
2014-04-12 18:54:57 +01:00
* @ param bool $tryProxy Whether to try proxying the service with a lazy proxy
2010-05-06 12:25:53 +01:00
*
2012-09-07 23:56:45 +01:00
* @ return object The service described by the service definition
2010-05-06 12:25:53 +01:00
*
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
2011-12-04 23:51:22 +00:00
* @ throws InvalidArgumentException When configure callable is not callable
2010-05-06 12:25:53 +01:00
*/
2017-11-23 11:01:29 +00:00
private function createService ( Definition $definition , \SplObjectStorage $inlinedDefinitions , $id = null , $tryProxy = true )
2010-01-04 14:26:20 +00:00
{
2017-11-23 08:59:26 +00:00
if ( null === $id && isset ( $inlinedDefinitions [ $definition ])) {
return $inlinedDefinitions [ $definition ];
}
2016-11-28 07:50:06 +00:00
if ( $definition instanceof ChildDefinition ) {
2016-11-24 10:37:50 +00:00
throw new RuntimeException ( sprintf ( 'Constructing service "%s" from a parent definition is not supported at build time.' , $id ));
2016-11-17 00:37:44 +00:00
}
2013-01-21 22:08:41 +00:00
if ( $definition -> isSynthetic ()) {
2013-01-22 07:14:10 +00:00
throw new RuntimeException ( sprintf ( 'You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.' , $id ));
2013-01-21 22:08:41 +00:00
}
2015-08-10 11:33:01 +01:00
if ( $definition -> isDeprecated ()) {
2015-08-10 13:35:12 +01:00
@ trigger_error ( $definition -> getDeprecationMessage ( $id ), E_USER_DEPRECATED );
2015-08-10 11:33:01 +01:00
}
2013-03-29 23:21:12 +00:00
if ( $tryProxy && $definition -> isLazy ()) {
$proxy = $this
-> getProxyInstantiator ()
-> instantiateProxy (
2014-12-03 22:04:33 +00:00
$this ,
2013-03-29 23:21:12 +00:00
$definition ,
2017-11-23 11:01:29 +00:00
$id , function () use ( $definition , $inlinedDefinitions , $id ) {
return $this -> createService ( $definition , $inlinedDefinitions , $id , false );
2013-03-29 23:21:12 +00:00
}
);
2017-11-23 08:59:26 +00:00
$this -> shareService ( $definition , $proxy , $id , $inlinedDefinitions );
2013-03-29 23:21:12 +00:00
return $proxy ;
}
2012-06-26 18:33:57 +01:00
$parameterBag = $this -> getParameterBag ();
2010-05-07 15:09:11 +01:00
if ( null !== $definition -> getFile ()) {
2012-06-26 18:33:57 +01:00
require_once $parameterBag -> resolveValue ( $definition -> getFile ());
2010-01-04 14:26:20 +00:00
}
2017-11-23 08:59:26 +00:00
$arguments = $this -> doResolveServices ( $parameterBag -> unescapeValue ( $parameterBag -> resolveValue ( $definition -> getArguments ())), $inlinedDefinitions );
2010-05-06 12:25:53 +01:00
2014-12-23 17:59:20 +00:00
if ( null !== $factory = $definition -> getFactory ()) {
if ( is_array ( $factory )) {
2017-11-23 08:59:26 +00:00
$factory = array ( $this -> doResolveServices ( $parameterBag -> resolveValue ( $factory [ 0 ]), $inlinedDefinitions ), $factory [ 1 ]);
2014-12-23 17:59:20 +00:00
} elseif ( ! is_string ( $factory )) {
2014-09-27 17:06:30 +01:00
throw new RuntimeException ( sprintf ( 'Cannot create service "%s" because of invalid factory' , $id ));
}
2014-12-23 17:59:20 +00:00
$service = call_user_func_array ( $factory , $arguments );
2015-09-29 14:40:49 +01:00
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 );
}
}
2010-05-07 15:09:11 +01:00
} else {
2017-03-30 09:29:34 +01:00
$r = new \ReflectionClass ( $class = $parameterBag -> resolveValue ( $definition -> getClass ()));
2010-07-04 17:56:48 +01:00
2017-03-25 13:44:42 +00:00
$service = null === $r -> getConstructor () ? $r -> newInstance () : $r -> newInstanceArgs ( $arguments );
2017-03-30 09:29:34 +01:00
// don't trigger deprecations for internal uses
// @deprecated since version 3.3, to be removed in 4.0 along with the deprecated class
$deprecationWhitelist = array ( 'event_dispatcher' => ContainerAwareEventDispatcher :: class );
2017-03-25 13:44:42 +00:00
2017-03-30 09:29:34 +01:00
if ( ! $definition -> isDeprecated () && 0 < strpos ( $r -> getDocComment (), " \n * @deprecated " ) && ( ! isset ( $deprecationWhitelist [ $id ]) || $deprecationWhitelist [ $id ] !== $class )) {
2015-09-29 14:40:49 +01:00
@ 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-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2013-03-29 23:21:12 +00:00
if ( $tryProxy || ! $definition -> isLazy ()) {
// share only if proxying failed, or if not a proxy
2017-11-23 08:59:26 +00:00
$this -> shareService ( $definition , $service , $id , $inlinedDefinitions );
2010-05-06 12:25:53 +01:00
}
2010-01-04 14:26:20 +00:00
2017-11-23 08:59:26 +00:00
$properties = $this -> doResolveServices ( $parameterBag -> unescapeValue ( $parameterBag -> resolveValue ( $definition -> getProperties ())), $inlinedDefinitions );
2011-05-03 22:46:02 +01:00
foreach ( $properties as $name => $value ) {
2011-06-27 07:28:54 +01:00
$service -> $name = $value ;
2011-05-03 22:46:02 +01:00
}
2016-11-19 11:21:18 +00:00
foreach ( $definition -> getMethodCalls () as $call ) {
2017-11-23 08:59:26 +00:00
$this -> callMethod ( $service , $call , $inlinedDefinitions );
2016-11-19 11:21:18 +00:00
}
2010-05-07 15:09:11 +01:00
if ( $callable = $definition -> getConfigurator ()) {
2012-11-18 20:27:35 +00:00
if ( is_array ( $callable )) {
2015-06-02 14:43:57 +01:00
$callable [ 0 ] = $parameterBag -> resolveValue ( $callable [ 0 ]);
if ( $callable [ 0 ] instanceof Reference ) {
$callable [ 0 ] = $this -> get (( string ) $callable [ 0 ], $callable [ 0 ] -> getInvalidBehavior ());
} elseif ( $callable [ 0 ] instanceof Definition ) {
2017-11-23 08:59:26 +00:00
$callable [ 0 ] = $this -> createService ( $callable [ 0 ], $inlinedDefinitions );
2015-06-02 14:43:57 +01:00
}
2010-05-06 12:25:53 +01:00
}
2010-05-07 15:09:11 +01:00
if ( ! is_callable ( $callable )) {
2011-12-04 23:51:22 +00:00
throw new InvalidArgumentException ( sprintf ( 'The configure callable for class "%s" is not a callable.' , get_class ( $service )));
2010-05-06 12:25:53 +01:00
}
call_user_func ( $callable , $service );
}
return $service ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
2013-09-02 11:46:47 +01:00
* Replaces service references by the real service instance and evaluates expressions .
2010-05-06 12:25:53 +01:00
*
2012-05-15 21:19:31 +01:00
* @ param mixed $value A value
2010-05-06 12:25:53 +01:00
*
2013-09-02 11:46:47 +01:00
* @ return mixed The same value with all service references replaced by
* the real service instances and all expressions evaluated
2010-05-06 12:25:53 +01:00
*/
public function resolveServices ( $value )
2017-11-23 08:59:26 +00:00
{
return $this -> doResolveServices ( $value , new \SplObjectStorage ());
}
private function doResolveServices ( $value , \SplObjectStorage $inlinedDefinitions )
2010-01-04 14:26:20 +00:00
{
2010-05-07 15:09:11 +01:00
if ( is_array ( $value )) {
2015-10-22 16:08:54 +01:00
foreach ( $value as $k => $v ) {
2017-11-23 08:59:26 +00:00
$value [ $k ] = $this -> doResolveServices ( $v , $inlinedDefinitions );
2015-10-22 16:08:54 +01:00
}
2017-02-26 17:31:03 +00:00
} elseif ( $value instanceof ServiceClosureArgument ) {
$reference = $value -> getValues ()[ 0 ];
$value = function () use ( $reference ) {
return $this -> resolveServices ( $reference );
};
2016-12-13 17:37:51 +00:00
} elseif ( $value instanceof IteratorArgument ) {
2017-04-21 10:31:17 +01:00
$value = new RewindableGenerator ( function () use ( $value ) {
2016-12-13 17:37:51 +00:00
foreach ( $value -> getValues () as $k => $v ) {
foreach ( self :: getServiceConditionals ( $v ) as $s ) {
if ( ! $this -> has ( $s )) {
continue 2 ;
}
}
2017-04-21 10:31:17 +01:00
yield $k => $this -> resolveServices ( $v );
2016-12-13 17:37:51 +00:00
}
2017-01-29 19:10:36 +00:00
}, function () use ( $value ) {
$count = 0 ;
foreach ( $value -> getValues () as $v ) {
foreach ( self :: getServiceConditionals ( $v ) as $s ) {
if ( ! $this -> has ( $s )) {
continue 2 ;
}
}
++ $count ;
}
return $count ;
2016-12-13 17:37:51 +00:00
});
2012-11-18 20:27:35 +00:00
} elseif ( $value instanceof Reference ) {
2010-06-27 17:28:29 +01:00
$value = $this -> get (( string ) $value , $value -> getInvalidBehavior ());
2012-11-18 20:27:35 +00:00
} elseif ( $value instanceof Definition ) {
2017-11-23 08:59:26 +00:00
$value = $this -> createService ( $value , $inlinedDefinitions );
2013-09-02 11:46:47 +01:00
} elseif ( $value instanceof Expression ) {
2013-09-04 16:17:08 +01:00
$value = $this -> getExpressionLanguage () -> evaluate ( $value , array ( 'container' => $this ));
2010-05-06 12:25:53 +01:00
}
return $value ;
2010-01-04 14:26:20 +00:00
}
2010-05-06 12:25:53 +01:00
/**
2010-08-05 06:34:53 +01:00
* Returns service ids for a given tag .
2010-05-06 12:25:53 +01:00
*
2013-05-02 18:49:45 +01:00
* Example :
*
* $container -> register ( 'foo' ) -> addTag ( 'my.tag' , array ( 'hello' => 'world' ));
*
* $serviceIds = $container -> findTaggedServiceIds ( 'my.tag' );
* foreach ( $serviceIds as $serviceId => $tags ) {
* foreach ( $tags as $tag ) {
* echo $tag [ 'hello' ];
* }
* }
*
2017-04-12 10:13:52 +01:00
* @ param string $name
* @ param bool $throwOnAbstract
2010-05-06 12:25:53 +01:00
*
2016-06-28 06:50:50 +01:00
* @ return array An array of tags with the tagged service as key , holding a list of attribute arrays
2010-05-06 12:25:53 +01:00
*/
2017-04-12 10:13:52 +01:00
public function findTaggedServiceIds ( $name , $throwOnAbstract = false )
2010-02-09 08:45:23 +00:00
{
2014-10-29 09:15:52 +00:00
$this -> usedTags [] = $name ;
2010-08-05 06:34:53 +01:00
$tags = array ();
2010-05-07 15:09:11 +01:00
foreach ( $this -> getDefinitions () as $id => $definition ) {
2013-05-02 18:49:45 +01:00
if ( $definition -> hasTag ( $name )) {
2017-04-12 10:13:52 +01:00
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-05-06 12:25:53 +01:00
}
}
2010-08-05 06:34:53 +01:00
return $tags ;
2010-02-09 08:45:23 +00:00
}
2012-09-16 16:15:14 +01:00
/**
* Returns all tags the defined services use .
*
* @ return array An array of tags
*/
public function findTags ()
{
$tags = array ();
foreach ( $this -> getDefinitions () as $id => $definition ) {
$tags = array_merge ( array_keys ( $definition -> getTags ()), $tags );
}
return array_unique ( $tags );
}
2014-10-29 09:15:52 +00:00
/**
2015-09-28 12:00:47 +01:00
* Returns all tags not queried by findTaggedServiceIds .
2014-10-29 09:15:52 +00:00
*
2015-09-28 12:00:47 +01:00
* @ return string [] An array of tags
2014-10-29 09:15:52 +00:00
*/
public function findUnusedTags ()
{
2015-09-28 12:00:47 +01:00
return array_values ( array_diff ( $this -> findTags (), $this -> usedTags ));
2014-10-29 09:15:52 +00:00
}
2014-09-23 16:21:57 +01:00
public function addExpressionLanguageProvider ( ExpressionFunctionProviderInterface $provider )
{
$this -> expressionLanguageProviders [] = $provider ;
}
2014-12-17 09:56:43 +00:00
/**
* @ return ExpressionFunctionProviderInterface []
*/
public function getExpressionLanguageProviders ()
{
return $this -> expressionLanguageProviders ;
}
2017-03-31 11:45:36 +01:00
/**
* Returns a ChildDefinition that will be used for autoconfiguring the interface / class .
*
* @ param string $interface The class or interface to match
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
*
2017-03-31 11:45:36 +01:00
* @ return ChildDefinition
*/
public function registerForAutoconfiguration ( $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 ( '' );
2017-03-31 11:45:36 +01:00
}
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 ];
2017-03-31 11:45:36 +01:00
}
/**
* 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 ()
2017-03-31 11:45:36 +01:00
{
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 ;
2017-03-31 11:45:36 +01:00
}
2016-08-12 19:34:27 +01:00
/**
2016-11-29 17:32:01 +00:00
* Resolves env parameter placeholders in a string or an array .
2016-08-12 19:34:27 +01:00
*
2016-11-24 09:31:58 +00:00
* @ 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
2016-08-12 19:34:27 +01:00
*
2017-11-23 12:03:37 +00:00
* @ return mixed The value with env parameters resolved if a string or an array is passed
2016-08-12 19:34:27 +01:00
*/
2016-11-29 17:32:01 +00:00
public function resolveEnvPlaceholders ( $value , $format = null , array & $usedEnvs = null )
2016-08-12 19:34:27 +01:00
{
if ( null === $format ) {
$format = '%%env(%s)%%' ;
}
2016-11-29 17:32:01 +00:00
if ( is_array ( $value )) {
$result = array ();
foreach ( $value as $k => $v ) {
$result [ $this -> resolveEnvPlaceholders ( $k , $format , $usedEnvs )] = $this -> resolveEnvPlaceholders ( $v , $format , $usedEnvs );
}
return $result ;
}
if ( ! is_string ( $value )) {
return $value ;
}
$bag = $this -> getParameterBag ();
2016-11-24 09:31:58 +00:00
if ( true === $format ) {
$value = $bag -> resolveValue ( $value );
}
2016-11-29 17:32:01 +00:00
$envPlaceholders = $bag instanceof EnvPlaceholderParameterBag ? $bag -> getEnvPlaceholders () : $this -> envPlaceholders ;
2016-08-12 19:34:27 +01:00
foreach ( $envPlaceholders as $env => $placeholders ) {
foreach ( $placeholders as $placeholder ) {
2016-11-29 17:32:01 +00:00
if ( false !== stripos ( $value , $placeholder )) {
2017-08-18 16:51:40 +01:00
if ( true === $format ) {
$resolved = $bag -> escapeValue ( $this -> getEnv ( $env ));
} else {
2016-11-24 09:31:58 +00:00
$resolved = sprintf ( $format , $env );
}
2017-08-18 16:51:40 +01:00
if ( $placeholder === $value ) {
$value = $resolved ;
} 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 ), $value ));
}
$value = str_ireplace ( $placeholder , $resolved , $value );
}
2016-08-12 19:34:27 +01:00
$usedEnvs [ $env ] = $env ;
$this -> envCounters [ $env ] = isset ( $this -> envCounters [ $env ]) ? 1 + $this -> envCounters [ $env ] : 1 ;
}
}
}
2016-11-29 17:32:01 +00:00
return $value ;
2016-08-12 19:34:27 +01:00
}
/**
* 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 ;
}
2017-01-10 07:04:52 +00:00
/**
* @ internal
*/
public function getNormalizedIds ()
{
$normalizedIds = array ();
foreach ( $this -> normalizedIds as $k => $v ) {
if ( $v !== ( string ) $k ) {
$normalizedIds [ $k ] = $v ;
}
}
return $normalizedIds ;
}
2017-01-24 18:16:15 +00:00
/**
* @ final
*/
public function log ( CompilerPassInterface $pass , $message )
{
$this -> getCompiler () -> log ( $pass , $message );
}
2011-02-13 18:06:41 +00:00
/**
* Returns the Service Conditionals .
*
2016-06-28 06:50:50 +01:00
* @ 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
*/
2012-07-09 13:50:58 +01:00
public static function getServiceConditionals ( $value )
2010-05-06 12:25:53 +01:00
{
$services = array ();
2010-02-09 08:45:23 +00:00
2010-05-07 15:09:11 +01:00
if ( is_array ( $value )) {
2010-05-08 14:32:30 +01:00
foreach ( $value as $v ) {
2010-05-06 12:25:53 +01:00
$services = array_unique ( array_merge ( $services , self :: getServiceConditionals ( $v )));
}
2017-09-07 10:04:22 +01:00
} elseif ( $value instanceof Reference && ContainerInterface :: IGNORE_ON_INVALID_REFERENCE === $value -> getInvalidBehavior ()) {
2010-05-06 12:25:53 +01:00
$services [] = ( string ) $value ;
}
2010-01-04 14:26:20 +00:00
2010-05-06 12:25:53 +01:00
return $services ;
2010-01-04 14:26:20 +00:00
}
2013-02-06 20:42:01 +00:00
2017-08-18 16:51:40 +01:00
/**
* { @ inheritdoc }
*/
protected function getEnv ( $name )
{
$value = parent :: getEnv ( $name );
if ( ! is_string ( $value ) || ! $this -> getParameterBag () instanceof EnvPlaceholderParameterBag ) {
return $value ;
}
foreach ( $this -> getParameterBag () -> getEnvPlaceholders () as $env => $placeholders ) {
if ( isset ( $placeholders [ $value ])) {
$bag = new ParameterBag ( $this -> getParameterBag () -> all ());
return $bag -> unescapeValue ( $bag -> get ( " env( $name ) " ));
}
}
return $value ;
}
2013-03-29 23:21:12 +00:00
/**
* Retrieves the currently set proxy instantiator or instantiates one .
*
* @ return InstantiatorInterface
*/
private function getProxyInstantiator ()
{
if ( ! $this -> proxyInstantiator ) {
$this -> proxyInstantiator = new RealServiceInstantiator ();
}
return $this -> proxyInstantiator ;
}
2017-11-23 08:59:26 +00:00
private function callMethod ( $service , $call , \SplObjectStorage $inlinedDefinitions )
2013-02-06 20:42:01 +00:00
{
$services = self :: getServiceConditionals ( $call [ 1 ]);
foreach ( $services as $s ) {
if ( ! $this -> has ( $s )) {
return ;
}
}
2017-11-23 08:59:26 +00:00
call_user_func_array ( array ( $service , $call [ 0 ]), $this -> doResolveServices ( $this -> getParameterBag () -> unescapeValue ( $this -> getParameterBag () -> resolveValue ( $call [ 1 ])), $inlinedDefinitions ));
2013-02-06 20:42:01 +00:00
}
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
*
2017-01-10 10:21:41 +00:00
* @ param Definition $definition
2017-04-21 10:31:17 +01:00
* @ param object $service
2017-01-10 10:21:41 +00:00
* @ param string | null $id
2013-03-29 23:21:12 +00:00
*/
2017-11-23 08:59:26 +00:00
private function shareService ( Definition $definition , $service , $id , \SplObjectStorage $inlinedDefinitions )
2013-03-29 23:21:12 +00:00
{
2017-11-23 11:01:29 +00:00
if ( ! $definition -> isShared ()) {
2017-11-23 08:59:26 +00:00
return ;
}
if ( null === $id ) {
$inlinedDefinitions [ $definition ] = $service ;
} else {
2017-01-10 07:04:52 +00:00
$this -> services [ $this -> normalizeId ( $id )] = $service ;
2013-03-29 23:21:12 +00:00
}
}
2013-09-02 11:46:47 +01:00
private function getExpressionLanguage ()
{
if ( null === $this -> expressionLanguage ) {
if ( ! class_exists ( 'Symfony\Component\ExpressionLanguage\ExpressionLanguage' )) {
throw new RuntimeException ( 'Unable to use expressions as the Symfony ExpressionLanguage component is not installed.' );
}
2014-09-23 16:21:57 +01:00
$this -> expressionLanguage = new ExpressionLanguage ( null , $this -> expressionLanguageProviders );
2013-09-02 11:46:47 +01:00
}
return $this -> expressionLanguage ;
}
2017-02-02 08:31:13 +00:00
private function inVendors ( $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
}