-
-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error for logging middleware when logger is missing
- Loading branch information
1 parent
0620e23
commit b700275
Showing
6 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
DependencyInjection/Compiler/RemoveLoggingMiddlewarePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Tests/DependencyInjection/Compiler/RemoveLoggingMiddlewarePassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters