Skip to content

Commit 8534496

Browse files
authored
Merge pull request #84 from samsonasik/doctrine-dbal-3
[Doctrine] Doctrine Dbal 3.0 support
2 parents 79890b3 + 43b8b5c commit 8534496

File tree

10 files changed

+116
-27
lines changed

10 files changed

+116
-27
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
/kahlan-config.php export-ignore
1010
/phpcs.xml export-ignore
1111
/rector.php export-ignore
12+
/stubs export-ignore

.github/workflows/ci_build.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ jobs:
3030
uses: actions/checkout@v2
3131
- name: Import DB
3232
run: "mysql -u root -h 127.0.0.1 -e 'create database errorheromodule' && mysql -u root -h 127.0.0.1 errorheromodule < spec/Fixture/data/sql.sql"
33-
- name: "Validate composer.json and composer.lock"
33+
- name: "Validate composer.json"
3434
run: "composer validate"
3535
- name: "Install dependencies"
36-
run: "composer install --prefer-dist --ignore-platform-reqs"
36+
run: "composer install --ignore-platform-reqs"
3737
- name: "CS Check"
3838
run: "composer cs-check"
3939
- name: "Code analyze"
4040
run: |
4141
bin/phpstan analyse src/ --level=8 -c phpstan.neon
4242
bin/rector process --dry-run
4343
- name: "Run test suite"
44-
run: "mkdir -p build/logs && bin/kahlan --coverage=4 --reporter=verbose --clover=build/logs/clover.xml"
44+
run: |
45+
composer dump-autoload -o
46+
mkdir -p build/logs && bin/kahlan --coverage=4 --reporter=verbose --clover=build/logs/clover.xml
4547
- name: Upload coverage to Codecov
4648
uses: codecov/codecov-action@v1
4749
with:

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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,22 @@
5151
"webmozart/assert": "^1.9.1"
5252
},
5353
"conflict": {
54-
"doctrine/doctrine-orm-module": "<4.0",
54+
"doctrine/dbal": "<3.0",
55+
"doctrine/doctrine-orm-module": "<4.1",
5556
"laminas/laminas-mvc": "<3.2 >=4.0",
56-
"laminas/laminas-servicemanager": "<3.6",
57+
"laminas/laminas-servicemanager": "<3.10",
5758
"mezzio/mezzio": "<3.3 >=4.0"
5859
},
5960
"require-dev": {
6061
"aura/di": "^3.4 || ^4.0",
61-
"doctrine/doctrine-orm-module": "^4.0",
62+
"doctrine/dbal": "^3.0",
63+
"doctrine/doctrine-orm-module": "^4.1",
6264
"kahlan/kahlan": "^5.1.3",
6365
"laminas/laminas-coding-standard": "^2.1.1",
6466
"laminas/laminas-form": "^2.16 || ^3.0",
6567
"laminas/laminas-mvc": "^3.2",
6668
"laminas/laminas-mvc-console": "^1.1",
67-
"laminas/laminas-servicemanager": "^3.6",
69+
"laminas/laminas-servicemanager": "^3.10",
6870
"mezzio/mezzio": "^3.3",
6971
"mezzio/mezzio-laminasviewrenderer": "^2.3",
7072
"northwoods/container": "^3.0",
@@ -101,7 +103,10 @@
101103
"autoload-dev": {
102104
"psr-4": {
103105
"ErrorHeroModule\\Spec\\": "spec/"
104-
}
106+
},
107+
"classmap": [
108+
"stubs"
109+
]
105110
},
106111
"minimum-stability": "dev",
107112
"prefer-stable": true,

config/module.config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use ErrorHeroModule\Listener\MvcFactory;
1010
use ErrorHeroModule\Handler\Logging;
1111
use ErrorHeroModule\Handler\LoggingFactory;
12-
use Laminas\Log;
1312
use Laminas\ServiceManager\Factory\InvokableFactory;
1413

1514
return [

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' => Driver::class,
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: 17 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,19 @@ 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+
2435
$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(),
36+
'username' => $params['user'],
37+
'password' => $params['password'],
38+
'driver' => $driverName,
39+
'database' => $params['dbname'],
40+
'host' => $params['host'],
41+
'port' => $params['port'],
3142
'driver_options' => $driverOptions,
3243
];
3344

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Driver\PDO\MySql;
6+
7+
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
8+
use Doctrine\DBAL\Driver\PDO\Connection;
9+
use PDO;
10+
11+
if (class_exists(Driver::class)) {
12+
return;
13+
}
14+
15+
final class Driver extends AbstractMySQLDriver
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*
20+
* @return Connection
21+
*/
22+
public function connect(array $params)
23+
{
24+
$driverOptions = $params['driverOptions'] ?? [];
25+
26+
if (! empty($params['persistent'])) {
27+
$driverOptions[PDO::ATTR_PERSISTENT] = true;
28+
}
29+
30+
return new Connection(
31+
$this->constructPdoDsn($params),
32+
$params['user'] ?? '',
33+
$params['password'] ?? '',
34+
$driverOptions
35+
);
36+
}
37+
38+
/**
39+
* Constructs the MySQL PDO DSN.
40+
*
41+
* @param mixed[] $params
42+
*
43+
* @return string The DSN.
44+
*/
45+
protected function constructPdoDsn(array $params)
46+
{
47+
$dsn = 'mysql:';
48+
if (isset($params['host']) && $params['host'] !== '') {
49+
$dsn .= 'host=' . $params['host'] . ';';
50+
}
51+
52+
if (isset($params['port'])) {
53+
$dsn .= 'port=' . $params['port'] . ';';
54+
}
55+
56+
if (isset($params['dbname'])) {
57+
$dsn .= 'dbname=' . $params['dbname'] . ';';
58+
}
59+
60+
if (isset($params['unix_socket'])) {
61+
$dsn .= 'unix_socket=' . $params['unix_socket'] . ';';
62+
}
63+
64+
if (isset($params['charset'])) {
65+
$dsn .= 'charset=' . $params['charset'] . ';';
66+
}
67+
68+
return $dsn;
69+
}
70+
}

0 commit comments

Comments
 (0)