<?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\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;
class ContainerBuilder extends Container implements TaggedContainerInterface {
private $extensions = array();
private $extensionsByNs = array();
private $definitions = array();
private $aliases = array();
private $resources = array();
private $extensionConfigs = array();
private $compiler;
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) {
$this->resources[] = $resource;
return $this;
}
public function setResources(array $resources) {
$this->resources = $resources;
return $this;
}
public function addObjectResource($object) {
$parent = new \ReflectionObject($object);
do {
$this
->addResource(new FileResource($parent
->getFileName()));
} while ($parent = $parent
->getParentClass());
}
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) {
if (null === $this->compiler) {
$this->compiler = new Compiler();
}
$this->compiler
->addPass($pass, $type);
$this
->addObjectResource($pass);
}
public function getCompilerPassConfig() {
if (null === $this->compiler) {
$this->compiler = new Compiler();
}
return $this->compiler
->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) {
if ($this
->isFrozen()) {
throw new BadMethodCallException('Setting service on a frozen container is not allowed');
}
$id = strtolower($id);
unset($this->definitions[$id], $this->aliases[$id]);
parent::set($id, $service, $scope);
}
public function removeDefinition($id) {
unset($this->definitions[strtolower($id)]);
}
public function has($id) {
$id = strtolower($id);
return isset($this->definitions[$id]) || isset($this->aliases[$id]) || parent::has($id);
}
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
$id = strtolower($id);
try {
return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
} catch (InvalidArgumentException $e) {
if (isset($this->loading[$id])) {
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
}
if (!$this
->hasDefinition($id) && isset($this->aliases[$id])) {
return $this
->get($this->aliases[$id]);
}
try {
$definition = $this
->getDefinition($id);
} catch (InvalidArgumentException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
}
$this->loading[$id] = true;
$service = $this
->createService($definition, $id);
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());
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 compile() {
if (null === $this->compiler) {
$this->compiler = new Compiler();
}
foreach ($this->compiler
->getPassConfig()
->getPasses() as $pass) {
$this
->addObjectResource($pass);
}
$this->compiler
->compile($this);
$this->extensionConfigs = array();
parent::compile();
}
public function getServiceIds() {
return array_unique(array_merge(array_keys($this
->getDefinitions()), array_keys($this->aliases), parent::getServiceIds()));
}
public function addAliases(array $aliases) {
foreach ($aliases as $alias => $id) {
$this
->setAlias($alias, $id);
}
}
public function setAliases(array $aliases) {
$this->aliases = 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 === strtolower($id)) {
throw new InvalidArgumentException('An alias can not reference itself, got a circular reference on "' . $alias . '".');
}
unset($this->definitions[$alias]);
$this->aliases[$alias] = $id;
}
public function removeAlias($alias) {
unset($this->aliases[strtolower($alias)]);
}
public function hasAlias($id) {
return isset($this->aliases[strtolower($id)]);
}
public function getAliases() {
return $this->aliases;
}
public function getAlias($id) {
$id = strtolower($id);
if (!$this
->hasAlias($id)) {
throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
}
return $this->aliases[$id];
}
public function register($id, $class = null) {
return $this
->setDefinition(strtolower($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->aliases[$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 (!$this
->hasDefinition($id)) {
throw new InvalidArgumentException(sprintf('The service definition "%s" does not exist.', $id));
}
return $this->definitions[$id];
}
public function findDefinition($id) {
while ($this
->hasAlias($id)) {
$id = (string) $this
->getAlias($id);
}
return $this
->getDefinition($id);
}
private function createService(Definition $definition, $id) {
$parameterBag = $this
->getParameterBag();
if (null !== $definition
->getFile()) {
require_once $parameterBag
->resolveValue($definition
->getFile());
}
$arguments = $this
->resolveServices($parameterBag
->unescapeValue($parameterBag
->resolveValue($definition
->getArguments())));
if (null !== $definition
->getFactoryMethod()) {
if (null !== $definition
->getFactoryClass()) {
$factory = $parameterBag
->resolveValue($definition
->getFactoryClass());
}
elseif (null !== $definition
->getFactoryService()) {
$factory = $this
->get($parameterBag
->resolveValue($definition
->getFactoryService()));
}
else {
throw new RuntimeException('Cannot create service from factory method without a factory service or factory class.');
}
$service = call_user_func_array(array(
$factory,
$definition
->getFactoryMethod(),
), $arguments);
}
else {
$r = new \ReflectionClass($parameterBag
->resolveValue($definition
->getClass()));
$service = null === $r
->getConstructor() ? $r
->newInstance() : $r
->newInstanceArgs($arguments);
}
if (self::SCOPE_PROTOTYPE !== ($scope = $definition
->getScope())) {
if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
throw new RuntimeException('You tried to create a service of an inactive scope.');
}
$this->services[$lowerId = strtolower($id)] = $service;
if (self::SCOPE_CONTAINER !== $scope) {
$this->scopedServices[$scope][$lowerId] = $service;
}
}
foreach ($definition
->getMethodCalls() as $call) {
$services = self::getServiceConditionals($call[1]);
$ok = true;
foreach ($services as $s) {
if (!$this
->has($s)) {
$ok = false;
break;
}
}
if ($ok) {
call_user_func_array(array(
$service,
$call[0],
), $this
->resolveServices($parameterBag
->resolveValue($call[1])));
}
}
$properties = $this
->resolveServices($parameterBag
->resolveValue($definition
->getProperties()));
foreach ($properties as $name => $value) {
$service->{$name} = $value;
}
if ($callable = $definition
->getConfigurator()) {
if (is_array($callable) && is_object($callable[0]) && $callable[0] instanceof Reference) {
$callable[0] = $this
->get((string) $callable[0]);
}
elseif (is_array($callable)) {
$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)) {
foreach ($value as &$v) {
$v = $this
->resolveServices($v);
}
}
elseif (is_object($value) && $value instanceof Reference) {
$value = $this
->get((string) $value, $value
->getInvalidBehavior());
}
elseif (is_object($value) && $value instanceof Definition) {
$value = $this
->createService($value, null);
}
return $value;
}
public function findTaggedServiceIds($name) {
$tags = array();
foreach ($this
->getDefinitions() as $id => $definition) {
if ($definition
->getTag($name)) {
$tags[$id] = $definition
->getTag($name);
}
}
return $tags;
}
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 (is_object($value) && $value instanceof Reference && $value
->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
$services[] = (string) $value;
}
return $services;
}
}