8000 Leverage AsMonologProcessor attribute by fancyweb · Pull Request #428 · symfony/monolog-bundle · GitHub
[go: up one dir, main page]

Skip to content

Leverage AsMonologProcessor attribute #428

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

Merged
merged 1 commit into from
May 10, 2022
Merged
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
Leverage AsMonologProcessor attribute
  • Loading branch information
fancyweb committed Mar 7, 2022
commit f1d87f0f6c20228300e3ada3d8e2bd7a3cfacf14
16 changes: 16 additions & 0 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\MonologBundle\DependencyInjection;

use Monolog\Attribute\AsMonologProcessor;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Logger;
use Monolog\Processor\ProcessorInterface;
Expand Down Expand Up @@ -148,6 +149,21 @@ public function load(array $configs, ContainerBuilder $container)
]);
}
}

if (80000 <= \PHP_VERSION_ID && method_exists($container, 'registerAttributeForAutoconfiguration')) {
$container->registerAttributeForAutoconfiguration(AsMonologProcessor::class, static function (ChildDefinition $definition, AsMonologProcessor $attribute, \Reflector $reflector): void {
$tagAttributes = get_object_vars($attribute);
if ($reflector instanceof \ReflectionMethod) {
if (isset($tagAttributes['method'])) {
throw new \LogicException(sprintf('AsMonologProcessor attribute cannot declare a method on "%s::%s()".', $reflector->class, $reflector->name));
}

$tagAttributes['method'] = $reflector->getName();
}

$definition->addTag('monolog.processor', $tagAttributes);
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor;

use Monolog\Attribute\AsMonologProcessor;

#[AsMonologProcessor(handler: 'foo_handler')]
class FooProcessor
{
#[AsMonologProcessor(channel: 'ccc_channel')]
public function __invoke(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor;

use Monolog\Attribute\AsMonologProcessor;

class RedeclareMethodProcessor
{
#[AsMonologProcessor(method: 'foobar')]
public function __invoke(): void
{
}
}
47 changes: 47 additions & 0 deletions Tests/DependencyInjection/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;

use InvalidArgumentException;
use Monolog\Attribute\AsMonologProcessor;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\RollbarHandler;
use Monolog\Logger;
use Monolog\Processor\UidProcessor;
use Symfony\Bridge\Monolog\Processor\SwitchUserTokenProcessor;
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\FooProcessor;
use Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
Expand Down Expand Up @@ -816,6 +819,50 @@ public function testProcessorAutoConfiguration()
$this->assertEquals('reset', $tags['kernel.reset'][0]['method']);
}

/**
* @requires PHP 8.0
*/
public function testAsMonologProcessorAutoconfigurationRedeclareMethod(): void
{
if (!\class_exists(AsMonologProcessor::class, true)) {
$this->markTestSkipped('Monolog >= 2.3.6 is needed.');
}

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('AsMonologProcessor attribute cannot declare a method on "Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Fixtures\AsMonologProcessor\RedeclareMethodProcessor::__invoke()".');

$this->getContainer([], [
RedeclareMethodProcessor::class => (new Definition(RedeclareMethodProcessor::class))->setAutoconfigured(true),
]);
}

/**
* @requires PHP 8.0
*/
public function testAsMonologProcessorAutoconfiguration(): void
{
if (!\class_exists(AsMonologProcessor::class, true)) {
$this->markTestSkipped('Monolog >= 2.3.6 is needed.');
}

$container = $this->getContainer([], [
FooProcessor::class => (new Definition(FooProcessor::class))->setAutoconfigured(true),
]);

$this->assertSame([
[
'channel' => null,
'handler' => 'foo_handler',
'method' => null,
],
[
'channel' => 'ccc_channel',
'handler' => null,
'method' => '__invoke',
],
], $container->getDefinition(FooProcessor::class)->getTag('monolog.processor'));
}

protected function getContainer(array $config = [], array $thirdPartyDefinitions = [])
{
$container = new ContainerBuilder(new EnvPlaceholderParameterBag());
Expand Down
0