Generates the service instance.
string $id:
Definition $definition:
string
private function addServiceInstance($id, $definition) {
$class = $this
->dumpValue($definition
->getClass());
if (0 === strpos($class, "'") && !preg_match('/^\'[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*(\\\\{2}[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)*\'$/', $class)) {
throw new InvalidArgumentException(sprintf('"%s" is not a valid class name for the "%s" service.', $class, $id));
}
$arguments = array();
foreach ($definition
->getArguments() as $value) {
$arguments[] = $this
->dumpValue($value);
}
$simple = $this
->isSimpleInstance($id, $definition);
$instantiation = '';
if (ContainerInterface::SCOPE_CONTAINER === $definition
->getScope()) {
$instantiation = "\$this->services['{$id}'] = " . ($simple ? '' : '$instance');
}
elseif (ContainerInterface::SCOPE_PROTOTYPE !== ($scope = $definition
->getScope())) {
$instantiation = "\$this->services['{$id}'] = \$this->scopedServices['{$scope}']['{$id}'] = " . ($simple ? '' : '$instance');
}
elseif (!$simple) {
$instantiation = '$instance';
}
$return = '';
if ($simple) {
$return = 'return ';
}
else {
$instantiation .= ' = ';
}
if (null !== $definition
->getFactoryMethod()) {
if (null !== $definition
->getFactoryClass()) {
$code = sprintf(" {$return}{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this
->dumpValue($definition
->getFactoryClass()), $definition
->getFactoryMethod(), $arguments ? ', ' . implode(', ', $arguments) : '');
}
elseif (null !== $definition
->getFactoryService()) {
$code = sprintf(" {$return}{$instantiation}%s->%s(%s);\n", $this
->getServiceCall($definition
->getFactoryService()), $definition
->getFactoryMethod(), implode(', ', $arguments));
}
else {
throw new RuntimeException('Factory method requires a factory service or factory class in service definition for ' . $id);
}
}
elseif (false !== strpos($class, '$')) {
$code = sprintf(" \$class = %s;\n\n {$return}{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
}
else {
$code = sprintf(" {$return}{$instantiation}new \\%s(%s);\n", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
}
if (!$simple) {
$code .= "\n";
}
return $code;
}