Skip to content

Commit

Permalink
Merge pull request #1263 from greg0ire/dbal-3-compat
Browse files Browse the repository at this point in the history
Fixes for compatibility with DBAL 3
  • Loading branch information
greg0ire authored Nov 19, 2020
2 parents 264434b + b79661a commit 74c3bab
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 38 deletions.
29 changes: 27 additions & 2 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection;

use Doctrine\Bundle\DoctrineBundle\Command\Proxy\ImportDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Dbal\ManagerRegistryAwareConnectionProvider;
use Doctrine\Bundle\DoctrineBundle\Dbal\RegexSchemaAssetFilter;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface;
use Doctrine\DBAL\Connections\MasterSlaveConnection;
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Logging\LoggerChain;
use Doctrine\DBAL\SQLParserUtils;
use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
use Doctrine\DBAL\Tools\Console\ConnectionProvider;
use Doctrine\ORM\Proxy\Autoloader;
use Doctrine\ORM\UnitOfWork;
Expand Down Expand Up @@ -85,6 +89,19 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('dbal.xml');
$chainLogger = $container->getDefinition('doctrine.dbal.logger.chain');
$logger = new Reference('doctrine.dbal.logger');
if (! method_exists(SQLParserUtils::class, 'getPositionalPlaceholderPositions') && method_exists(LoggerChain::class, 'addLogger')) {
// doctrine/dbal < 2.10.0
$chainLogger->addMethodCall('addLogger', [$logger]);
} else {
$chainLogger->addArgument([$logger]);
}

if (class_exists(ImportCommand::class)) {
$container->register('doctrine.database_import_command', ImportDoctrineCommand::class)
->addTag('console.command', ['command' => 'doctrine:database:import']);
}

