Skip to content

Commit

Permalink
Fix error for logging middleware when logger is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
l-vo authored and ostrolucky committed Apr 2, 2022
1 parent 0620e23 commit b700275
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 1 deletion.
19 changes: 19 additions & 0 deletions DependencyInjection/Compiler/RemoveLoggingMiddlewarePass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/** @internal */
final class RemoveLoggingMiddlewarePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->has('logger')) {
return;
}

$container->removeDefinition('doctrine.dbal.logging_middleware');
}
}
2 changes: 2 additions & 0 deletions DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\IdGeneratorPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\MiddlewaresPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveLoggingMiddlewarePass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveProfilerControllerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\WellKnownSchemaFilterPass;
Expand Down Expand Up @@ -65,6 +66,7 @@ public function build(ContainerBuilder $container)

/** @psalm-suppress UndefinedClass */
if (interface_exists(Middleware::class)) {
$container->addCompilerPass(new RemoveLoggingMiddlewarePass());
$container->addCompilerPass(new MiddlewaresPass());
}

Expand Down
2 changes: 1 addition & 1 deletion Resources/config/middlewares.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<services>
<service id="doctrine.dbal.logging_middleware" class="Doctrine\DBAL\Logging\Middleware" abstract="true">
<argument type="service" id="logger" on-invalid="null" />
<argument type="service" id="logger" />
<tag name="monolog.logger" channel="doctrine" />
<tag name="doctrine.middleware" />
</service>
Expand Down
5 changes: 5 additions & 0 deletions Tests/DependencyInjection/Compiler/MiddlewarePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -191,6 +192,10 @@ private function createContainer(callable $func, bool $addConnections = true): C
{
$container = new ContainerBuilder(new ParameterBag(['kernel.debug' => false]));

$loggerDef = new Definition();
$loggerDef->setClass(NullLogger::class);
$container->setDefinition('logger', $loggerDef);

$container->registerExtension(new DoctrineExtension());

if ($addConnections) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Compiler;

use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RemoveLoggingMiddlewarePass;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

final class RemoveLoggingMiddlewarePassTest extends TestCase
{
public function testLoggingMiddlewareRemovedWhenLoggerMissing(): void
{
$container = $this->createContainer();
$container->compile();

$this->assertFalse($container->hasDefinition('logging_middleware_child'));
}

public function testLoggingMiddlewareNotRemovedWhenLoggerPresent(): void
{
$container = $this->createContainer();

$logger = (new Definition())
->setClass(NullLogger::class);
$container->setDefinition('logger', $logger);

$container->compile();

$this->assertTrue($container->hasDefinition('logging_middleware_child'));
}

private function createContainer(): ContainerBuilder
{
$container = new ContainerBuilder();

$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../../Resources/config'));
$loader->load('middlewares.xml');

$container->addCompilerPass(new RemoveLoggingMiddlewarePass());
$container->addCompilerPass(new class implements CompilerPassInterface {
public function process(ContainerBuilder $container)
{
if (! $container->hasDefinition('doctrine.dbal.logging_middleware')) {
return;
}

$loggingMiddlewareChild = (new ChildDefinition('doctrine.dbal.logging_middleware'))
->setPublic(true);
$container->setDefinition('logging_middleware_child', $loggingMiddlewareChild);
}
});

return $container;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"phpunit/phpunit": "^7.5 || ^8.0 || ^9.3 || ^10.0",
"psalm/plugin-phpunit": "^0.16.1",
"psalm/plugin-symfony": "^3",
"psr/log": "^1.1.4|^2.0|^3.0",
"symfony/phpunit-bridge": "^5.2|^6.0",
"symfony/property-info": "^4.3.3|^5.0|^6.0",
"symfony/proxy-manager-bridge": "^3.4|^4.3.3|^5.0|^6.0",
Expand Down

0 comments on commit b700275

Please sign in to comment.