Newer
Older
Dries Buytaert
committed
<?php
// @codingStandardsIgnoreFile
Dries Buytaert
committed
namespace Drupal\Core\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
catch
committed
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
Dries Buytaert
committed
/**
* Drupal's dependency injection container builder.
*
* @todo Submit upstream patches to Symfony to not require these overrides.
*
* @ingroup container
Dries Buytaert
committed
*/
class ContainerBuilder extends SymfonyContainerBuilder {
Dries Buytaert
committed
catch
committed
/**
* @var \Doctrine\Instantiator\InstantiatorInterface|null
*/
private $proxyInstantiator;
* {@inheritdoc}
public function __construct(ParameterBagInterface $parameterBag = NULL) {
$this->setResourceTracking(FALSE);
parent::__construct($parameterBag);
Katherine Bailey
committed
}
catch
committed
/**
* Retrieves the currently set proxy instantiator or instantiates one.
*
* @return InstantiatorInterface
*/
private function getProxyInstantiator()
{
if (!$this->proxyInstantiator) {
$this->proxyInstantiator = new RealServiceInstantiator();
}
return $this->proxyInstantiator;
}
/**
Lee Rowlands
committed
* A 1to1 copy of parent::shareService.
*
* @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
* 3.4 this is not a 1to1 copy.
catch
committed
*/
Lee Rowlands
committed
protected function shareService(Definition $definition, $service, $id, array &$inlineServices)
catch
committed
{
if ($definition->isShared()) {
catch
committed
$this->services[$lowerId = strtolower($id)] = $service;
}
}
/**
* Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
*
* Drupal's container builder can be used at runtime after compilation, so we
* override Symfony's ContainerBuilder's restriction on setting services in a
* frozen builder.
*
* @todo Restrict this to synthetic services only. Ideally, the upstream
* ContainerBuilder class should be fixed to allow setting synthetic
* services in a frozen builder.
*/
public function set($id, $service) {
catch
committed
if (strtolower($id) !== $id) {
throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
}
SymfonyContainer::set($id, $service);
Dries Buytaert
committed
Alex Pott
committed
// Ensure that the _serviceId property is set on synthetic services as well.
if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
$this->services[$id]->_serviceId = $id;
}
Dries Buytaert
committed
}
catch
committed
/**
* {@inheritdoc}
*/
public function register($id, $class = null) {
if (strtolower($id) !== $id) {
throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
}
Lee Rowlands
committed
$definition = parent::register($id, $class);
// As of Symfony 3.4 all services are private by default.
$definition->setPublic(TRUE);
return $definition;
}
/**
* {@inheritdoc}
*/
public function setAlias($alias, $id) {
$alias = parent::setAlias($alias, $id);
// As of Symfony 3.4 all aliases are private by default.
$alias->setPublic(TRUE);
return $alias;
catch
committed
}
/**
* {@inheritdoc}
*/
public function setParameter($name, $value) {
if (strtolower($name) !== $name) {
throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
}
parent::setParameter($name, $value);
}
Dries Buytaert
committed
/**
* A 1to1 copy of parent::callMethod.
Lee Rowlands
committed
*
* @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
* 3.4 this is not a 1to1 copy.
Dries Buytaert
committed
*/
Lee Rowlands
committed
protected function callMethod($service, $call, array &$inlineServices = array()) {
Dries Buytaert
committed
$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])));
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function __sleep() {
assert(FALSE, 'The container was serialized.');
Alex Pott
committed
return array_keys(get_object_vars($this));
}