Adds a service
string $id:
Definition $definition:
string
private function addService($id, $definition) {
$name = Container::camelize($id);
$this->definitionVariables = new \SplObjectStorage();
$this->referenceVariables = array();
$this->variableCount = 0;
$return = array();
if ($definition
->isSynthetic()) {
$return[] = '@throws RuntimeException always since this service is expected to be injected dynamically';
}
elseif ($class = $definition
->getClass()) {
$return[] = sprintf("@return %s A %s instance.", 0 === strpos($class, '%') ? 'object' : $class, $class);
}
elseif ($definition
->getFactoryClass()) {
$return[] = sprintf('@return object An instance returned by %s::%s().', $definition
->getFactoryClass(), $definition
->getFactoryMethod());
}
elseif ($definition
->getFactoryService()) {
$return[] = sprintf('@return object An instance returned by %s::%s().', $definition
->getFactoryService(), $definition
->getFactoryMethod());
}
$scope = $definition
->getScope();
if (!in_array($scope, array(
ContainerInterface::SCOPE_CONTAINER,
ContainerInterface::SCOPE_PROTOTYPE,
))) {
if ($return && 0 === strpos($return[count($return) - 1], '@return')) {
$return[] = '';
}
$return[] = sprintf("@throws InactiveScopeException when the '%s' service is requested while the '%s' scope is not active", $id, $scope);
}
$return = implode("\n * ", $return);
$doc = '';
if (ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
$doc .= <<<EOF
*
* This service is shared.
* This method always returns the same instance of the service.
EOF;
}
if (!$definition
->isPublic()) {
$doc .= <<<EOF
*
* This service is private.
* If you want to be able to request this service from the container directly,
* make it public, otherwise you might end up with broken code.
EOF;
}
if ($definition
->isLazy()) {
$lazyInitialization = '$lazyLoad = true';
$lazyInitializationDoc = "\n * @param boolean \$lazyLoad whether to try lazy-loading the service with a proxy\n *";
}
else {
$lazyInitialization = '';
$lazyInitializationDoc = '';
}
// with proxies, for 5.3.3 compatibility, the getter must be public to be accessible to the initializer
$isProxyCandidate = $this
->getProxyDumper()
->isProxyCandidate($definition);
$visibility = $isProxyCandidate ? 'public' : 'protected';
$code = <<<EOF
/**
* Gets the '{<span class="php-variable">$id</span>}' service.{<span class="php-variable">$doc</span>}
*{<span class="php-variable">$lazyInitializationDoc</span>}
* {<span class="php-variable">$return</span>}
*/
{<span class="php-variable">$visibility</span>} function get{<span class="php-variable">$name</span>}Service({<span class="php-variable">$lazyInitialization</span>})
{
EOF;
$code .= $isProxyCandidate ? $this
->getProxyDumper()
->getProxyFactoryCode($definition, $id) : '';
if (!in_array($scope, array(
ContainerInterface::SCOPE_CONTAINER,
ContainerInterface::SCOPE_PROTOTYPE,
))) {
$code .= <<<EOF
if (!isset(\$this->scopedServices['{<span class="php-variable">$scope</span>}'])) {
throw new InactiveScopeException('{<span class="php-variable">$id</span>}', '{<span class="php-variable">$scope</span>}');
}
EOF;
}
if ($definition
->isSynthetic()) {
$code .= sprintf(" throw new RuntimeException('You have requested a synthetic service (\"%s\"). The DIC does not know how to construct this service.');\n }\n", $id);
}
else {
$code .= $this
->addServiceInclude($id, $definition) . $this
->addServiceLocalTempVariables($id, $definition) . $this
->addServiceInlinedDefinitions($id, $definition) . $this
->addServiceInstance($id, $definition) . $this
->addServiceInlinedDefinitionsSetup($id, $definition) . $this
->addServiceMethodCalls($id, $definition) . $this
->addServiceProperties($id, $definition) . $this
->addServiceConfigurator($id, $definition) . $this
->addServiceReturn($id, $definition);
}
$this->definitionVariables = null;
$this->referenceVariables = null;
return $code;
}