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 = '';
if ($definition
->isSynthetic()) {
$return = sprintf('@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());
}
$doc = '';
if (ContainerInterface::SCOPE_PROTOTYPE !== $definition
->getScope()) {
$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;
}
$code = <<<EOF
/**
* Gets the '{<span class="php-variable">$id</span>}' service.{<span class="php-variable">$doc</span>}
*
* {<span class="php-variable">$return</span>}
*/
protected function get{<span class="php-variable">$name</span>}Service()
{
EOF;
$scope = $definition
->getScope();
if (ContainerInterface::SCOPE_CONTAINER !== $scope && ContainerInterface::SCOPE_PROTOTYPE !== $scope) {
$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;
}