Skip to content

Commit ae0f940

Browse files
authored
Merge pull request #16 from axleus/adapter-migration-alternate-driver-creation
Refactor to simplify factoring all services
2 parents 6de09ff + 4cc048c commit ae0f940

15 files changed

+183
-124
lines changed

src/Container/AdapterFactory.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Laminas\Db\Adapter\AdapterInterface;
88
use Laminas\Db\Adapter\Driver\DriverInterface;
9+
use Laminas\Db\Adapter\Exception\RuntimeException;
910
use Laminas\Db\Adapter\Mysql\Adapter;
1011
use Laminas\Db\Adapter\Platform\PlatformInterface;
1112
use Laminas\Db\Adapter\Profiler\ProfilerInterface;
@@ -19,8 +20,19 @@ public function __invoke(ContainerInterface $container): AdapterInterface
1920
{
2021
/** @var AdapterManager $adapterManager */
2122
$adapterManager = $container->get(AdapterManager::class);
23+
/** @var array $config */
24+
$config = $container->get('config');
25+
if (! isset($config['db']['driver'])) {
26+
throw new RuntimeException('Database driver configuration is missing.');
27+
}
28+
if (! $adapterManager->has($config['db']['driver'])) {
29+
throw new RuntimeException(sprintf(
30+
'Database driver "%s" is not registered in the adapter manager.',
31+
$config['db']['driver']
32+
));
33+
}
2234
return new Adapter(
23-
$adapterManager->get(DriverInterface::class),
35+
$adapterManager->get($config['db']['driver']),
2436
$adapterManager->get(PlatformInterface::class),
2537
$adapterManager->get(ResultSetInterface::class),
2638
$adapterManager->get(ProfilerInterface::class)

src/Container/AdapterManagerDelegator.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
namespace Laminas\Db\Adapter\Mysql\Container;
66

77
use Laminas\Db\Adapter\AdapterInterface;
8-
use Laminas\Db\Adapter\Driver\ConnectionInterface;
98
use Laminas\Db\Adapter\Driver\DriverInterface;
10-
use Laminas\Db\Adapter\Driver\ResultInterface;
9+
use Laminas\Db\Adapter\Driver\PdoDriverInterface;
10+
use Laminas\Db\Adapter\Driver\Pdo\Result;
11+
use Laminas\Db\Adapter\Driver\Pdo\Statement as PdoStatement;
12+
use Laminas\Db\Adapter\Mysql\Driver;
1113
use Laminas\Db\Adapter\Platform\PlatformInterface;
1214
use Laminas\Db\Adapter\Profiler;
1315
use Laminas\Db\Container\AdapterManager;
@@ -27,17 +29,32 @@ public function __invoke(
2729
$adapterManager = $callback();
2830
$adapterManager->configure([
2931
'aliases' => [
32+
'MySqli' => Driver\Mysqli\Mysqli::class,
33+
'MySQLi' => Driver\Mysqli\Mysqli::class,
34+
'mysqli' => Driver\Mysqli\Mysqli::class,
35+
'Pdo_Mysql' => Driver\Pdo\Pdo::class,
36+
'pdo_mysql' => Driver\Pdo\Pdo::class,
37+
'pdomysql' => Driver\Pdo\Pdo::class,
38+
'pdodriver' => Driver\Pdo\Pdo::class,
39+
'pdo' => Driver\Pdo\Pdo::class,
40+
DriverInterface::class => Driver\Mysqli\Mysqli::class,
41+
PdoDriverInterface::class => Driver\Pdo\Pdo::class,
3042
Profiler\ProfilerInterface::class => Profiler\Profiler::class,
3143
ResultSet\ResultSetInterface::class => ResultSet\ResultSet::class,
3244
],
3345
'factories' => [
34-
AdapterInterface::class => AdapterFactory::class,
35-
ConnectionInterface::class => ConnectionInterfaceFactory::class,
36-
DriverInterface::class => DriverInterfaceFactory::class,
37-
PlatformInterface::class => PlatformInterfaceFactory::class,
38-
Profiler\Profiler::class => InvokableFactory::class,
39-
ResultInterface::class => ResultInterfaceFactory::class,
40-
ResultSet\ResultSet::class => InvokableFactory::class,
46+
AdapterInterface::class => AdapterFactory::class,
47+
Driver\Mysqli\Mysqli::class => MysqliDriverFactory::class,
48+
Driver\Mysqli\Connection::class => MysqliConnectionFactory::class,
49+
Driver\Mysqli\Result::class => MysqliResultFactory::class,
50+
Driver\Mysqli\Statement::class => MysqliStatementFactory::class,
51+
Driver\Pdo\Pdo::class => PdoDriverFactory::class,
52+
Driver\Pdo\Connection::class => PdoConnectionFactory::class,
53+
Result::class => PdoResultFactory::class,
54+
PdoStatement::class => PdoStatementFactory::class,
55+
PlatformInterface::class => PlatformInterfaceFactory::class,
56+
Profiler\Profiler::class => InvokableFactory::class,
57+
ResultSet\ResultSet::class => InvokableFactory::class,
4158
],
4259
]);
4360

src/Container/ConnectionInterfaceFactory.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Container/InterfaceFactoryTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ private function getDriver(AdapterManager $adapterManager): DriverInterface
3636
$adapterManager->get(ConnectionInterface::class)
3737
);
3838
}
39-
return new Driver\Mysqli\Mysqli(
40-
$adapterManager->get(ConnectionInterface::class)
41-
);
39+
// return new Driver\Mysqli\Mysqli(
40+
// $adapterManager->get(ConnectionInterface::class)
41+
// );
4242
}
4343

