Skip to content

Commit b95c929

Browse files
committed
Use PriorityTaggedServiceTrait
1 parent aebea64 commit b95c929

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

DependencyInjection/Compiler/AddProcessorsPass.php

+31-52
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use Symfony\Component\DependencyInjection\ChildDefinition;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
17-
use Symfony\Component\DependencyInjection\Reference;
1818

1919
/**
2020
* Registers processors in Monolog loggers or handlers.
@@ -25,71 +25,50 @@
2525
*/
2626
class AddProcessorsPass implements CompilerPassInterface
2727
{
28+
use PriorityTaggedServiceTrait;
29+
2830
public function process(ContainerBuilder $container)
2931
{
3032
if (!$container->hasDefinition('monolog.logger')) {
3133
return;
3234
}
3335

34-
$processors = [];
36+
foreach (array_reverse($this->findAndSortTaggedServices('monolog.processor', $container)) as $reference) {
37+
$tags = $container->getDefinition((string) $reference)->getTag('monolog.processor');
3538

36-
foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) {
3739
foreach ($tags as $tag) {
38-
if (!isset($tag['priority'])) {
39-
$tag['priority'] = 0;
40+
if (!empty($tag['channel']) && !empty($tag['handler'])) {
41+
throw new \InvalidArgumentException(\sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $reference));
4042
}
4143

42-
$processors[] = [
43-
'id' => $id,
44-
'tag' => $tag,
45-
];
46-
}
47-
}
48-
49-
// Sort by priority so that higher-prio processors are added last.
50-
// The effect is the monolog will call the higher-prio processors first
51-
usort(
52-
$processors,
53-
function (array $left, array $right) {
54-
return $left['tag']['priority'] <=> $right['tag']['priority'];
55-
}
56-
);
57-
58-
foreach ($processors as $processor) {
59-
$tag = $processor['tag'];
60-
$id = $processor['id'];
61-
62-
if (!empty($tag['channel']) && !empty($tag['handler'])) {
63-
throw new \InvalidArgumentException(sprintf('you cannot specify both the "handler" and "channel" attributes for the "monolog.processor" tag on service "%s"', $id));
64-
}
65-
66-
if (!empty($tag['handler'])) {
67-
$definition = $container->findDefinition(sprintf('monolog.handler.%s', $tag['handler']));
68-
$parentDef = $definition;
69-
while (!$parentDef->getClass() && $parentDef instanceof ChildDefinition) {
70-
$parentDef = $container->findDefinition($parentDef->getParent());
71-
}
72-
$class = $container->getParameterBag()->resolveValue($parentDef->getClass());
73-
if (!method_exists($class, 'pushProcessor')) {
74-
throw new \InvalidArgumentException(sprintf('The "%s" handler does not accept processors', $tag['handler']));
75-
}
76-
} elseif (!empty($tag['channel'])) {
77-
if ('app' === $tag['channel']) {
78-
$definition = $container->getDefinition('monolog.logger');
44+
if (!empty($tag['handler'])) {
45+
$definition = $container->findDefinition(\sprintf('monolog.handler.%s', $tag['handler']));
46+
$parentDef = $definition;
47+
while (!$parentDef->getClass() && $parentDef instanceof ChildDefinition) {
48+
$parentDef = $container->findDefinition($parentDef->getParent());
49+
}
50+
$class = $container->getParameterBag()->resolveValue($parentDef->getClass());
51+
if (!method_exists($class, 'pushProcessor')) {
52+
throw new \InvalidArgumentException(\sprintf('The "%s" handler does not accept processors', $tag['handler']));
53+
}
54+
} elseif (!empty($tag['channel'])) {
55+
if ('app' === $tag['channel']) {
56+
$definition = $container->getDefinition('monolog.logger');
57+
} else {
58+
$definition = $container->getDefinition(\sprintf('monolog.logger.%s', $tag['channel']));
59+
}
7960
} else {
80-
$definition = $container->getDefinition(sprintf('monolog.logger.%s', $tag['channel']));
61+
$definition = $container->getDefinition('monolog.logger_prototype');
8162
}
82-
} else {
83-
$definition = $container->getDefinition('monolog.logger_prototype');
84-
}
8563

86-
if (!empty($tag['method'])) {
87-
$processor = [new Reference($id), $tag['method']];
88-
} else {
89-
// If no method is defined, fallback to use __invoke
90-
$processor = new Reference($id);
64+
if (!empty($tag['method'])) {
65+
$processor = [$reference, $tag['method']];
66+
} else {
67+
// If no method is defined, fallback to use __invoke
68+
$processor = $reference;
69+
}
70+
$definition->addMethodCall('pushProcessor', [$processor]);
9171
}
92-
$definition->addMethodCall('pushProcessor', [$processor]);
9372
}
9473
}
9574
}

0 commit comments

Comments
 (0)