[DI] [FrameworkBundle] Add LoggerAwareInterface to auto configuration

This commit is contained in:
Gary PEGEOT 2018-08-09 15:26:43 +01:00 committed by Nicolas Grekas
parent 00e8b493d1
commit afda3c8844
3 changed files with 22 additions and 0 deletions

View File

@ -8,6 +8,7 @@ CHANGELOG
* Allowed configuring PDO-based cache pools via a new `cache.adapter.pdo` abstract service
* Deprecated auto-injection of the container in AbstractController instances, register them as service subscribers instead
* Deprecated processing of services tagged `security.expression_language_provider` in favor of a new `AddExpressionLanguageProvidersPass` in SecurityBundle.
* Enabled autoconfiguration for `Psr\Log\LoggerAwareInterface`
4.1.0
-----

View File

@ -13,6 +13,7 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\Reader;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Bridge\Monolog\Processor\ProcessorInterface;
use Symfony\Bridge\Twig\Extension\CsrfExtension;
@ -360,6 +361,8 @@ class FrameworkExtension extends Extension
->addTag('messenger.message_handler');
$container->registerForAutoconfiguration(TransportFactoryInterface::class)
->addTag('messenger.transport_factory');
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
->addMethodCall('setLogger', array(new Reference('logger')));
if (!$container->getParameter('kernel.debug')) {
// remove tagged iterator argument for resource checkers

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection;
use Doctrine\Common\Annotations\Annotation;
use Psr\Log\LoggerAwareInterface;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
@ -26,6 +27,7 @@ use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
@ -1224,6 +1226,22 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertEmpty($container->getDefinition('config_cache_factory')->getArguments());
}
public function testLoggerAwareRegistration()
{
$container = $this->createContainerFromFile('full', array(), true, false);
$container->addCompilerPass(new ResolveInstanceofConditionalsPass());
$container->register('foo', LoggerAwareInterface::class)
->setAutoconfigured(true);
$container->compile();
$calls = $container->findDefinition('foo')->getMethodCalls();
$this->assertCount(1, $calls, 'Definition should contain 1 method call');
$this->assertSame('setLogger', $calls[0][0], 'Method name should be "setLogger"');
$this->assertInstanceOf(Reference::class, $calls[0][1][0]);
$this->assertSame('logger', (string) $calls[0][1][0], 'Argument should be a reference to "logger"');
}
protected function createContainer(array $data = array())
{
return new ContainerBuilder(new ParameterBag(array_merge(array(