Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] [DI] Automatically add "setLogger" method call. #24973

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

Closed
wants to merge 7 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

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

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Psr\Log\LoggerAwareInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Automatically add setLogger method call to any service that implements Psr\Log\LoggerAwareInterface.
*
* @see http://www.php-fig.org/psr/psr-3/
*
* @author Gary PEGEOT <[email protected]>
*/
class LoggerAwarePass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('logger') && !$container->hasAlias('logger')) {
return;
}

$reference = new Reference('logger');

foreach (\array_keys($container->findTaggedServiceIds('logger.aware')) as $id) {
$definition = $container->findDefinition($id);
$class = $container->getReflectionClass($definition->getClass());

if (!$definition->hasMethodCall('setLogger') && $class && $class->implementsInterface(LoggerAwareInterface::class)) {
$definition->addMethodCall('setLogger', array($reference));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;

use Doctrine\Common\Annotations\Reader;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand;
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
Expand Down Expand Up @@ -294,6 +295,8 @@ public function load(array $configs, ContainerBuilder $container)
->addTag('kernel.event_subscriber');
$container->registerForAutoconfiguration(ResettableInterface::class)
->addTag('kernel.reset', array('method' => 'reset'));
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
->addTag('logger.aware');
$container->registerForAutoconfiguration(PropertyListExtractorInterface::class)
->addTag('property_info.list_extractor');
$container->registerForAutoconfiguration(PropertyTypeExtractorInterface::class)
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPrunerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerAwarePass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
Expand Down Expand Up @@ -114,6 +115,7 @@ public function build(ContainerBuilder $container)
$this->addCompilerPassIfExists($container, FormPass::class);
$container->addCompilerPass(new WorkflowGuardListenerPass());
$container->addCompilerPass(new ResettableServicePass());
$container->addCompilerPass(new LoggerAwarePass());

if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

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

namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerAwarePass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Unit tests for Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerAwarePass.
*
* @author Gary PEGEOT <[email protected]>
*/
class LoggerAwarePassTest extends TestCase
{
/**
* @covers \Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggerAwarePass::process()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont think this is needed really.

*/
public function testProcess()
{
$container = new ContainerBuilder();
$container->register('logger', LoggerInterface::class);

$definition = $container->register('foo', get_class($this->createMock(LoggerAwareInterface::class)))
->addTag('logger.aware');

$container->register('bar', 'stdClass');
$container->register('not.autowired', LoggerInterface::class);
$this->assertFalse(
$definition->hasMethodCall('setLogger'),
'Service should not have "setLogger" method call yet.'
);

(new LoggerAwarePass())->process($container);

$this->assertTrue($definition->hasMethodCall('setLogger'), 'Service should have "setLogger" method call.');

$this->assertFalse(
$container->findDefinition('bar')->hasMethodCall('setLogger'),
'"bar" service should not be affected'
);

$this->assertFalse(
$container->findDefinition('not.autowired')->hasMethodCall('setLogger'),
'Not autoconfigured service should not be affected.'
);
}
}