Skip to content

Add priority field to processor tags #421

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions DependencyInjection/Compiler/AddProcessorsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,57 @@ 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'])) {
$tag['priority'] = 0;
}

if (!empty($tag['handler'])) {
$definition = $container->findDefinition(sprintf('monolog.handler.%s', $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'];
Copy link
Member

Choose a reason for hiding this comment

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

The spaceship operator is designed for this use-case.

Suggested change
return $left['tag']['priority'] - $right['tag']['priority'];
return $left['tag']['priority'] <=> $right['tag']['priority'];

}
);

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['method'])) {
$processor = [new Reference($id), $tag['method']];
if (!empty($tag['handler'])) {
$definition = $container->findDefinition(sprintf('monolog.handler.%s', $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 @@ -34,6 +34,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]);
}

protected function getContainer()
Expand All @@ -45,9 +52,11 @@ protected function getContainer()
$definition = $container->getDefinition('monolog.logger_prototype');
$container->setDefinition('monolog.handler.test', new Definition('%monolog.handler.null.class%', [100, false]));
$container->setDefinition('handler_test', new Definition('%monolog.handler.null.class%', [100, false]));
$container->setDefinition('monolog.handler.priority_test', new Definition('%monolog.handler.null.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 @@ -57,6 +66,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