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

Skip to content

Commit c9fea7a

Browse files
committed
Add AsMonologProcessor attribute
1 parent fde12fc commit c9fea7a

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

Attribute/AsMonologProcessor.php

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\Attribute;
13+
14+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
15+
class AsMonologProcessor
16+
{
17+
public function __construct(
18+
public ?string $channel = null,
19+
public ?string $handler = null,
20+
public ?string $method = null,
21+
) {
22+
}
23+
}

DependencyInjection/MonologExtension.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Bridge\Monolog\Processor\TokenProcessor;
2222
use Symfony\Bridge\Monolog\Processor\WebProcessor;
2323
use Symfony\Bundle\FullStack;
24+
use Symfony\Bundle\MonologBundle\Attribute\AsMonologProcessor;
2425
use Symfony\Component\Config\FileLocator;
2526
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
2627
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -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 Symfony\Bundle\MonologBundle\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 Symfony\Bundle\MonologBundle\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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
2020
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
2121
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
22+
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessor;
23+
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor;
2224
use Symfony\Component\DependencyInjection\ContainerBuilder;
2325
use Symfony\Component\DependencyInjection\Definition;
2426
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
@@ -816,6 +818,42 @@ public function testProcessorAutoConfiguration()
816818
$this->assertEquals('reset', $tags['kernel.reset'][0]['method']);
817819
}
818820

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

0 commit comments

Comments
 (0)
0