Sets a service.
@api
string $id The service identifier:
object $service The service instance:
string $scope The scope of the service:
RuntimeException When trying to set a service in an inactive scope
InvalidArgumentException When trying to set a service in the prototype scope
Overrides ContainerInterface::set
public function set($id, $service, $scope = self::SCOPE_CONTAINER) {
if (self::SCOPE_PROTOTYPE === $scope) {
throw new InvalidArgumentException(sprintf('You cannot set service "%s" of scope "prototype".', $id));
}
$id = strtolower($id);
if (self::SCOPE_CONTAINER !== $scope) {
if (!isset($this->scopedServices[$scope])) {
throw new RuntimeException(sprintf('You cannot set service "%s" of inactive scope.', $id));
}
$this->scopedServices[$scope][$id] = $service;
}
$this->services[$id] = $service;
if (method_exists($this, $method = 'synchronize' . strtr($id, array(
'_' => '',
'.' => '_',
)) . 'Service')) {
$this
->{$method}();
}
}