/** * @covers Symfony\Components\DependencyInjection\Container::set * @covers Symfony\Components\DependencyInjection\Container::offsetSet */ public function testSet() { $sc = new Container(); $sc->set('foo', $foo = new \stdClass()); $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service'); $sc['bar'] = $foo = new \stdClass(); $this->assertEquals($foo, $sc->get('bar'), '->offsetSet() sets a service'); }
/** * Gets a service. * * @param string $id The service identifier * @param int $invalidBehavior The behavior when the service does not exist * * @return object The associated service * * @throws \InvalidArgumentException if the service is not defined * @throws \LogicException if the service has a circular reference to itself * * @see Reference */ public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) { 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)); } 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; } }