Bläddra i källkod

[DependencyInjection] better logging

Johannes Schmitt 14 år sedan
förälder
incheckning
2397bcbe94

+ 24 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CompilerDebugDumpPass.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Config\ConfigCache;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+
+class CompilerDebugDumpPass implements CompilerPassInterface
+{
+    public function process(ContainerBuilder $container)
+    {
+        $cache = new ConfigCache($this->getCompilerLogFilename($container), false);
+        $cache->write(serialize($container->getCompiler()->getLog()));
+    }
+
+    public static function getCompilerLogFilename(ContainerInterface $container)
+    {
+        $class = $container->getParameter('kernel.container_class');
+
+        return $container->getParameter('kernel.cache_dir').'/'.$class.'Compiler.log';
+    }
+}

+ 3 - 0
src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Bundle\FrameworkBundle;
 
+use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CompilerDebugDumpPass;
+
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddFieldFactoryGuessersPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
@@ -82,5 +84,6 @@ class FrameworkBundle extends Bundle
         $container->addCompilerPass(new TranslatorPass());
         $container->addCompilerPass(new AddCacheWarmerPass());
         $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING);
+        $container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
     }
 }

+ 14 - 2
src/Symfony/Component/DependencyInjection/Compiler/Compiler.php

@@ -25,6 +25,7 @@ class Compiler
     private $currentPass;
     private $currentStartTime;
     private $log;
