Skip to content

Allow setting priority for processors #506

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

Open
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 0 additions & 70 deletions DependencyInjection/Compiler/AddProcessorsPass.php

This file was deleted.

69 changes: 69 additions & 0 deletions DependencyInjection/Compiler/LoggerChannelPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
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\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
Expand All @@ -28,6 +30,8 @@
*/
class LoggerChannelPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;

protected $channels = ['app'];

public function process(ContainerBuilder $container)
Expand Down Expand Up @@ -103,6 +107,8 @@ public function process(ContainerBuilder $container)
$logger->addMethodCall('pushHandler', [new Reference($handler)]);
}
}

$this->addProcessors($container);
}

/**
Expand Down Expand Up @@ -155,4 +161,67 @@ private function changeReference(Reference $reference, string $serviceId): Refer
{
return new Reference($serviceId, $reference->getInvalidBehavior());
}

private function addProcessors(ContainerBuilder $container)
{
$indexedTags = [];
$i = 1;

foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) {
foreach ($tags as &$tag) {
$indexedTags[$tag['index'] = $i++] = $tag;
}
unset($tag);
$definition = $container->getDefinition($id);
$definition->setTags(array_merge($definition->getTags(), ['monolog.processor' => $tags]));
}

$taggedIteratorArgument = new TaggedIteratorArgument('monolog.processor', 'index', null, true);
// array_reverse is used because ProcessableHandlerTrait::pushProcessor prepends processors to the beginning of the stack
foreach (array_reverse($this->findAndSortTaggedServices($taggedIteratorArgument, $container), true) as $index => $reference) {
$tag = $indexedTags[$index];

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"', $reference));
}

if (!empty($tag['handler'])) {
$parentDef = $container->findDefinition(\sprintf('monolog.handler.%s', $tag['handler']));
$definitions = [$parentDef];
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']) {
$definitions = [$container->getDefinition('monolog.logger')];
} else {
$definitions = [$container->getDefinition(\sprintf('monolog.logger.%s', $tag['channel']))];
}
} else {
$definitions = [$container->getDefinition('monolog.logger')];
foreach ($this->channels as $channel) {
if ('app' === $channel) {
continue;
}

$definitions[] = $container->getDefinition(\sprintf('monolog.logger.%s', $channel));
}
}

if (!empty($tag['method'])) {
$processor = [$reference, $tag['method']];
} else {
// If no method is defined, fallback to use __invoke
$processor = $reference;
}

foreach ($definitions as $definition) {
$definition->addMethodCall('pushProcessor', [$processor]);
}
}
}
}
2 changes: 0 additions & 2 deletions MonologBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Monolog\Formatter\JsonFormatter;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\HandlerInterface;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddProcessorsPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddSwiftMailerTransportPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\FixEmptyLoggerPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
Expand All @@ -37,7 +36,6 @@ public function build(ContainerBuilder $container)

$container->addCompilerPass($channelPass = new LoggerChannelPass());
$container->addCompilerPass(new FixEmptyLoggerPass($channelPass));
$container->addCompilerPass(new AddProcessorsPass());
$container->addCompilerPass(new AddSwiftMailerTransportPass());
}

Expand Down
97 changes: 0 additions & 97 deletions Tests/DependencyInjection/Compiler/AddProcessorsPassTest.php

This file was deleted.

Loading
Loading