4444
private function getResult(AdapterManager $adapterManager): ResultInterface
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Driver\ConnectionInterface;
8+
use Laminas\Db\Adapter\Mysql\Driver\Mysqli\Connection;
9+
use Psr\Container\ContainerInterface;
10+
11+
final class MysqliConnectionFactory
12+
{
13+
public function __invoke(ContainerInterface $container): ConnectionInterface
14+
{
15+
$dbConfig = $container->get('config')['db']['connection'] ?? [];
16+
17+
return new Connection($dbConfig);
18+
}
19+
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@
55
namespace Laminas\Db\Adapter\Mysql\Container;
66

77
use Laminas\Db\Adapter\Driver\DriverInterface;
8+
use Laminas\Db\Adapter\Mysql\Driver\Mysqli;
89
use Laminas\Db\Container\AdapterManager;
910
use Psr\Container\ContainerInterface;
1011

11-
final class DriverInterfaceFactory
12+
final class MysqliDriverFactory
1213
{
13-
use InterfaceFactoryTrait;
14-
1514
public function __invoke(ContainerInterface $container): DriverInterface
1615
{
1716
/** @var AdapterManager $adapterManager */
1817
$adapterManager = $container->get(AdapterManager::class);
19-
return $this->getDriver($adapterManager);
18+
$options = $container->get('config')['db']['options'] ?? [];
19+
return new Mysqli\Mysqli(
20+
$adapterManager->get(Mysqli\Connection::class),
21+
$adapterManager->get(Mysqli\Statement::class),
22+
$adapterManager->get(Mysqli\Result::class),
23+
$options
24+
);
2025
}
2126
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Driver\ResultInterface;
8+
use Laminas\Db\Adapter\Mysql\Driver\Mysqli\Result;
9+
use Psr\Container\ContainerInterface;
10+
11+
final class MysqliResultFactory
12+
{
13+
public function __invoke(ContainerInterface $container): ResultInterface&Result
14+
{
15+
return new Result();
16+
}
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Driver\StatementInterface;
8+
use Laminas\Db\Adapter\Mysql\Driver\Mysqli\Statement;
9+
use Psr\Container\ContainerInterface;
10+
11+
final class MysqliStatementFactory
12+
{
13+
public function __invoke(ContainerInterface $container): StatementInterface
14+
{
15+
$bufferResults = $container->get('config')['db']['options']['buffer_results'] ?? false;
16+
return new Statement($bufferResults);
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
use Laminas\Db\Adapter\Driver\ConnectionInterface;
8+
use Laminas\Db\Adapter\Mysql\Driver\Pdo\Connection;
9+
use Psr\Container\ContainerInterface;
10+
11+
final class PdoConnectionFactory
12+
{
13+
public function __invoke(ContainerInterface $container): ConnectionInterface {
14+
$dbConfig = $container->get('config')['db']['connection'] ?? [];
15+
return new Connection($dbConfig);
16+
}
17+
}

src/Container/PdoDriverFactory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laminas\Db\Adapter\Mysql\Container;
6+
7+
final class PdoDriverFactory
8+
{
9+
10+
}

0 commit comments

Comments
 (0)