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 #485

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
15 changes: 10 additions & 5 deletions DependencyInjection/Compiler/AddProcessorsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Registers processors in Monolog loggers or handlers.
Expand All @@ -25,16 +25,21 @@
*/
class AddProcessorsPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('monolog.logger')) {
return;
}

foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) {
// array_reverse is used because ProcessableHandlerTrait::pushProcessor prepends processors to the beginning of the stack
foreach (array_reverse($this->findAndSortTaggedServices('monolog.processor', $container)) as $reference) {
Copy link

Choose a reason for hiding this comment

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

perhaps comment why array_reverse is used ?
otherwise its smart :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, done

Copy link
Member

Choose a reason for hiding this comment

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

how does this behave when we have multiple tags (for instance when registering the processor in specific channels or in specific handlers ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stof Good catch, it didn't work. I've managed to fix it. Please let me know what you think of the fix. It's probably easiest to review it with whitespace changes ignored.

$tags = $container->getDefinition((string) $reference)->getTag('monolog.processor');

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));
throw new \InvalidArgumentException(\sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $reference));
}

if (!empty($tag['handler'])) {
Expand All @@ -58,10 +63,10 @@ public function process(ContainerBuilder $container)
}

if (!empty($tag['method'])) {
$processor = [new Reference($id), $tag['method']];
$processor = [$reference, $tag['method']];
} else {
// If no method is defined, fallback to use __invoke
$processor = new Reference($id);
$processor = $reference;
}
$definition->addMethodCall('pushProcessor', [$processor]);
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/DependencyInjection/Compiler/AddProcessorsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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 @@ -75,9 +82,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 @@ -87,6 +96,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
Loading