+    private $loggingFormatter;
     private $serviceReferenceGraph;
 
     /**
@@ -34,6 +35,7 @@ class Compiler
     {
         $this->passConfig = new PassConfig();
         $this->serviceReferenceGraph = new ServiceReferenceGraph();
+        $this->loggingFormatter = new LoggingFormatter();
         $this->log = array();
     }
 
@@ -57,6 +59,16 @@ class Compiler
         return $this->serviceReferenceGraph;
     }
 
+    /**
+     * Returns the logging formatter which can be used by compilation passes.
+     *
+     * @return LoggingFormatter
+     */
+    public function getLoggingFormatter()
+    {
+        return $this->loggingFormatter;
+    }
+
     /**
      * Adds a pass to the PassConfig.
      *
@@ -71,7 +83,7 @@ class Compiler
     /**
      * Adds a log message.
      *
-     * @param string $string The log message 
+     * @param string $string The log message
      */
     public function addLogMessage($string)
     {
@@ -91,7 +103,7 @@ class Compiler
     /**
      * Run the Compiler and process all Passes.
      *
-     * @param ContainerBuilder $container 
+     * @param ContainerBuilder $container
      */
     public function compile(ContainerBuilder $container)
     {

+ 11 - 2
src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

@@ -25,6 +25,9 @@ class InlineServiceDefinitionsPass implements RepeatablePassInterface
 {
     private $repeatedPass;
     private $graph;
+    private $compiler;
+    private $formatter;
+    private $currentId;
 
     /**
      * {@inheritDoc}
@@ -41,9 +44,13 @@ class InlineServiceDefinitionsPass implements RepeatablePassInterface
      */
     public function process(ContainerBuilder $container)
     {
-        $this->graph = $container->getCompiler()->getServiceReferenceGraph();
+        $this->compiler = $container->getCompiler();
+        $this->formatter = $this->compiler->getLoggingFormatter();
+        $this->graph = $this->compiler->getServiceReferenceGraph();
+
+        foreach ($container->getDefinitions() as $id => $definition) {
+            $this->currentId = $id;
 
-        foreach ($container->getDefinitions() as $definition) {
             $definition->setArguments(
                 $this->inlineArguments($container, $definition->getArguments())
             );
@@ -75,6 +82,8 @@ class InlineServiceDefinitionsPass implements RepeatablePassInterface
                 }
 
                 if ($this->isInlinableDefinition($container, $id, $definition = $container->getDefinition($id))) {
+                    $this->compiler->addLogMessage($this->formatter->formatInlineDefinition($this, $id, $this->currentId));
+
                     if (ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope()) {
                         $arguments[$k] = $definition;
                     } else {

+ 28 - 0
src/Symfony/Component/DependencyInjection/Compiler/LoggingFormatter.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace Symfony\Component\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Definition;
+
+/**
+ * Used to format logging messages during the compilation.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class LoggingFormatter
+{
+    public function formatRemoveDefinition(CompilerPassInterface $pass, $id, $reason)
+    {
+        return $this->format($pass, sprintf('Removed definition "%s"; reason: %s', $id, $reason));
+    }
+
+    public function formatInlineDefinition(CompilerPassInterface $pass, $id, $target)
+    {
+        return $this->format($pass, sprintf('Inlined definition "%s" to "%s".', $id, $target));
+    }
+
+    public function format(CompilerPassInterface $pass, $message)
+    {
+        return sprintf('%s: %s', get_class($pass), $message);
+    }
+}

+ 5 - 1
src/Symfony/Component/DependencyInjection/Compiler/RemoveAbstractDefinitionsPass.php

@@ -13,13 +13,17 @@ class RemoveAbstractDefinitionsPass implements CompilerPassInterface
     /**
      * Removes abstract definitions from the ContainerBuilder
      *
-     * @param ContainerBuilder $container 
+     * @param ContainerBuilder $container
      */
     public function process(ContainerBuilder $container)
     {
+        $compiler = $container->getCompiler();
+        $formatter = $compiler->getLoggingFormatter();
+
         foreach ($container->getDefinitions() as $id => $definition) {
             if ($definition->isAbstract()) {
                 $container->remove($id);
+                $compiler->addLogMessage($formatter->formatRemoveDefinition($this, $id, 'abstract'));
             }
         }
     }

+ 5 - 1
src/Symfony/Component/DependencyInjection/Compiler/RemovePrivateAliasesPass.php

@@ -25,16 +25,20 @@ class RemovePrivateAliasesPass implements CompilerPassInterface
     /**
      * Removes private aliases from the ContainerBuilder
      *
-     * @param ContainerBuilder $container 
+     * @param ContainerBuilder $container
      */
     public function process(ContainerBuilder $container)
     {
+        $compiler = $container->getCompiler();
+        $formatter = $compiler->getLoggingFormatter();
+
         foreach ($container->getAliases() as $id => $alias) {
             if ($alias->isPublic()) {
                 continue;
             }
 
             $container->removeAlias($id);
+            $compiler->addLogMessage($formatter->formatRemoveDefinition($this, $id, 'private alias'));
         }
     }
 }

+ 6 - 2
src/Symfony/Component/DependencyInjection/Compiler/RemoveUnusedDefinitionsPass.php

@@ -36,12 +36,14 @@ class RemoveUnusedDefinitionsPass implements RepeatablePassInterface
     /**
      * Processes the ContainerBuilder to remove unused definitions.
      *
-     * @param ContainerBuilder $container 
+     * @param ContainerBuilder $container
      * @return void
      */
     public function process(ContainerBuilder $container)
     {
-        $graph = $container->getCompiler()->getServiceReferenceGraph();
+        $compiler = $container->getCompiler();
+        $formatter = $compiler->getLoggingFormatter();
+        $graph = $compiler->getServiceReferenceGraph();
 
         $hasChanged = false;
         foreach ($container->getDefinitions() as $id => $definition) {
@@ -71,8 +73,10 @@ class RemoveUnusedDefinitionsPass implements RepeatablePassInterface
                 $container->setDefinition((string) reset($referencingAliases), $definition);
                 $definition->setPublic(true);
                 $container->remove($id);
+                $compiler->addLogMessage($formatter->formatRemovedDefinition($this, $id, 'replaces alias '.reset($referencingAliases)));
             } else if (0 === count($referencingAliases) && false === $isReferenced) {
                 $container->remove($id);
+                $compiler->addLogMessage($formatter->formatRemoveDefinition($this, $id, 'unused'));
                 $hasChanged = true;
             }
         }