فهرست منبع

[DependencyInjection] made some cosmetic changes to the PHP dumper output

Fabien Potencier 14 سال پیش
والد
کامیت
341178e869

+ 20 - 11
src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

@@ -62,12 +62,11 @@ class PhpDumper extends Dumper
 
     protected function addServiceReturn($id, $definition)
     {
-        return <<<EOF
-
-        return \$instance;
-    }
+        if (!$definition->getMethodCalls() && !$definition->getConfigurator()) {
+            return "    }\n";
+        }
 
-EOF;
+        return "\n        return \$instance;\n    }\n";
     }
 
     protected function addServiceInstance($id, $definition)
@@ -83,22 +82,32 @@ EOF;
             $arguments[] = $this->dumpValue($value);
         }
 
+        $simple = !$definition->getMethodCalls() && !$definition->getConfigurator();
+
+        $instantiation = '';
         if ($definition->isShared()) {
-            $instantiation = sprintf("        \$this->services['$id'] = \$instance");
+            $instantiation = "\$this->services['$id'] = ".($simple ? '' : '$instance');
+        } elseif (!$simple) {
+            $instantiation = '$instance';
+        }
+
+        $return = '';
+        if ($simple) {
+            $return = 'return ';
         } else {
-            $instantiation = sprintf("        \$instance");
+            $instantiation .= ' = ';
         }
 
         if (null !== $definition->getFactoryMethod()) {
             if (null !== $definition->getFactoryService()) {
-                $code = sprintf("$instantiation = %s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments));
+                $code = sprintf("        $return{$instantiation}%s->%s(%s);\n", $this->getServiceCall($definition->getFactoryService()), $definition->getFactoryMethod(), implode(', ', $arguments));
             } else {
-                $code = sprintf("$instantiation = call_user_func(array(%s, '%s')%s);\n", $class, $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
+                $code = sprintf("        $return{$instantiation}call_user_func(array(%s, '%s')%s);\n", $class, $definition->getFactoryMethod(), $arguments ? ', '.implode(', ', $arguments) : '');
             }
         } elseif (false !== strpos($class, '$')) {
-            $code = sprintf("        \$class = %s;\n$instantiation = new \$class(%s);\n", $class, implode(', ', $arguments));
+            $code = sprintf("        \$class = %s;\n        $return{$instantiation}new \$class(%s);\n", $class, implode(', ', $arguments));
         } else {
-            $code = sprintf("$instantiation = new \\%s(%s);\n", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
+            $code = sprintf("        $return{$instantiation}new \\%s(%s);\n", substr(str_replace('\\\\', '\\', $class), 1, -1), implode(', ', $arguments));
         }
 
         return $code;

+ 2 - 6
tests/Symfony/Tests/Component/DependencyInjection/Fixtures/php/services9.php

@@ -81,9 +81,7 @@ class ProjectServiceContainer extends Container implements TaggedContainerInterf
     protected function getFooBarService()
     {
         $class = $this->getParameter('foo_class');
-        $this->services['foo_bar'] = $instance = new $class();
-
-        return $instance;
+        return $this->services['foo_bar'] = new $class();
     }
 
     /**
@@ -121,9 +119,7 @@ class ProjectServiceContainer extends Container implements TaggedContainerInterf
      */
     protected function getFactoryServiceService()
     {
-        $this->services['factory_service'] = $instance = $this->get('foo.baz')->getInstance();
-
-        return $instance;
+        return $this->services['factory_service'] = $this->get('foo.baz')->getInstance();
     }
 
     /**