Skip to content
Snippets Groups Projects
Verified Commit 931ad45c authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3395032 by kim.pepper, acbramley, longwave: Add autoconfigure for module loggers

(cherry picked from commit 3d917f37)
parent ac7227ef
Branches
Tags
20 merge requests!8376Drupal views: adding more granularity to the ‘use ajax’ functionality,!8300Issue #3443586 View area displays even when parent view has no results.,!7567Issue #3153723 by quietone, Hardik_Patel_12: Change the scaffolding...,!7565Issue #3153723 by quietone, Hardik_Patel_12: Change the scaffolding...,!7509Change label "Block description" to "Block type",!7344Issue #3292350 by O'Briat, KlemenDEV, hswong3i, smustgrave, quietone: Update...,!6922Issue #3412959 by quietone, smustgrave, longwave: Fix 12 'un' words,!6848Issue #3417553 by longwave: Remove withConsecutive() in CacheCollectorTest,!6720Revert "Issue #3358581 by pfrenssen, _tarik_, a.dmitriiev, smustgrave:...,!6560Update ClaroPreRender.php, confirming classes provided are in array format,!6528Issue #3414261 by catch: Add authenticated user umami performance tests,!6501Issue #3263668 by omkar-pd, Wim Leers, hooroomoo: Re-enable inline form errors...,!6354Draft: Issue #3380392 by phma: Updating language weight from the overview reverts label if translated,!6324Issue #3416723 by Ludo.R: Provide a "node type" views default argument,!6119Issue #3405704 by Spokje, longwave: symfony/psr-http-message-bridge major version bump,!5950Issue #3403653 by alexpott, longwave: Incorporate improvements to how contrib runs PHPStan to core,!5858Issue #3401971 by fjgarlin: Test-only job shouldn't require constant rebases...,!5716Draft: Issue #3401102 by Spokje, longwave, smustgrave: Nightwatch artifacts on GitLab not retained,!5674Transaction autocommit during shutdown relies on unreliable object destruction order,!5644Issue #3395563 by nireneko, marvil07, lauriii, borisson_, smustgrave, Wim...
Pipeline #41350 passed
Pipeline: drupal

