Skip to content

Commit 2cf495c

Browse files
committed
[Doctrine] Doctrine Dbal 3.0 support
1 parent 79890b3 commit 2cf495c

File tree

7 files changed

+35
-18
lines changed

7 files changed

+35
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ return [
8989
'doctrine' => [
9090
'connection' => [
9191
'orm_default' => [
92-
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
92+
'driverClass' => 'Doctrine\DBAL\Driver\PDO\MySql\Driver,
9393
'params' => [
9494
'user' => 'mysqluser',
9595
'password' => 'mysqlpassword',

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"webmozart/assert": "^1.9.1"
5252
},
5353
"conflict": {
54+
"doctrine/dbal": "<3.0",
5455
"doctrine/doctrine-orm-module": "<4.0",
5556
"laminas/laminas-mvc": "<3.2 >=4.0",
5657
"laminas/laminas-servicemanager": "<3.6",

config/module.config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ErrorHeroModule;
44

5+
use DoctrineModule\ServiceFactory\AbstractDoctrineServiceFactory;
56
use ErrorHeroModule\Controller\ErrorPreviewController;
67
use ErrorHeroModule\Controller\ErrorPreviewConsoleController;
78
use Laminas\Log\LoggerAbstractServiceFactory;
@@ -57,6 +58,7 @@
5758
'service_manager' => [
5859
'abstract_factories' => [
5960
LoggerAbstractServiceFactory::class,
61+
AbstractDoctrineServiceFactory::class,
6062
],
6163
'factories' => [
6264
Mvc::class => MvcFactory::class,

spec/Fixture/config/autoload-with-doctrine/error-hero-module.local.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
use Doctrine\DBAL\Driver\PDOMySql\Driver;
3+
use Doctrine\DBAL\Driver\PDO\MySql\Driver;
44
use Laminas\Db\Adapter\AdapterInterface;
55

66
return [
77

88
'doctrine' => [
99
'connection' => [
1010
'orm_default' => [
11-
'driverClass' =>Driver::class,
11+
'driverClass' => Driver::class,
1212
'params' => [
1313
'user' => 'root',
1414
'password' => '',

spec/Middleware/MezzioFactorySpec.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Aura\Di\ContainerBuilder as AuraContainerBuilder;
88
use Auryn\Injector;
99
use Doctrine\DBAL\Connection;
10-
use Doctrine\DBAL\Driver\PDOMySql\Driver;
10+
use Doctrine\DBAL\Driver\PDO\MySql\Driver;
1111
use Doctrine\ORM\EntityManager;
1212
use ErrorHeroModule\Handler\Logging;
1313
use ErrorHeroModule\Middleware\Mezzio;
@@ -157,15 +157,16 @@
157157

158158
$connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']);
159159
$driver = Double::instance(['extends' => Driver::class, 'methods' => '__construct']);
160-
allow($driver)->toReceive('getName')->andReturn('pdo_mysql');
161160

162-
allow($connection)->toReceive('getParams')->andReturn([]);
163-
allow($connection)->toReceive('getUsername')->andReturn('root');
164-
allow($connection)->toReceive('getPassword')->andReturn('');
161+
allow($connection)->toReceive('getParams')->andReturn([
162+
'user' => 'mysqluser',
163+
'password' => 'mysqlpassword',
164+
'dbname' => 'mysqldbname',
165+
'host' => 'mysqlhost',
166+
'port' => '3306',
167+
'driverClass' => 'Doctrine\DBAL\Driver\PDO\MySql\Driver'
168+
]);
165169
allow($connection)->toReceive('getDriver')->andReturn($driver);
166-
allow($connection)->toReceive('getDatabase')->andReturn('mydb');
167-
allow($connection)->toReceive('getHost')->andReturn('localhost');
168-
allow($connection)->toReceive('getPort')->andReturn('3306');
169170

170171
allow($entityManager)->toReceive('getConnection')->andReturn($connection);
171172
allow($container)->toReceive('get')->with(EntityManager::class)->andReturn(

spec/ModuleSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ErrorHeroModule\Spec;
44

55
use Doctrine\DBAL\Connection;
6-
use Doctrine\DBAL\Driver\PDOMySql\Driver;
6+
use Doctrine\DBAL\Driver\PDO\MySql\Driver;
77
use Doctrine\ORM\EntityManager;
88
use ErrorHeroModule\Module;
99
use Kahlan\Plugin\Double;

src/Transformer/Doctrine.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
use Psr\Container\ContainerInterface;
1010
use Webmozart\Assert\Assert;
1111

12+
use function explode;
13+
use function implode;
14+
use function rtrim;
15+
use function strtolower;
16+
1217
class Doctrine extends TransformerAbstract implements TransformerInterface
1318
{
1419
public static function transform(ContainerInterface $container, array $configuration): ContainerInterface
@@ -21,13 +26,21 @@ public static function transform(ContainerInterface $container, array $configura
2126
$params = $doctrineDBALConnection->getParams();
2227
$driverOptions = $params['driverOptions'] ?? [];
2328

29+
$driverClass = $params['driverClass'];
30+
$driverNamespaces = explode('\\', $driverClass);
31+
$fullUnderscoredDriverName = strtolower(implode('_', $driverNamespaces));
32+
$driverName = rtrim($fullUnderscoredDriverName, '_driver');
33+
[, $driverName] = explode('driver_', $driverName);
34+
35+
Assert::startsWithLetter($driverName, 'pdo_');
36+
2437
$dbAdapterConfig = [
25-
'username' => $doctrineDBALConnection->getUsername(),
26-
'password' => $doctrineDBALConnection->getPassword(),
27-
'driver' => $doctrineDBALConnection->getDriver()->getName(),
28-
'database' => $doctrineDBALConnection->getDatabase(),
29-
'host' => $doctrineDBALConnection->getHost(),
30-
'port' => $doctrineDBALConnection->getPort(),
38+
'username' => $params['user'],
39+
'password' => $params['password'],
40+
'driver' => $driverName,
41+
'database' => $params['dbname'],
42+
'host' => $params['host'],
43+
'port' => $params['port'],
3144
'driver_options' => $driverOptions,
3245
];
3346

0 commit comments

Comments
 (0)