if (empty($config['default_connection'])) {
$keys = array_keys($config['connections']);
Expand Down Expand Up @@ -143,8 +160,16 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
->replaceArgument(1, $connection['profiling_collect_schema_errors']);

if ($logger !== null) {
$chainLogger = new ChildDefinition('doctrine.dbal.logger.chain');
$chainLogger->addMethodCall('addLogger', [$profilingLogger]);
$chainLogger = $container->register(
'doctrine.dbal.logger.chain',
LoggerChain::class
);
if (! method_exists(SQLParserUtils::class, 'getPositionalPlaceholderPositions') && method_exists(LoggerChain::class, 'addLogger')) {
// doctrine/dbal < 2.10.0
$chainLogger->addMethodCall('addLogger', [$profilingLogger]);
} else {
$chainLogger->addArgument([$logger, $profilingLogger]);
}

$loggerId = 'doctrine.dbal.logger.chain.' . $name;
$container->setDefinition($loggerId, $chainLogger);
Expand Down
10 changes: 2 additions & 8 deletions Resources/config/dbal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
<service id="Doctrine\DBAL\Driver\Connection" alias="database_connection" public="false" />
<service id="Doctrine\DBAL\Connection" alias="database_connection" public="false" />

<service id="doctrine.dbal.logger.chain" class="%doctrine.dbal.logger.chain.class%" public="false" abstract="true">
<call method="addLogger">
<argument type="service" id="doctrine.dbal.logger" />
</call>
<service id="doctrine.dbal.logger.chain" class="%doctrine.dbal.logger.chain.class%" public="false" abstract="true" >
<!-- TODO: deprecate this service in 2.3.0 -->
</service>

<service id="doctrine.dbal.logger.profiling" class="%doctrine.dbal.logger.profiling.class%" public="false" abstract="true" />
Expand Down Expand Up @@ -94,10 +92,6 @@
<tag name="console.command" command="doctrine:database:drop" />
</service>

<service id="doctrine.database_import_command" class="Doctrine\Bundle\DoctrineBundle\Command\Proxy\ImportDoctrineCommand">
<tag name="console.command" command="doctrine:database:import" />
</service>

<service id="doctrine.query_sql_command" class="Doctrine\Bundle\DoctrineBundle\Command\Proxy\RunSqlDoctrineCommand">
<tag name="console.command" command="doctrine:query:sql" />
</service>
Expand Down
25 changes: 20 additions & 5 deletions Tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\DbalTestKernel;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration as DBALConfiguration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Logging\LoggerChain;
use Doctrine\DBAL\Tools\Console\Command\ImportCommand;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
Expand All @@ -22,13 +25,21 @@

class ContainerTest extends TestCase
{
public static function setUpBeforeClass(): void
public function testContainerWithDbalOnly(): void
{
if (interface_exists(EntityManagerInterface::class)) {
return;
}
$kernel = new DbalTestKernel();
$kernel->boot();

self::markTestSkipped('This test requires ORM');
$container = $kernel->getContainer();
$this->assertInstanceOf(
LoggerChain::class,
$container->get('doctrine.dbal.logger.chain.default')
);
if (class_exists(ImportCommand::class)) {
self::assertTrue($container->has('doctrine.database_import_command'));
} else {
self::assertFalse($container->has('doctrine.database_import_command'));
}
}

/**
Expand All @@ -38,6 +49,10 @@ public static function setUpBeforeClass(): void
*/
public function testContainer(): void
{
if (! interface_exists(EntityManagerInterface::class)) {
self::markTestSkipped('This test requires ORM');
}

$container = $this->createXmlBundleTestContainer();

$this->assertInstanceOf(DbalLogger::class, $container->get('doctrine.dbal.logger'));
Expand Down
63 changes: 63 additions & 0 deletions Tests/DependencyInjection/Fixtures/DbalTestKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

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

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\DoctrineBundle\Tests\TestCaseAllPublicCompilerPass;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;

class DbalTestKernel extends Kernel
{
/** @var string|null */
private $projectDir;

public function __construct()
{
parent::__construct('test', true);
}

public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
new DoctrineBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(static function (ContainerBuilder $container): void {
// @todo Setting the kernel.name parameter can be removed once the dependency on DoctrineCacheBundle has been dropped
$container->setParameter('kernel.name', 'foo');
$container->loadFromExtension('framework', ['secret' => 'F00']);

$container->loadFromExtension('doctrine', [
'dbal' => ['driver' => 'pdo_sqlite'],
]);

// Register a NullLogger to avoid getting the stderr default logger of FrameworkBundle
$container->register('logger', NullLogger::class);

// make all Doctrine services public, so we can fetch them in the test
$container->getCompilerPassConfig()->addPass(new TestCaseAllPublicCompilerPass());
});
}

public function getProjectDir(): string
{
if ($this->projectDir === null) {
$this->projectDir = sys_get_temp_dir() . '/sf_kernel_' . md5(mt_rand());
}

return $this->projectDir;
}

public function getRootDir(): string
{
return $this->getProjectDir();
}
}
23 changes: 0 additions & 23 deletions Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase as BaseTestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -82,25 +81,3 @@ public function createXmlBundleTestContainer(): ContainerBuilder
return $container;
}
}

class TestCaseAllPublicCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
foreach ($container->getDefinitions() as $id => $definition) {
if (strpos($id, 'doctrine') === false) {
continue;
}

$definition->setPublic(true);
}

foreach ($container->getAliases() as $id => $alias) {
if (strpos($id, 'doctrine') === false) {
continue;
}

$alias->setPublic(true);
}
}
}
28 changes: 28 additions & 0 deletions Tests/TestCaseAllPublicCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Doctrine\Bundle\DoctrineBundle\Tests;

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

class TestCaseAllPublicCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
foreach ($container->getDefinitions() as $id => $definition) {
if (strpos($id, 'doctrine') === false) {
continue;
}

$definition->setPublic(true);
}

foreach ($container->getAliases() as $id => $alias) {
if (strpos($id, 'doctrine') === false) {
continue;
}

$alias->setPublic(true);
}
}
}

0 comments on commit 74c3bab

Please sign in to comment.