8000 Leverage AsMonologProcessor attribute · symfony/monolog-bundle@f1d87f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1d87f0

Browse files
committed
Leverage AsMonologProcessor attribute
1 parent fde12fc commit f1d87f0

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

DependencyInjection/MonologExtension.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\MonologBundle\DependencyInjection;
1313

14+
use Monolog\Attribute\AsMonologProcessor;
1415
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1516
use Monolog\Logger;
1617
use Monolog\Processor\ProcessorInterface;
@@ -148,6 +149,21 @@ public function load(array $configs, ContainerBuilder $container)
148149
]);
149150
}
150151
}
152+
153+
if (80000 <= \PHP_VERSION_ID && method_exists($container, 'registerAttributeForAutoconfiguration')) {
154+
$container->registerAttributeForAutoconfiguration(AsMonologProcessor::class, static function (ChildDefinition $definition, AsMonologProcessor $attribute, \Reflector $reflector): void {
155+
$tagAttributes = get_object_vars($attribute);
156+
if ($reflector instanceof \ReflectionMethod) {
157+
if (isset($tagAttributes['method'])) {
158+
throw new \LogicException(sprintf('AsMonologProcessor attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
159+
}
160+
161+
$tagAttributes['method'] = $reflector->getName();
162+
}
163+
164+
$definition->addTag('monolog.processor', $tagAttributes);
165+
});
166+
}
151167
}
152168

153169
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor;
13+
14+
use Monolog\Attribute\AsMonologProcessor;
15+
16+
#[AsMonologProcessor(handler: 'foo_handler')]
17+
class FooProcessor
18+
{
19+
#[AsMonologProcessor(channel: 'ccc_channel')]
20+
public function __invoke(): void
21+
{
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor;
13+
14+
use Monolog\Attribute\AsMonologProcessor;
15+
16+
class RedeclareMethodProcessor
17+
{
18+
#[AsMonologProcessor(method: 'foobar')]
19+
public function __invoke(): void
20+
{
21+
}
22+
}

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;
1313

1414
use InvalidArgumentException;
15+
use Monolog\Attribute\AsMonologProcessor;
1516
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
1617
use Monolog\Handler\RollbarHandler;
1718
use Monolog\Logger;
1819
use Monolog\Processor\UidProcessor;
1920
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
2021
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
2122
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
23+
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessor;
24+
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor;
2225
use Symfony\Component\DependencyInjection\ContainerBuilder;
2326
use Symfony\Component\DependencyInjection\Definition;
2427
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
@@ -816,6 +819,50 @@ public function testProcessorAutoConfiguration()
816819
$this->assertEquals('reset', $tags['kernel.reset'][0]['method']);
817820
}
818821

822+
/**
823+
* @requires PHP 8.0
824+
*/
825+
public function testAsMonologProcessorAutoconfigurationRedeclareMethod(): void
826+
{
827+
if (!\class_exists(AsMonologProcessor::class, true)) {
828+
$this->markTestSkipped('Monolog >= 2.3.6 is needed.');
829+
}
830+
831+
$this->expectException(\LogicException::class);
832+
$this->expectExceptionMessage('AsMonologProcessor attribute cannot declare a method on "Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor::__invoke()".');
833+
834+
$this->getContainer([], [
835+
RedeclareMethodProcessor::class => (new Definition(RedeclareMethodProcessor::class))->setAutoconfigured(true),
836+
]);
837+
}
838+
839+
/**
840+
* @requires PHP 8.0
841+
*/
842+
public function testAsMonologProcessorAutoconfiguration(): void
843+
{
844+
if (!\class_exists(AsMonologProcessor::class, true)) {
845+
$this->markTestSkipped('Monolog >= 2.3.6 is needed.');
846+
}
847+
848+
$container = $this->getContainer([], [
849+
FooProcessor::class => (new Definition(FooProcessor::class))->setAutoconfigured(true),
850+
]);
851+
852+
$this->assertSame([
853+
[
854+
'channel' => null,
855+
'handler' => 'foo_handler',
856+
'method' => null,
857+
],
858+
[
859+
'channel' => 'ccc_channel',
860+
'handler' => null,
861+
'method' => '__invoke',
862+
],
863+
], $container->getDefinition(FooProcessor::class)->getTag('monolog.processor'));
864+
}
865+
819866
protected function getContainer(array $config = [], array $thirdPartyDefinitions = [])
820867
{
821868
$container = new ContainerBuilder(new EnvPlaceholderParameterBag());

0 commit comments

Comments
 (0)
0