<?php
namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
class Container implements IntrospectableContainerInterface {
protected $parameterBag;
protected $services;
protected $scopes;
protected $scopeChildren;
protected $scopedServices;
protected $scopeStacks;
protected $loading = array();
public function __construct(ParameterBagInterface $parameterBag = null) {
$this->parameterBag = null === $parameterBag ? new ParameterBag() : $parameterBag;
$this->services = array();
$this->scopes = array();
$this->scopeChildren = array();
$this->scopedServices = array();
$this->scopeStacks = array();
$this
->set('service_container', $this);
}
public function compile() {
$this->parameterBag
->resolve();
$this->parameterBag = new FrozenParameterBag($this->parameterBag
->all());
}
public function isFrozen() {
return $this->parameterBag instanceof FrozenParameterBag;
}
public function getParameterBag() {
return $this->parameterBag;
}
public function getParameter($name) {
return $this->parameterBag
->get($name);
}
public function hasParameter($name) {
return $this->parameterBag
->has($name);
}
public function setParameter($name, $value) {
$this->parameterBag
->set($name, $value);
}
public function set($id, $service, $scope = self::SCOPE_CONTAINER) {
if (self::SCOPE_PROTOTYPE === $scope) {
throw new InvalidArgumentException('You cannot set services of scope "prototype".');
}
$id = strtolower($id);
if (self::SCOPE_CONTAINER !== $scope) {
if (!isset($this->scopedServices[$scope])) {
throw new RuntimeException('You cannot set services of inactive scopes.');
}
$this->scopedServices[$scope][$id] = $service;
}
$this->services[$id] = $service;
}
public function has($id) {
$id = strtolower($id);
return isset($this->services[$id]) || method_exists($this, 'get' . strtr($id, array(
'_' => '',
'.' => '_',
)) . 'Service');
}
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {
$id = strtolower($id);
if (isset($this->services[$id])) {
return $this->services[$id];
}
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
}
if (method_exists($this, $method = 'get' . strtr($id, array(
'_' => '',
'.' => '_',
)) . 'Service')) {
$this->loading[$id] = true;
try {
$service = $this
->{$method}();
} catch (\Exception $e) {
unset($this->loading[$id]);
throw $e;
}
unset($this->loading[$id]);
return $service;
}
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
throw new ServiceNotFoundException($id);
}
}
public function initialized($id) {
return isset($this->services[strtolower($id)]);
}
public function getServiceIds() {
$ids = array();
$r = new \ReflectionClass($this);
foreach ($r
->getMethods() as $method) {
if (preg_match('/^get(.+)Service$/', $method->name, $match)) {
$ids[] = self::underscore($match[1]);
}
}
return array_unique(array_merge($ids, array_keys($this->services)));
}
public function enterScope($name) {
if (!isset($this->scopes[$name])) {
throw new InvalidArgumentException(sprintf('The scope "%s" does not exist.', $name));
}
if (self::SCOPE_CONTAINER !== $this->scopes[$name] && !isset($this->scopedServices[$this->scopes[$name]])) {
throw new RuntimeException(sprintf('The parent scope "%s" must be active when entering this scope.', $this->scopes[$name]));
}
if (isset($this->scopedServices[$name])) {
$services = array(
$this->services,
$name => $this->scopedServices[$name],
);
unset($this->scopedServices[$name]);
foreach ($this->scopeChildren[$name] as $child) {
$services[$child] = $this->scopedServices[$child];
unset($this->scopedServices[$child]);
}
$this->services = call_user_func_array('array_diff_key', $services);
array_shift($services);
if (!isset($this->scopeStacks[$name])) {
$this->scopeStacks[$name] = new \SplStack();
}
$this->scopeStacks[$name]
->push($services);
}
$this->scopedServices[$name] = array();
}
public function leaveScope($name) {
if (!isset($this->scopedServices[$name])) {
throw new InvalidArgumentException(sprintf('The scope "%s" is not active.', $name));
}
$services = array(
$this->services,
$this->scopedServices[$name],
);
unset($this->scopedServices[$name]);
foreach ($this->scopeChildren[$name] as $child) {
if (!isset($this->scopedServices[$child])) {
continue;
}
$services[] = $this->scopedServices[$child];
unset($this->scopedServices[$child]);
}
$this->services = call_user_func_array('array_diff_key', $services);
if (isset($this->scopeStacks[$name]) && count($this->scopeStacks[$name]) > 0) {
$services = $this->scopeStacks[$name]
->pop();
$this->scopedServices += $services;
array_unshift($services, $this->services);
$this->services = call_user_func_array('array_merge', $services);
}
}
public function addScope(ScopeInterface $scope) {
$name = $scope
->getName();
$parentScope = $scope
->getParentName();
if (self::SCOPE_CONTAINER === $name || self::SCOPE_PROTOTYPE === $name) {
throw new InvalidArgumentException(sprintf('The scope "%s" is reserved.', $name));
}
if (isset($this->scopes[$name])) {
throw new InvalidArgumentException(sprintf('A scope with name "%s" already exists.', $name));
}
if (self::SCOPE_CONTAINER !== $parentScope && !isset($this->scopes[$parentScope])) {
throw new InvalidArgumentException(sprintf('The parent scope "%s" does not exist, or is invalid.', $parentScope));
}
$this->scopes[$name] = $parentScope;
$this->scopeChildren[$name] = array();
while ($parentScope !== self::SCOPE_CONTAINER) {
$this->scopeChildren[$parentScope][] = $name;
$parentScope = $this->scopes[$parentScope];
}
}
public function hasScope($name) {
return isset($this->scopes[$name]);
}
public function isScopeActive($name) {
return isset($this->scopedServices[$name]);
}
public static function camelize($id) {
return preg_replace_callback('/(^|_|\\.)+(.)/', function ($match) {
return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]);
}, $id);
}
public static function underscore($id) {
return strtolower(preg_replace(array(
'/([A-Z]+)([A-Z][a-z])/',
'/([a-z\\d])([A-Z])/',
), array(
'\\1_\\2',
'\\1_\\2',
), strtr($id, '_', '.')));
}
}