/** * Gets a service. * * @param string $id The service identifier * * @return object The associated service * * @throw InvalidArgumentException if the service is not defined * @throw LogicException if the service has a circular reference to itself */ public function getService($id) { Profile::start('SymfonyContainer', 'Getting service'); try { $return = parent::getService($id); } catch (InvalidArgumentException $e) { if (isset($this->loading[$id])) { Profile::stop(); throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id)); } if (!$this->hasServiceDefinition($id) && isset($this->aliases[$id])) { $return = $this->getService($this->aliases[$id]); } else { $definition = $this->getServiceDefinition($id); $this->loading[$id] = true; if ($definition->isShared()) { $service = $this->services[$id] = $this->createService($definition); } else { $service = $this->createService($definition); } unset($this->loading[$id]); $return = $service; } } Profile::stop(); return $return; }
/** * Returns the correct renderer for given attributes. * * @param string $viewName * @param string $templatesPath * @param array $requestedContentTypes * @return Renderer */ public function getAppropriateRenderer($viewName, $templatesPath, $requestedContentTypes) { if (!$requestedContentTypes) { $requestedContentTypes = array(); } array_push($requestedContentTypes, '*'); // This is the catch all content type. foreach ($requestedContentTypes as $contentType) { foreach ($this->renderersList as $rendererInfo) { list($rendererClassName, $rendererServiceName) = $rendererInfo; if (call_user_func($rendererClassName . '::accepts', $viewName, $templatesPath, $contentType)) { Log::debug('Using ' . $rendererClassName, 'Renderer'); $renderer = $this->container->getService($rendererServiceName); $renderer->setTemplatesPath($templatesPath); return $renderer; } } } return null; }
/** * Gets a service. * * @param string $id The service identifier * * @return object The associated service * * @throw InvalidArgumentException if the service is not defined * @throw LogicException if the service has a circular reference to itself */ public function getService($id) { try { return parent::getService($id); } catch (InvalidArgumentException $e) { if (isset($this->loading[$id])) { throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id)); } $definition = $this->getServiceDefinition($id); $this->loading[$id] = true; if ($definition->isShared()) { $service = $this->services[$id] = $this->createService($definition); } else { $service = $this->createService($definition); } unset($this->loading[$id]); return $service; } }
$sc = new sfServiceContainer(array('foo' => 'bar')); $t->ok($sc->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined'); $t->ok($sc->hasParameter('Foo'), '->hasParameter() converts the key to lowercase'); $t->ok(!$sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined'); // ->addParameters() $t->diag('->addParameters()'); $sc = new sfServiceContainer(array('foo' => 'bar')); $sc->addParameters(array('bar' => 'foo')); $t->is($sc->getParameters(), array('foo' => 'bar', 'bar' => 'foo'), '->addParameters() adds parameters to the existing ones'); $sc->addParameters(array('Bar' => 'fooz')); $t->is($sc->getParameters(), array('foo' => 'bar', 'bar' => 'fooz'), '->addParameters() converts keys to lowercase'); // ->setService() ->hasService() ->getService() $t->diag('->setService() ->hasService() ->getService()'); $sc = new sfServiceContainer(); $sc->setService('foo', $obj = new stdClass()); $t->is(spl_object_hash($sc->getService('foo')), spl_object_hash($obj), '->setService() registers a service under a key name'); $sc->foo1 = $obj1 = new stdClass(); $t->ok($sc->hasService('foo'), '->hasService() returns true if the service is defined'); $t->ok(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined'); // ->getServiceIds() $t->diag('->getServiceIds()'); $sc = new sfServiceContainer(); $sc->setService('foo', $obj = new stdClass()); $sc->setService('bar', $obj = new stdClass()); $t->is($sc->getServiceIds(), array('service_container', 'foo', 'bar'), '->getServiceIds() returns all defined service ids'); class ProjectServiceContainer extends sfServiceContainer { public $__bar, $__foo_bar, $__foo_baz; public function __construct() { parent::__construct();