Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03be6dd

Browse files
[DI] Enhance logging in compiler passes
1 parent 84a5483 commit 03be6dd

13 files changed

+46
-33
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ class UnusedTagsPass implements CompilerPassInterface
5353

5454
public function process(ContainerBuilder $container)
5555
{
56-
$compiler = $container->getCompiler();
57-
$formatter = $compiler->getLoggingFormatter();
5856
$tags = array_unique(array_merge($container->findTags(), $this->whitelist));
5957

6058
foreach ($container->findUnusedTags() as $tag) {
@@ -81,7 +79,7 @@ public function process(ContainerBuilder $container)
8179
$message .= sprintf(' Did you mean "%s"?', implode('", "', $candidates));
8280
}
8381

84-
$compiler->addLogMessage($formatter->format($this, $message));
82+
$container->log($this, $message);
8583
}
8684
}
8785
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function build(ContainerBuilder $container)
104104
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
105105
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
106106
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
107-
$container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
107+
$container->addCompilerPass(new CompilerDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING, -32);
108108
$container->addCompilerPass(new ConfigCachePass());
109109
$container->addCompilerPass(new CacheCollectorPass());
110110
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/UnusedTagsPassTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,10 @@ public function testProcess()
1919
{
2020
$pass = new UnusedTagsPass();
2121

22-
$formatter = $this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\LoggingFormatter')->getMock();
23-
$formatter
24-
->expects($this->at(0))
25-
->method('format')
26-
->with($pass, 'Tag "kenrel.event_subscriber" was defined on service(s) "foo", "bar", but was never used. Did you mean "kernel.event_subscriber"?')
27-
;
28-
29-
$compiler = $this->getMockBuilder('Symfony\Component\DependencyInjection\Compiler\Compiler')->getMock();
30-
$compiler->expects($this->once())->method('getLoggingFormatter')->will($this->returnValue($formatter));
31-
32-
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds', 'getCompiler', 'findUnusedTags', 'findTags'))->getMock();
33-
$container->expects($this->once())->method('getCompiler')->will($this->returnValue($compiler));
22+
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->setMethods(array('findTaggedServiceIds', 'findUnusedTags', 'findTags', 'log'))->getMock();
23+
$container->expects($this->once())
24+
->method('log')
25+
->with($pass, 'Tag "kenrel.event_subscriber" was defined on service(s) "foo", "bar", but was never used. Did you mean "kernel.event_subscriber"?');
3426
$container->expects($this->once())
3527
->method('findTags')
3628
->will($this->returnValue(array('kenrel.event_subscriber')));

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
145145
}
146146

147147
if ($notFound = array_diff($configuredAutowiredMethods, $found)) {
148-
$compiler = $this->container->getCompiler();
149-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatUnusedAutowiringPatterns($this, $this->currentId, $notFound));
148+
$this->container->log($this, sprintf('Autowiring\'s patterns "%s" for service "%s" don\'t match any method.', implode('", "', $notFound), $this->currentId));
150149
}
151150
}
152151

src/Symfony/Component/DependencyInjection/Compiler/Compiler.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function __construct()
3030
{
3131
$this->passConfig = new PassConfig();
3232
$this->serviceReferenceGraph = new ServiceReferenceGraph();
33-
$this->loggingFormatter = new LoggingFormatter();
3433
}
3534

3635
/**
@@ -57,9 +56,17 @@ public function getServiceReferenceGraph()
5756
* Returns the logging formatter which can be used by compilation passes.
5857
*
5958
* @return LoggingFormatter
59+
*
60+
* @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
6061
*/
6162
public function getLoggingFormatter()
6263
{
64+
if (null === $this->loggingFormatter) {
65+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', __METHOD__), E_USER_DEPRECATED);
66+
67+
$this->loggingFormatter = new LoggingFormatter();
68+
}
69+
6370
return $this->loggingFormatter;
6471
}
6572

@@ -92,12 +99,21 @@ public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BE
9299
* Adds a log message.
93100
*
94101
* @param string $string The log message
102+
*
103+
* @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
95104
*/
96105
public function addLogMessage($string)
97106
{
107+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', __METHOD__), E_USER_DEPRECATED);
108+
98109
$this->log[] = $string;
99110
}
100111

112+
public function log(CompilerPassInterface $pass, $message, $level = E_USER_NOTICE)
113+
{
114+
$this->log[] = get_class($pass).': '.$message;
115+
}
116+
101117
/**
102118
* Returns the log.
103119
*

src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ protected function processValue($value, $isRoot = false)
4343
return $value;
4444
}
4545
if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) {
46-
$compiler = $this->container->getCompiler();
4746
$definition = $this->container->getDefinition($id);
4847

49-
if ($this->isInlineableDefinition($id, $definition, $compiler->getServiceReferenceGraph())) {
50-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatInlineService($this, $id, $this->currentId));
48+
if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
49+
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
5150

5251
if ($definition->isShared()) {
5352
return $definition;

src/Symfony/Component/DependencyInjection/Compiler/LoggingFormatter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

14+
@trigger_error('The '.__NAMESPACE__.'\LoggingFormatter class is deprecated since version 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', E_USER_DEPRECATED);
15+
1416
/**
1517
* Used to format logging messages during the compilation.
1618
*
1719
* @author Johannes M. Schmitt <[email protected]>
20+
*
21+
* @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
1822
*/
1923
class LoggingFormatter
2024
{

src/Symfony/Component/DependencyInjection/Compiler/RemoveAbstractDefinitionsPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ class RemoveAbstractDefinitionsPass implements CompilerPassInterface
2626
public function process(ContainerBuilder $container)
2727
{
2828
$compiler = $container->getCompiler();
29-
$formatter = $compiler->getLoggingFormatter();
3029

3130
foreach ($container->getDefinitions() as $id => $definition) {
3231
if ($definition->isAbstract()) {
3332
$container->removeDefinition($id);
34-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'abstract'));
33+
$container->logRemoveService($this, $id, 'abstract');
3534
}
3635
}
3736
}

src/Symfony/Component/DependencyInjection/Compiler/RemovePrivateAliasesPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ class RemovePrivateAliasesPass implements CompilerPassInterface
3030
public function process(ContainerBuilder $container)
3131
{
3232
$compiler = $container->getCompiler();
33-
$formatter = $compiler->getLoggingFormatter();
3433

3534
foreach ($container->getAliases() as $id => $alias) {
3635
if ($alias->isPublic()) {
3736
continue;
3837
}
3938

4039
$container->removeAlias($id);
41-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'private alias'));
40+
$container->logRemoveService($this, $id, 'private alias');
4241
}
4342
}
4443
}

src/Symfony/Component/DependencyInjection/Compiler/RemoveUnusedDefinitionsPass.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function setRepeatedPass(RepeatedPass $repeatedPass)
3838
public function process(ContainerBuilder $container)
3939
{
4040
$compiler = $container->getCompiler();
41-
$formatter = $compiler->getLoggingFormatter();
4241
$graph = $compiler->getServiceReferenceGraph();
4342

4443
$hasChanged = false;
@@ -69,10 +68,10 @@ public function process(ContainerBuilder $container)
6968
$container->setDefinition((string) reset($referencingAliases), $definition);
7069
$definition->setPublic(true);
7170
$container->removeDefinition($id);
72-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'replaces alias '.reset($referencingAliases)));
71+
$container->logRemoveService($this, $id, 'replaces alias '.reset($referencingAliases));
7372
} elseif (0 === count($referencingAliases) && false === $isReferenced) {
7473
$container->removeDefinition($id);
75-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'unused'));
74+
$container->logRemoveService($this, $id, 'unused');
7675
$hasChanged = true;
7776
}
7877
}

src/Symfony/Component/DependencyInjection/Compiler/ReplaceAliasByActualDefinitionPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ protected function processValue($value, $isRoot = false)
8282
// Perform the replacement
8383
$newId = $this->replacements[$referenceId];
8484
$value = new Reference($newId, $value->getInvalidBehavior());
85-
$compiler = $this->container->getCompiler();
86-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatUpdateReference($this, $this->currentId, $referenceId, $newId));
85+
$this->container->log($this, sprintf('Changed reference of service "%s" previously pointing to "%s" to "%s".', $this->currentId, $referenceId, $newId));
8786
}
8887

8988
return parent::processValue($value, $isRoot);

src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ private function doResolveDefinition(ChildDefinition $definition)
8080
$this->currentId = $id;
8181
}
8282

83-
$compiler = $this->container->getCompiler();
84-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatResolveInheritance($this, $this->currentId, $parent));
83+
$this->container->log($this, sprintf('Resolving inheritance for "%s" (parent: %s).', $this->currentId, $parent));
8584
$def = new Definition();
8685

8786
// merge in parent definition

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,16 @@ public function getNormalizedIds()
11711171
return $normalizedIds;
11721172
}
11731173

1174+
public function logRemoveService(CompilerPassInterface $pass, $id, $reason)
1175+
{
1176+
$this->getCompiler()->log($pass, sprintf('Removed service "%s"; reason: %s.', $id, $reason));
1177+
}
1178+
1179+
public function log(CompilerPassInterface $pass, $message, $level = E_USER_NOTICE)
1180+
{
1181+
$this->getCompiler()->log($pass, $message, $level);
1182+
}
1183+
11741184
/**
11751185
* Returns the Service Conditionals.
11761186
*

0 commit comments

Comments
 (0)