[DependencyInjection] better logging
This commit is contained in:
parent
d873f21f69
commit
2397bcbe94
@ -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';
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -17,9 +17,13 @@ class RemoveAbstractDefinitionsPass implements CompilerPassInterface
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,12 +29,16 @@ class RemovePrivateAliasesPass implements CompilerPassInterface
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
}
|
||||
}
|
@ -41,7 +41,9 @@ class RemoveUnusedDefinitionsPass implements RepeatablePassInterface
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user