View source
<?php
namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
class ContainerBuilder extends Container implements TaggedContainerInterface {
private $extensions = array();
private $extensionsByNs = array();
private $definitions = array();
private $obsoleteDefinitions = array();
private $aliasDefinitions = array();
private $resources = array();
private $extensionConfigs = array();
private $compiler;
private $trackResources = true;
private $proxyInstantiator;
private $expressionLanguage;
private $expressionLanguageProviders = array();
public function setResourceTracking($track) {
$this->trackResources = (bool) $track;
}
public function isTrackingResources() {
return $this->trackResources;
}
public function setProxyInstantiator(InstantiatorInterface $proxyInstantiator) {
$this->proxyInstantiator = $proxyInstantiator;
}
public function registerExtension(ExtensionInterface $extension) {
$this->extensions[$extension
->getAlias()] = $extension;
if (false !== $extension
->getNamespace()) {
$this->extensionsByNs[$extension
->getNamespace()] = $extension;
}
}
public function getExtension($name) {
if (isset($this->extensions[$name])) {
return $this->extensions[$name];
}
if (isset($this->extensionsByNs[$name])) {
return $this->extensionsByNs[$name];
}
throw new LogicException(sprintf('Container extension "%s" is not registered', $name));
}
public function getExtensions() {
return $this->extensions;
}
public function hasExtension($name) {
return isset($this->extensions[$name]) || isset($this->extensionsByNs[$name]);
}
public function getResources() {
return array_unique($this->resources);
}
public function addResource(ResourceInterface $resource) {
if (!$this->trackResources) {
return $this;
}
$this->resources[] = $resource;
return $this;
}
public function setResources(array $resources) {
if (!$this->trackResources) {
return $this;
}
$this->resources = $resources;
return $this;
}
public function addObjectResource($object) {
if ($this->trackResources) {
$this
->addClassResource(new \ReflectionClass($object));
}
return $this;
}
public function addClassResource(\ReflectionClass $class) {
if (!$this->trackResources) {
return $this;
}
do {
$this
->addResource(new FileResource($class
->getFileName()));
} while ($class = $class
->getParentClass());
return $this;
}
public function loadFromExtension($extension, array $values = array()) {
if ($this
->isFrozen()) {
throw new BadMethodCallException('Cannot load from an extension on a frozen container.');
}
$namespace = $this
->getExtension($extension)
->getAlias();
$this->extensionConfigs[$namespace][] = $values;
return $this;
}
public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION) {
$this
->getCompiler()
->addPass($pass, $type);
$this
->addObjectResource($pass);
return $this;
}
public function getCompilerPassConfig() {
return $this
->getCompiler()
->getPassConfig();
}
public function getCompiler() {
if (null === $this->compiler) {
$this->compiler = new Compiler();
}
return $this->compiler;
}
public function getScopes() {
return $this->scopes;
}
public function getScopeChildren() {
return $this->scopeChildren;
}
public function set($id, $service, $scope = self::SCOPE_CONTAINER) {
$id = strtolower($id);
if ($this
->isFrozen()) {
if (!isset($this->definitions[$id]) && !isset($this->obsoleteDefinitions[$id]) || isset($this->definitions[$id]) && !$this->definitions[$id]
->isSynthetic() || isset($this->obsoleteDefinitions[$id]) && !$this->obsoleteDefinitions[$id]
->isSynthetic()) {
throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
}
}
if (isset($this->definitions[$id])) {
$this->obsoleteDefinitions[$id] = $this->definitions[$id];
}
unset($this->definitions[$id], $this->aliasDefinitions[$id]);
parent::set($id, $service, $scope);
if (isset($this->obsoleteDefinitions[$id]) && $this->obsoleteDefinitions[$id]
->isSynchronized(false)) {
$this
->synchronize($id);
}
}
public function removeDefinition($id) {
unset($this->definitions[strtolower($id)]);
}
public function has($id) {
$id = strtolower($id);
return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || parent::has($id);
}
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
$id = strtolower($id);
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
return $service;
}
if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) {
return $this
->get($this->aliasDefinitions[$id]);
}
try {
$definition = $this
->getDefinition($id);
} catch (InvalidArgumentException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return;
}
throw $e;
}
$this->loading[$id] = true;
try {
$service = $this
->createService($definition, $id);
} catch (\Exception $e) {
unset($this->loading[$id]);
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return;
}
throw $e;
}
unset($this->loading[$id]);
return $service;
}
public function merge(ContainerBuilder $container) {
if ($this
->isFrozen()) {
throw new BadMethodCallException('Cannot merge on a frozen container.');
}
$this
->addDefinitions($container
->getDefinitions());
$this
->addAliases($container
->getAliases());
$this
->getParameterBag()
->add($container
->getParameterBag()
->all());
if ($this->trackResources) {
foreach ($container
->getResources() as $resource) {
$this
->addResource($resource);
}
}
foreach ($this->extensions as $name => $extension) {
if (!isset($this->extensionConfigs[$name])) {
$this->extensionConfigs[$name] = array();
}
$this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container
->getExtensionConfig($name));
}
}
public function getExtensionConfig($name) {
if (!isset($this->extensionConfigs[$name])) {
$this->extensionConfigs[$name] = array();
}
return $this->extensionConfigs[$name];
}
public function prependExtensionConfig($name, array $config) {
if (!isset($this->extensionConfigs[$name])) {
$this->extensionConfigs[$name] = array();
}
array_unshift($this->extensionConfigs[$name], $config);
}
public function compile() {
$compiler = $this
->getCompiler();
if ($this->trackResources) {
foreach ($compiler
->getPassConfig()
->getPasses() as $pass) {
$this
->addObjectResource($pass);
}
}
$compiler
->compile($this);
if ($this->trackResources) {
foreach ($this->definitions as $definition) {
if ($definition
->isLazy() && ($class = $definition
->getClass()) && class_exists($class)) {
$this
->addClassResource(new \ReflectionClass($class));
}
}
}
$this->extensionConfigs = array();
parent::compile();
}
public function getServiceIds() {
return array_unique(array_merge(array_keys($this
->getDefinitions()), array_keys($this->aliasDefinitions), parent::getServiceIds()));
}
public function addAliases(array $aliases) {
foreach ($aliases as $alias => $id) {
$this
->setAlias($alias, $id);
}
}
public function setAliases(array $aliases) {
$this->aliasDefinitions = array();
$this
->addAliases($aliases);
}
public function setAlias($alias, $id) {
$alias = strtolower($alias);
if (is_string($id)) {
$id = new Alias($id);
}
elseif (!$id instanceof Alias) {
throw new InvalidArgumentException('$id must be a string, or an Alias object.');
}
if ($alias === (string) $id) {
throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias));
}
unset($this->definitions[$alias]);
$this->aliasDefinitions[$alias] = $id;
}
public function removeAlias($alias) {
unset($this->aliasDefinitions[strtolower($alias)]);
}
public function hasAlias($id) {
return isset($this->aliasDefinitions[strtolower($id)]);
}
public function getAliases() {
return $this->aliasDefinitions;
}
public function getAlias($id) {
$id = strtolower($id);
if (!isset($this->aliasDefinitions[$id])) {
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
}
return $this->aliasDefinitions[$id];
}
public function register($id, $class = null) {
return $this
->setDefinition($id, new Definition($class));
}
public function addDefinitions(array $definitions) {
foreach ($definitions as $id => $definition) {
$this
->setDefinition($id, $definition);
}
}
public function setDefinitions(array $definitions) {
$this->definitions = array();
$this
->addDefinitions($definitions);
}
public function getDefinitions() {
return $this->definitions;
}
public function setDefinition($id, Definition $definition) {
if ($this
->isFrozen()) {
throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
}
$id = strtolower($id);
unset($this->aliasDefinitions[$id]);
return $this->definitions[$id] = $definition;
}
public function hasDefinition($id) {
return array_key_exists(strtolower($id), $this->definitions);
}
public function getDefinition($id) {
$id = strtolower($id);
if (!array_key_exists($id, $this->definitions)) {
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
}
return $this->definitions[$id];
}
public function findDefinition($id) {
$id = strtolower($id);
while (isset($this->aliasDefinitions[$id])) {
$id = (string) $this->aliasDefinitions[$id];
}
return $this
->getDefinition($id);
}
public function createService(Definition $definition, $id, $tryProxy = true) {
if ($definition
->isSynthetic()) {
throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id));
}
if ($tryProxy && $definition
->isLazy()) {
$container = $this;
$proxy = $this
->getProxyInstantiator()
->instantiateProxy($container, $definition, $id, function () use ($definition, $id, $container) {
return $container
->createService($definition, $id, false);
});
$this
->shareService($definition, $proxy, $id);
return $proxy;
}
$parameterBag = $this
->getParameterBag();
if (null !== $definition
->getFile()) {
require_once $parameterBag
->resolveValue($definition
->getFile());
}
$arguments = $this
->resolveServices($parameterBag
->unescapeValue($parameterBag
->resolveValue($definition
->getArguments())));
if (null !== ($factory = $definition
->getFactory())) {
if (is_array($factory)) {
$factory = array(
$this
->resolveServices($parameterBag
->resolveValue($factory[0])),
$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);
}
elseif (null !== $definition
->getFactoryMethod(false)) {
if (null !== $definition
->getFactoryClass(false)) {
$factory = $parameterBag
->resolveValue($definition
->getFactoryClass(false));
}
elseif (null !== $definition
->getFactoryService(false)) {
$factory = $this
->get($parameterBag
->resolveValue($definition
->getFactoryService(false)));
}
else {
throw new RuntimeException(sprintf('Cannot create service "%s" from factory method without a factory service or factory class.', $id));
}
$service = call_user_func_array(array(
$factory,
$definition
->getFactoryMethod(false),
), $arguments);
}
else {
$r = new \ReflectionClass($parameterBag
->resolveValue($definition
->getClass()));
$service = null === $r
->getConstructor() ? $r
->newInstance() : $r
->newInstanceArgs($arguments);
}
if ($tryProxy || !$definition
->isLazy()) {
$this
->shareService($definition, $service, $id);
}
foreach ($definition
->getMethodCalls() as $call) {
$this
->callMethod($service, $call);
}
$properties = $this
->resolveServices($parameterBag
->resolveValue($definition
->getProperties()));
foreach ($properties as $name => $value) {
$service->{$name} = $value;
}
if ($callable = $definition
->getConfigurator()) {
if (is_array($callable)) {
$callable[0] = $callable[0] instanceof Reference ? $this
->get((string) $callable[0]) : $parameterBag
->resolveValue($callable[0]);
}
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);
}
return $service;
}
public function resolveServices($value) {
if (is_array($value)) {
$value = array_map(array(
$this,
'resolveServices',
), $value);
}
elseif ($value instanceof Reference) {
$value = $this
->get((string) $value, $value
->getInvalidBehavior());
}
elseif ($value instanceof Definition) {
$value = $this
->createService($value, null);
}
elseif ($value instanceof Expression) {
$value = $this
->getExpressionLanguage()
->evaluate($value, array(
'container' => $this,
));
}
return $value;
}
public function findTaggedServiceIds($name) {
$tags = array();
foreach ($this
->getDefinitions() as $id => $definition) {
if ($definition
->hasTag($name)) {
$tags[$id] = $definition
->getTag($name);
}
}
return $tags;
}
public function findTags() {
$tags = array();
foreach ($this
->getDefinitions() as $id => $definition) {
$tags = array_merge(array_keys($definition
->getTags()), $tags);
}
return array_unique($tags);
}
public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider) {
$this->expressionLanguageProviders[] = $provider;
}
public function getExpressionLanguageProviders() {
return $this->expressionLanguageProviders;
}
public static function getServiceConditionals($value) {
$services = array();
if (is_array($value)) {
foreach ($value as $v) {
$services = array_unique(array_merge($services, self::getServiceConditionals($v)));
}
}
elseif ($value instanceof Reference && $value
->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
$services[] = (string) $value;
}
return $services;
}
private function getProxyInstantiator() {
if (!$this->proxyInstantiator) {
$this->proxyInstantiator = new RealServiceInstantiator();
}
return $this->proxyInstantiator;
}
private function synchronize($id) {
if ('request' !== $id) {
trigger_error('The ' . __METHOD__ . ' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);
}
foreach ($this->definitions as $definitionId => $definition) {
if (!$this
->initialized($definitionId)) {
continue;
}
foreach ($definition
->getMethodCalls() as $call) {
foreach ($call[1] as $argument) {
if ($argument instanceof Reference && $id == (string) $argument) {
$this
->callMethod($this
->get($definitionId), $call);
}
}
}
}
}
private function callMethod($service, $call) {
$services = self::getServiceConditionals($call[1]);
foreach ($services as $s) {
if (!$this
->has($s)) {
return;
}
}
call_user_func_array(array(
$service,
$call[0],
), $this
->resolveServices($this
->getParameterBag()
->resolveValue($call[1])));
}
private function shareService(Definition $definition, $service, $id) {
if (self::SCOPE_PROTOTYPE !== ($scope = $definition
->getScope())) {
if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
throw new InactiveScopeException($id, $scope);
}
$this->services[$lowerId = strtolower($id)] = $service;
if (self::SCOPE_CONTAINER !== $scope) {
$this->scopedServices[$scope][$lowerId] = $service;
}
}
}
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.');
}
$this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
}
return $this->expressionLanguage;
}
}