Skip to content

Commit

Permalink
[DependencyInjection] refactored code to avoid logic duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 6, 2013
1 parent 151f2c1 commit 1e2fb64
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
66 changes: 30 additions & 36 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Expand Up @@ -227,24 +227,7 @@ private function addServiceInlinedDefinitions($id, $definition)
throw new ServiceCircularReferenceException($id, array($id));
}

$arguments = array();
foreach ($sDefinition->getArguments() as $argument) {
$arguments[] = $this->dumpValue($argument);
}

if (null !== $sDefinition->getFactoryMethod()) {
if (null !== $sDefinition->getFactoryClass()) {
$code .= sprintf(" \$%s = call_user_func(array(%s, '%s')%s);\n", $name, $this->dumpValue($sDefinition->getFactoryClass()), $sDefinition->getFactoryMethod(), count($arguments) > 0 ? ', '.implode(', ', $arguments) : '');
} elseif (null !== $sDefinition->getFactoryService()) {
$code .= sprintf(" \$%s = %s->%s(%s);\n", $name, $this->getServiceCall($sDefinition->getFactoryService()), $sDefinition->getFactoryMethod(), implode(', ', $arguments));
} else {
throw new \RuntimeException('Factory service or factory class must be defined in service definition for '.$id);
}
} elseif (false !== strpos($class, '$')) {
$code .= sprintf(" \$class = %s;\n \$%s = new \$class(%s);\n", $class, $name, implode(', ', $arguments));
} else {
$code .= sprintf(" \$%s = new \\%s(%s);\n", $name, substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
}
$code .= $this->addNewInstance($id, $sDefinition, '$'.$name, ' = ');

if (!$this->hasReference($id, $sDefinition->getMethodCalls(), true) && !$this->hasReference($id, $sDefinition->getProperties(), true)) {
$code .= $this->addServiceMethodCalls(null, $sDefinition, $name);
Expand Down Expand Up @@ -295,11 +278,6 @@ private function addServiceInstance($id, $definition)
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 = '';
Expand All @@ -318,19 +296,7 @@ private function addServiceInstance($id, $definition)
$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 $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));
}
$code = $this->addNewInstance($id, $definition, $return, $instantiation);

if (!$simple) {
$code .= "\n";
Expand Down Expand Up @@ -609,6 +575,34 @@ private function addServices()
return $publicServices.$aliasServices.$privateServices;
}

private function addNewInstance($id, Definition $definition, $return, $instantiation)
{
$class = $this->dumpValue($definition->getClass());

$arguments = array();
foreach ($definition->getArguments() as $value) {
$arguments[] = $this->dumpValue($value);
}

if (null !== $definition->getFactoryMethod()) {
if (null !== $definition->getFactoryClass()) {
return sprintf(" $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $this->dumpValue($definition->getFactoryClass()), $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
}

if (null !== $definition->getFactoryService()) {
return sprintf(" $return{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments));
}

throw new RuntimeException('Factory method requires a factory service or factory class in service definition for '.$id);
}

if (false !== strpos($class, '$')) {
return sprintf(" \$class = %s;\n\n $return{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
}

return sprintf(" $return{$instantiation}new \\%s(%s);\n", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
}

/**
* Adds the class headers.
*
Expand Down
Expand Up @@ -118,6 +118,7 @@ protected function getFoo_BazService()
protected function getFooBarService()
{
$class = $this->getParameter('foo_class');

return new $class();
}

Expand Down

0 comments on commit 1e2fb64

Please sign in to comment.