Pārlūkot izejas kodu

[FrameworkBundle] refactored container:debug command

 * Use a dumper to serialize the container into the cache (XML)
 * Only keep the "real" services (abstract ones are not displayed anymore)
Fabien Potencier 14 gadi atpakaļ
vecāks
revīzija
11cdff93f3

+ 17 - 10
src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

@@ -16,9 +16,11 @@ use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Output\Output;
-use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass;
 use Symfony\Component\DependencyInjection\Alias;
 use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\Config\FileLocator;
 
 /**
  * A console command for retrieving information about services
@@ -28,7 +30,7 @@ use Symfony\Component\DependencyInjection\Definition;
 class ContainerDebugCommand extends Command
 {
     /**
-     * @var \Symfony\Component\DependencyInjection\ContainerBuilder
+     * @var ContainerBuilder
      */
     private $containerBuilder;
 
@@ -78,8 +80,7 @@ EOF
         if ($name) {
             $this->outputService($output, $name);
         } else {
-            $showPrivate = $input->getOption('show-private');
-            $this->outputServices($output, $serviceIds, $showPrivate);
+            $this->outputServices($output, $serviceIds, $input->getOption('show-private'));
         }
     }
 
@@ -94,7 +95,7 @@ EOF
 
         $output->writeln($this->getHelper('formatter')->formatSection('container', $label));
 
-        // loop through to find get space needed and filter private services
+        // loop through to get space needed and filter private services
         $maxName = 4;
         $maxScope = 6;
         foreach ($serviceIds as $key => $serviceId) {
@@ -174,18 +175,24 @@ EOF
     /**
      * Loads the ContainerBuilder from the cache.
      *
-     * @see ContainerBuilderDebugDumpPass
-     * @return \Symfony\Component\DependencyInjection\ContainerBuilder
+     * @return ContainerBuilder
      */
     private function getContainerBuilder()
     {
-        $cachedFile = ContainerBuilderDebugDumpPass::getBuilderCacheFilename($this->container);
+        if (!$this->getApplication()->getKernel()->isDebug()) {
+            throw new \LogicException(sprintf('Debug information about the container is only available in debug mode.'));
+        }
 
-        if (!file_exists($cachedFile)) {
+        if (!file_exists($cachedFile = $this->container->getParameter('debug.container.dump'))) {
             throw new \LogicException(sprintf('Debug information about the container could not be found. Please clear the cache and try again.'));
         }
 
-        return unserialize(file_get_contents($cachedFile));
+        $container = new ContainerBuilder();
+
+        $loader = new XmlFileLoader($container, new FileLocator());
+        $loader->load($cachedFile);
+
+        return $container;
     }
 
     /**

+ 4 - 18
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ContainerBuilderDebugDumpPass.php

@@ -12,7 +12,7 @@
 namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
 
 use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
 use Symfony\Component\Config\ConfigCache;
 
@@ -27,22 +27,8 @@ class ContainerBuilderDebugDumpPass implements CompilerPassInterface
 {
     public function process(ContainerBuilder $container)
     {
-        $cache = new ConfigCache(self::getBuilderCacheFilename($container), false);
-
-        $cache->write(serialize($container));
-    }
-
-    /**
-     * Calculates the cache filename to be used to cache the ContainerBuilder
-     *
-     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
-     *
-     * @return string
-     */
-    static public function getBuilderCacheFilename(ContainerInterface $container)
-    {
-        $class = $container->getParameter('kernel.container_class');
-
-        return $container->getParameter('kernel.cache_dir').'/'.$class.'Builder.cache';
+        $dumper = new XmlDumper($container);
+        $cache = new ConfigCache($container->getParameter('debug.container.dump'), false);
+        $cache->write($dumper->dump());
     }
 }

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

@@ -60,7 +60,7 @@ class FrameworkBundle extends Bundle
         $container->addCompilerPass(new AddCacheWarmerPass());
 
         if ($container->getParameter('kernel.debug')) {
-            $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_BEFORE_REMOVING);
+            $container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
             $container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
         }
     }

+ 1 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/debug.xml

@@ -6,6 +6,7 @@
 
     <parameters>
         <parameter key="debug.event_dispatcher.class">Symfony\Bundle\FrameworkBundle\Debug\TraceableEventDispatcher</parameter>
+        <parameter key="debug.container.dump">%kernel.cache_dir%/%kernel.container_class%.xml</parameter>
     </parameters>
 
     <services>