Symfony\Component\DependencyInjection\ContainerBuilder::set PHP Method

set() public method

Sets a service.
public set ( string $id, object $service )
$id string The service identifier
$service object The service instance
    public function set($id, $service)
    {
        $id = strtolower($id);

        if ($this->isFrozen() && (isset($this->definitions[$id]) && !$this->definitions[$id]->isSynthetic())) {
            // setting a synthetic service on a frozen container is alright
            throw new BadMethodCallException(sprintf('Setting service "%s" for an unknown or non-synthetic service definition on a frozen container is not allowed.', $id));
        }

        unset($this->definitions[$id], $this->aliasDefinitions[$id]);

        parent::set($id, $service);
    }

Usage Example

 /**
  * @test
  */
 public function it_does_not_fail_if_the_synthetic_service_has_been_provided_already()
 {
     $this->containerBuilder->setDefinition('synthetic_service', $this->createSyntheticDefinition());
     $this->containerBuilder->set('synthetic_service', new \stdClass());
     $constraint = new ContainerBuilderHasSyntheticServiceConstraint('synthetic_service');
     $this->assertConstraintPasses($constraint);
 }
All Usage Examples Of Symfony\Component\DependencyInjection\ContainerBuilder::set