#41354

    Pipeline: drupal

    #41353

      Pipeline: drupal

      #41352

        +1
        ......@@ -9,22 +9,24 @@
        use Drupal\Core\DependencyInjection\Compiler\CorsCompilerPass;
        use Drupal\Core\DependencyInjection\Compiler\DeprecatedServicePass;
        use Drupal\Core\DependencyInjection\Compiler\DevelopmentSettingsPass;
        use Drupal\Core\DependencyInjection\Compiler\LoggerAwarePass;
        use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
        use Drupal\Core\DependencyInjection\Compiler\ProxyServicesPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterAccessChecksPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
        use Drupal\Core\DependencyInjection\Compiler\StackedKernelPass;
        use Drupal\Core\DependencyInjection\Compiler\StackedSessionHandlerPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterStreamWrappersPass;
        use Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass;
        use Drupal\Core\DependencyInjection\Compiler\TwigExtensionPass;
        use Drupal\Core\DependencyInjection\ContainerBuilder;
        use Drupal\Core\DependencyInjection\ServiceModifierInterface;
        use Drupal\Core\DependencyInjection\ServiceProviderInterface;
        use Drupal\Core\DependencyInjection\ContainerBuilder;
        use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass;
        use Drupal\Core\DependencyInjection\Compiler\TaggedHandlersPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterAccessChecksPass;
        use Drupal\Core\DependencyInjection\Compiler\RegisterServicesForDestructionPass;
        use Drupal\Core\Plugin\PluginManagerPass;
        use Drupal\Core\Render\MainContent\MainContentRenderersPass;
        use Drupal\Core\Site\Settings;
        use Psr\Log\LoggerAwareInterface;
        use Symfony\Component\DependencyInjection\Compiler\PassConfig;
        use Symfony\Component\DependencyInjection\Reference;
        use Symfony\Component\EventDispatcher\EventSubscriberInterface;
        ......@@ -81,6 +83,7 @@ public function register(ContainerBuilder $container) {
        // Add a compiler pass for registering event subscribers.
        $container->addCompilerPass(new RegisterEventSubscribersPass(), PassConfig::TYPE_AFTER_REMOVING);
        $container->addCompilerPass(new LoggerAwarePass(), PassConfig::TYPE_AFTER_REMOVING);
        $container->addCompilerPass(new RegisterAccessChecksPass());
        ......@@ -99,6 +102,10 @@ public function register(ContainerBuilder $container) {
        $container->registerForAutoconfiguration(EventSubscriberInterface::class)
        ->addTag('event_subscriber');
        $container->registerForAutoconfiguration(LoggerAwareInterface::class)
        ->addTag('logger_aware');
        }
        /**
        ......
        <?php
        namespace Drupal\Core\DependencyInjection\Compiler;
        use Psr\Log\LoggerAwareInterface;
        use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
        use Symfony\Component\DependencyInjection\ContainerBuilder;
        use Symfony\Component\DependencyInjection\Reference;
        /**
        * Sets the logger on all services that implement LoggerAwareInterface.
        */
        class LoggerAwarePass implements CompilerPassInterface {
        /**
        * {@inheritdoc}
        */
        public function process(ContainerBuilder $container): void {
        $interface = LoggerAwareInterface::class;
        foreach ($container->findTaggedServiceIds('logger_aware') as $id => $attributes) {
        $definition = $container->getDefinition($id);
        // Skip services that are already calling setLogger().
        if ($definition->hasMethodCall('setLogger')) {
        continue;
        }
        if (!is_subclass_of($definition->getClass(), $interface)) {
        throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
        }
        $providerTag = $definition->getTag('_provider');
        $loggerId = 'logger.channel.' . $providerTag[0]['provider'];
        if ($container->has($loggerId)) {
        $definition->addMethodCall('setLogger', [new Reference($loggerId)]);
        }
        }
        }
        }
        name: 'Logger Aware Autoconfigure Test'
        type: module
        description: 'Test logger aware auto-configuration support'
        package: Testing
        version: VERSION
        services:
        _defaults:
        autoconfigure: true
        logger_aware_test.logger_aware_stub:
        class: Drupal\logger_aware_test\LoggerAwareStub
        logger.channel.logger_aware_test:
        parent: logger.channel_base
        arguments: ['logger_aware_test']
        logger.channel.logger_stub:
        class: Drupal\logger_aware_test\LoggerStub
        logger_aware_test.logger_aware_existing:
        class: Drupal\logger_aware_test\LoggerAwareStub
        calls:
        - [setLogger, ['@logger.channel.logger_stub']]
        <?php
        declare(strict_types=1);
        namespace Drupal\logger_aware_test;
        use Psr\Log\LoggerAwareInterface;
        use Psr\Log\LoggerAwareTrait;
        use Psr\Log\LoggerInterface;
        /**
        * A test class that implements LoggerAwareInterface.
        */
        class LoggerAwareStub implements LoggerAwareInterface {
        use LoggerAwareTrait;
        /**
        * Gets the logger.
        */
        public function getLogger(): LoggerInterface {
        return $this->logger;
        }
        }
        <?php
        namespace Drupal\logger_aware_test;
        use Psr\Log\AbstractLogger;
        /**
        * A logger stub.
        */
        class LoggerStub extends AbstractLogger {
        /**
        * {@inheritdoc}
        */
        public function log($level, $message, array $context = []): void {
        // Do nothing.
        }
        }
        <?php
        declare(strict_types=1);
        namespace Drupal\Tests\system\Kernel\DependencyInjection\CompilerPass;
        use Drupal\KernelTests\KernelTestBase;
        use Drupal\logger_aware_test\LoggerAwareStub;
        use Drupal\logger_aware_test\LoggerStub;
        use Psr\Log\LoggerInterface;
        /**
        * Tests the logger aware compiler pass.
        *
        * @group system
        * @coversDefaultClass \Drupal\Core\DependencyInjection\Compiler\LoggerAwarePass
        */
        class LoggerAwarePassTest extends KernelTestBase {
        /**
        * {@inheritdoc}
        */
        protected static $modules = [
        'system',
        'logger_aware_test',
        ];
        /**
        * Tests that the logger aware compiler pass works.
        *
        * @covers ::process
        */
        public function testLoggerAwarePass(): void {
        $container = $this->container;
        $logger = $container->get('logger.channel.logger_aware_test');
        $this->assertInstanceOf(LoggerInterface::class, $logger);
        $logger_aware_stub = $container->get('logger_aware_test.logger_aware_stub');
        $this->assertInstanceOf(LoggerAwareStub::class, $logger_aware_stub);
        $this->assertSame($logger, $logger_aware_stub->getLogger());
        }
        /**
        * Tests that existing loggers are not overwritten.
        *
        * @covers ::process
        */
        public function testExistingLogger(): void {
        $container = $this->container;
        $logger_aware_stub = $container->get('logger_aware_test.logger_aware_existing');
        $logger = $logger_aware_stub->getLogger();
        $this->assertInstanceOf(LoggerStub::class, $logger);
        }
        }
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment