Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add priority field to processor tags #455

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 51 additions & 26 deletions DependencyInjection/Compiler/AddProcessorsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,65 @@ public function process(ContainerBuilder $container)
return;
}

$processors = [];

foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) {
foreach ($tags as $tag) {
if (!empty($tag['channel']) && !empty($tag['handler'])) {
throw new \InvalidArgumentException(sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id));
if (!isset($tag['priority'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the priority option also be added to #[AsMonologProcessor] ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should be able to add it without a problem, thanks for the suggestion!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: 1797

$tag['priority'] = 0;
}

if (!empty($tag['handler'])) {
$definition = $container->findDefinition(sprintf('monolog.handler.%s', $tag['handler']));
$parentDef = $definition;
while (!$parentDef->getClass() && $parentDef instanceof ChildDefinition) {
$parentDef = $container->findDefinition($parentDef->getParent());
}
$class = $container->getParameterBag()->resolveValue($parentDef->getClass());
if (!method_exists($class, 'pushProcessor')) {
throw new \InvalidArgumentException(sprintf('The "%s" handler does not accept processors', $tag['handler']));
}
} elseif (!empty($tag['channel'])) {
if ('app' === $tag['channel']) {
$definition = $container->getDefinition('monolog.logger');
} else {
$definition = $container->getDefinition(sprintf('monolog.logger.%s', $tag['channel']));
}
} else {
$definition = $container->getDefinition('monolog.logger_prototype');
}
$processors[] = [
'id' => $id,
'tag' => $tag,
];
}
}

// Sort by priority so that higher-prio processors are added last.
// The effect is the monolog will call the higher-prio processors first
usort(
$processors,
function (array $left, array $right) {
return $left['tag']['priority'] <=> $right['tag']['priority'];
}
Comment on lines +49 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the PriorityTaggedServiceTrait be used here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion, however the PriorityTaggedServiceTrait can´t be used since it will require some changes because the Trait sorts it ascending (as expected). But monologs pushProcessor actually array_shifts instead of adding it on top of the stack, so I need a descending sort. Which is clarified in the comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could still use the trait to get the sorted services and reverse the list before adding method calls.

);

if (!empty($tag['method'])) {
$processor = [new Reference($id), $tag['method']];
foreach ($processors as $processor) {
$tag = $processor['tag'];
$id = $processor['id'];

if (!empty($tag['channel']) && !empty($tag['handler'])) {
throw new \InvalidArgumentException(sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id));
}

if (!empty($tag['handler'])) {
$definition = $container->findDefinition(sprintf('monolog.handler.%s', $tag['handler']));
$parentDef = $definition;
while (!$parentDef->getClass() && $parentDef instanceof ChildDefinition) {
$parentDef = $container->findDefinition($parentDef->getParent());
}
$class = $container->getParameterBag()->resolveValue($parentDef->getClass());
if (!method_exists($class, 'pushProcessor')) {
throw new \InvalidArgumentException(sprintf('The "%s" handler does not accept processors', $tag['handler']));
}
} elseif (!empty($tag['channel'])) {
if ('app' === $tag['channel']) {
$definition = $container->getDefinition('monolog.logger');
} else {
// If no method is defined, fallback to use __invoke
$processor = new Reference($id);
$definition = $container->getDefinition(sprintf('monolog.logger.%s', $tag['channel']));
}
$definition->addMethodCall('pushProcessor', [$processor]);
} else {
$definition = $container->getDefinition('monolog.logger_prototype');
}

if (!empty($tag['method'])) {
$processor = [new Reference($id), $tag['method']];
} else {
// If no method is defined, fallback to use __invoke
$processor = new Reference($id);
}
$definition->addMethodCall('pushProcessor', [$processor]);
}
}
}
21 changes: 21 additions & 0 deletions Tests/DependencyInjection/Compiler/AddProcessorsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public function testHandlerProcessors()
$calls = $service->getMethodCalls();
$this->assertCount(1, $calls);
$this->assertEquals(['pushProcessor', [new Reference('test2')]], $calls[0]);

$service = $container->getDefinition('monolog.handler.priority_test');
$calls = $service->getMethodCalls();
$this->assertCount(3, $calls);
$this->assertEquals(['pushProcessor', [new Reference('processor-10')]], $calls[0]);
$this->assertEquals(['pushProcessor', [new Reference('processor+10')]], $calls[1]);
$this->assertEquals(['pushProcessor', [new Reference('processor+20')]], $calls[2]);
}

public function testFailureOnHandlerWithoutPushProcessor()
Expand Down Expand Up @@ -76,9 +83,11 @@ protected function getContainer()
$container->setParameter('monolog.handler.console.class', ConsoleHandler::class);
$container->setDefinition('monolog.handler.test', new Definition('%monolog.handler.console.class%', [100, false]));
$container->setDefinition('handler_test', new Definition('%monolog.handler.console.class%', [100, false]));
$container->setDefinition('monolog.handler.priority_test', new Definition('%monolog.handler.console.class%', [100, false]));
$container->setAlias('monolog.handler.test2', 'handler_test');
$definition->addMethodCall('pushHandler', [new Reference('monolog.handler.test')]);
$definition->addMethodCall('pushHandler', [new Reference('monolog.handler.test2')]);
$definition->addMethodCall('pushHandler', [new Reference('monolog.handler.priority_test')]);

$service = new Definition('TestClass', ['false', new Reference('logger')]);
$service->addTag('monolog.processor', ['handler' => 'test']);
Expand All @@ -88,6 +97,18 @@ protected function getContainer()
$service->addTag('monolog.processor', ['handler' => 'test2']);
$container->setDefinition('test2', $service);

$service = new Definition('TestClass', ['false', new Reference('logger')]);
$service->addTag('monolog.processor', ['handler' => 'priority_test', 'priority' => 10]);
$container->setDefinition('processor+10', $service);

$service = new Definition('TestClass', ['false', new Reference('logger')]);
$service->addTag('monolog.processor', ['handler' => 'priority_test', 'priority' => -10]);
$container->setDefinition('processor-10', $service);

$service = new Definition('TestClass', ['false', new Reference('logger')]);
$service->addTag('monolog.processor', ['handler' => 'priority_test', 'priority' => 20]);
$container->setDefinition('processor+20', $service);

$container->getCompilerPassConfig()->setOptimizationPasses([]);
$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->addCompilerPass(new AddProcessorsPass());
Expand Down