namespace Symfony\Component\DependencyInjection;
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;
+use Symfony\Component\Config\Resource\FileResource;
+use Symfony\Component\Config\Resource\GlobResource;
+use Symfony\Component\Config\Resource\ReflectionClassResource;
+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\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
-use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
-use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
-use Symfony\Component\Config\Resource\ClassExistenceResource;
-use Symfony\Component\Config\Resource\ComposerResource;
-use Symfony\Component\Config\Resource\DirectoryResource;
-use Symfony\Component\Config\Resource\FileExistenceResource;
-use Symfony\Component\Config\Resource\FileResource;
-use Symfony\Component\Config\Resource\GlobResource;
-use Symfony\Component\Config\Resource\ReflectionClassResource;
-use Symfony\Component\Config\Resource\ResourceInterface;
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\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\ExpressionLanguage\Expression;
private $autoconfiguredInstanceof = array();
private $removedIds = array();
- private $alreadyLoading = array();
private static $internalTypes = array(
'int' => true,
public function addObjectResource($object)
{
if ($this->trackResources) {
- if (is_object($object)) {
- $object = get_class($object);
+ if (\is_object($object)) {
+ $object = \get_class($object);
}
if (!isset($this->classReflectors[$object])) {
$this->classReflectors[$object] = new \ReflectionClass($object);
if (is_dir($path)) {
if ($trackContents) {
- $this->addResource(new DirectoryResource($path, is_string($trackContents) ? $trackContents : null));
+ $this->addResource(new DirectoryResource($path, \is_string($trackContents) ? $trackContents : null));
} else {
$this->addResource(new GlobResource($path, '/*', false));
}
throw new BadMethodCallException('Cannot load from an extension on a compiled container.');
}
- if (func_num_args() < 2) {
+ if (\func_num_args() < 2) {
$values = array();
}
*/
public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/*, int $priority = 0*/)
{
- if (func_num_args() >= 3) {
+ if (\func_num_args() >= 3) {
$priority = func_get_arg(2);
} else {
- if (__CLASS__ !== get_class($this)) {
+ if (__CLASS__ !== \get_class($this)) {
$r = new \ReflectionMethod($this, __FUNCTION__);
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
@trigger_error(sprintf('Method %s() will have a third `int $priority = 0` argument in version 4.0. Not defining it is deprecated since Symfony 3.2.', __METHOD__), E_USER_DEPRECATED);
return $this->doGet($id, $invalidBehavior);
}
- private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = array())
+ private function doGet($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, array &$inlineServices = null, $isConstructorArgument = false)
{
$id = $this->normalizeId($id);
if (isset($inlineServices[$id])) {
return $inlineServices[$id];
}
- if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
- return parent::get($id, $invalidBehavior);
+ if (null === $inlineServices) {
+ $isConstructorArgument = true;
+ $inlineServices = array();
}
- if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
- return $service;
+ 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;
+ }
}
if (!isset($this->definitions[$id]) && isset($this->aliasDefinitions[$id])) {
- return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices);
+ return $this->doGet((string) $this->aliasDefinitions[$id], $invalidBehavior, $inlineServices, $isConstructorArgument);
}
try {
throw $e;
}
- $loading = isset($this->alreadyLoading[$id]) ? 'loading' : 'alreadyLoading';
- $this->{$loading}[$id] = true;
+ if ($isConstructorArgument) {
+ $this->loading[$id] = true;
+ }
try {
- $service = $this->createService($definition, $inlineServices, $id);
+ return $this->createService($definition, $inlineServices, $isConstructorArgument, $id);
} finally {
- unset($this->{$loading}[$id]);
+ if ($isConstructorArgument) {
+ unset($this->loading[$id]);
+ }
}
-
- return $service;
}
/**
* the parameters passed to the container constructor to have precedence
* over the loaded ones.
*
- * $container = new ContainerBuilder(array('foo' => 'bar'));
- * $loader = new LoaderXXX($container);
- * $loader->load('resource_name');
- * $container->register('foo', new stdClass());
+ * $container = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
+ * $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
*/
public function compile(/*$resolveEnvPlaceholders = false*/)
{
- if (1 <= func_num_args()) {
+ if (1 <= \func_num_args()) {
$resolveEnvPlaceholders = func_get_arg(0);
} else {
if (__CLASS__ !== static::class) {
{
$alias = $this->normalizeId($alias);
- if (is_string($id)) {
+ if (\is_string($id)) {
$id = new Alias($this->normalizeId($id));
} elseif (!$id instanceof Alias) {
throw new InvalidArgumentException('$id must be a string, or an Alias object.');
* an autowired definition.
*
* @param string $id The service identifier
- * @param null|string $class The service class
+ * @param string|null $class The service class
*
* @return Definition The created definition
*/
if (isset($seen[$id])) {
$seen = array_values($seen);
- $seen = array_slice($seen, array_search($id, $seen));
+ $seen = \array_slice($seen, array_search($id, $seen));
$seen[] = $id;
throw new ServiceCircularReferenceException($id, $seen);
* @throws RuntimeException When the service is a synthetic service
* @throws InvalidArgumentException When configure callable is not callable
*/
- private function createService(Definition $definition, array &$inlineServices, $id = null, $tryProxy = true)
+ private function createService(Definition $definition, array &$inlineServices, $isConstructorArgument = false, $id = null, $tryProxy = true)
{
if (null === $id && isset($inlineServices[$h = spl_object_hash($definition)])) {
return $inlineServices[$h];
@trigger_error($definition->getDeprecationMessage($id), E_USER_DEPRECATED);
}
- if ($tryProxy && $definition->isLazy()) {
- $proxy = $this
- ->getProxyInstantiator()
- ->instantiateProxy(
- $this,
- $definition,
- $id, function () use ($definition, &$inlineServices, $id) {
- return $this->createService($definition, $inlineServices, $id, false);
- }
- );
+ 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);
return $proxy;
require_once $parameterBag->resolveValue($definition->getFile());
}
- $arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())), $inlineServices);
-
- if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
- return $this->services[$id];
- }
+ $arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())), $inlineServices, $isConstructorArgument);
if (null !== $factory = $definition->getFactory()) {
- if (is_array($factory)) {
- $factory = array($this->doResolveServices($parameterBag->resolveValue($factory[0]), $inlineServices), $factory[1]);
- } elseif (!is_string($factory)) {
+ if (\is_array($factory)) {
+ $factory = array($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));
}
+ }
- $service = call_user_func_array($factory, $arguments);
+ if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
+ return $this->services[$id];
+ }
- if (!$definition->isDeprecated() && is_array($factory) && is_string($factory[0])) {
+ if (null !== $factory) {
+ $service = \call_user_func_array($factory, $arguments);
+
+ if (!$definition->isDeprecated() && \is_array($factory) && \is_string($factory[0])) {
$r = new \ReflectionClass($factory[0]);
if (0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
}
if ($callable = $definition->getConfigurator()) {
- if (is_array($callable)) {
+ if (\is_array($callable)) {
$callable[0] = $parameterBag->resolveValue($callable[0]);
if ($callable[0] instanceof Reference) {
}
}
- if (!is_callable($callable)) {
- throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
+ if (!\is_callable($callable)) {
+ throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', \get_class($service)));
}
- call_user_func($callable, $service);
+ \call_user_func($callable, $service);
}
return $service;
return $this->doResolveServices($value);
}
- private function doResolveServices($value, array &$inlineServices = array())
+ private function doResolveServices($value, array &$inlineServices = array(), $isConstructorArgument = false)
{
- if (is_array($value)) {
+ if (\is_array($value)) {
foreach ($value as $k => $v) {
- $value[$k] = $this->doResolveServices($v, $inlineServices);
+ $value[$k] = $this->doResolveServices($v, $inlineServices, $isConstructorArgument);
}
} elseif ($value instanceof ServiceClosureArgument) {
$reference = $value->getValues()[0];
return $count;
});
} elseif ($value instanceof Reference) {
- $value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices);
+ $value = $this->doGet((string) $value, $value->getInvalidBehavior(), $inlineServices, $isConstructorArgument);
} elseif ($value instanceof Definition) {
- $value = $this->createService($value, $inlineServices);
+ $value = $this->createService($value, $inlineServices, $isConstructorArgument);
} elseif ($value instanceof Parameter) {
$value = $this->getParameter((string) $value);
} elseif ($value instanceof Expression) {
*
* Example:
*
- * $container->register('foo')->addTag('my.tag', array('hello' => 'world'));
+ * $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'];
+ * $serviceIds = $container->findTaggedServiceIds('my.tag');
+ * foreach ($serviceIds as $serviceId => $tags) {
+ * foreach ($tags as $tag) {
+ * echo $tag['hello'];
+ * }
* }
- * }
*
* @param string $name
* @param bool $throwOnAbstract
$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)));
+ 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);
}
*/
public function log(CompilerPassInterface $pass, $message)
{
- $this->getCompiler()->log($pass, $message);
+ $this->getCompiler()->log($pass, $this->resolveEnvPlaceholders($message));
}
/**
{
$services = array();
- if (is_array($value)) {
+ if (\is_array($value)) {
foreach ($value as $v) {
$services = array_unique(array_merge($services, self::getServiceConditionals($v)));
}
{
$services = array();
- if (is_array($value)) {
+ if (\is_array($value)) {
foreach ($value as $v) {
$services = array_unique(array_merge($services, self::getInitializedConditionals($v)));
}
$value = parent::getEnv($name);
$bag = $this->getParameterBag();
- if (!is_string($value) || !$bag instanceof EnvPlaceholderParameterBag) {
+ if (!\is_string($value) || !$bag instanceof EnvPlaceholderParameterBag) {
return $value;
}
}
}
- /**
- * Retrieves the currently set proxy instantiator or instantiates one.
- *
- * @return InstantiatorInterface
- */
- private function getProxyInstantiator()
- {
- if (!$this->proxyInstantiator) {
- $this->proxyInstantiator = new RealServiceInstantiator();
- }
-
- return $this->proxyInstantiator;
- }
-
private function callMethod($service, $call, array &$inlineServices)
{
foreach (self::getServiceConditionals($call[1]) as $s) {
}
}
- call_user_func_array(array($service, $call[0]), $this->doResolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1])), $inlineServices));
+ \call_user_func_array(array($service, $call[0]), $this->doResolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1])), $inlineServices));
}
/**
if (null !== $id && $definition->isShared()) {
$this->services[$id] = $service;
- unset($this->loading[$id], $this->alreadyLoading[$id]);
+ unset($this->loading[$id]);
}
}
$path = realpath($path) ?: $path;
foreach ($this->vendors as $vendor) {
- if (0 === strpos($path, $vendor) && false !== strpbrk(substr($path, strlen($vendor), 1), '/'.DIRECTORY_SEPARATOR)) {
+ if (0 === strpos($path, $vendor) && false !== strpbrk(substr($path, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
return true;
}
}