|
13 | 13 |
|
14 | 14 | use Symfony\Component\DependencyInjection\ChildDefinition;
|
15 | 15 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
| 16 | +use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; |
16 | 17 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
17 |
| -use Symfony\Component\DependencyInjection\Reference; |
18 | 18 |
|
19 | 19 | /**
|
20 | 20 | * Registers processors in Monolog loggers or handlers.
|
|
25 | 25 | */
|
26 | 26 | class AddProcessorsPass implements CompilerPassInterface
|
27 | 27 | {
|
| 28 | + use PriorityTaggedServiceTrait; |
| 29 | + |
28 | 30 | public function process(ContainerBuilder $container)
|
29 | 31 | {
|
30 | 32 | if (!$container->hasDefinition('monolog.logger')) {
|
31 | 33 | return;
|
32 | 34 | }
|
33 | 35 |
|
34 |
| - $processors = []; |
| 36 | + foreach (array_reverse($this->findAndSortTaggedServices('monolog.processor', $container)) as $reference) { |
| 37 | + $tags = $container->getDefinition((string) $reference)->getTag('monolog.processor'); |
35 | 38 |
|
36 |
| - foreach ($container->findTaggedServiceIds('monolog.processor') as $id => $tags) { |
37 | 39 | 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)); |
40 | 42 | }
|
41 | 43 |
|
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 | + } |
79 | 60 | } else {
|
80 |
| - $definition = $container->getDefinition(sprintf('monolog.logger.%s', $tag['channel'])); |
| 61 | + $definition = $container->getDefinition('monolog.logger_prototype'); |
81 | 62 | }
|
82 |
| - } else { |
83 |
| - $definition = $container->getDefinition('monolog.logger_prototype'); |
84 |
| - } |
85 | 63 |
|
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]); |
91 | 71 | }
|
92 |
| - $definition->addMethodCall('pushProcessor', [$processor]); |
93 | 72 | }
|
94 | 73 | }
|
95 | 74 | }
|
0 commit comments