Skip to content

Commit 3d9a72a

Browse files
committed
Merge branch '0.2.x' into pr/tyrsson/33
2 parents 3eb8279 + 34c01bb commit 3d9a72a

20 files changed

+234
-34
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333
},
3434
"require": {
35-
"php": "~8.2.0 || ~8.3.0 || ~8.4.0",
35+
"php": "~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0",
3636
"php-db/phpdb": "^0.1.0"
3737
},
3838
"require-dev": {

src/ConfigProvider.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
use PhpDb\Adapter\Mysql\Metadata\Source\MysqlMetadata;
1515
use PhpDb\Adapter\Platform\PlatformInterface;
1616
use PhpDb\Adapter\Profiler;
17+
use PhpDb\Container\AdapterAbstractServiceFactory;
1718
use PhpDb\Container\AdapterManager;
19+
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface;
20+
use PhpDb\Container\DriverInterfaceFactoryFactoryInterface;
1821
use PhpDb\Container\MetadataFactory;
22+
use PhpDb\Container\PlatformInterfaceFactoryFactoryInterface;
1923
use PhpDb\Metadata\MetadataInterface;
2024
use PhpDb\ResultSet;
2125

@@ -32,13 +36,16 @@ public function __invoke(): array
3236
public function getDependencies(): array
3337
{
3438
return [
35-
'aliases' => [
39+
'abstract_factories' => [
40+
AdapterAbstractServiceFactory::class,
41+
],
42+
'aliases' => [
3643
MetadataInterface::class => MysqlMetadata::class,
3744
],
38-
'factories' => [
45+
'factories' => [
3946
MysqlMetadata::class => MetadataFactory::class,
4047
],
41-
'delegators' => [
48+
'delegators' => [
4249
AdapterManager::class => [
4350
Container\AdapterManagerDelegator::class,
4451
],
@@ -49,24 +56,27 @@ public function getDependencies(): array
4956
public function getAdapterManagerConfig(): array
5057
{
5158
return [
52-
'aliases' => [
53-
'MySqli' => Driver\Mysqli\Mysqli::class,
54-
'MySQLi' => Driver\Mysqli\Mysqli::class,
55-
'Mysqli' => Driver\Mysqli\Mysqli::class,
56-
'mysqli' => Driver\Mysqli\Mysqli::class,
57-
'PDO_MySQL' => Driver\Pdo\Pdo::class,
58-
'Pdo_MySQL' => Driver\Pdo\Pdo::class,
59-
'Pdo_Mysql' => Driver\Pdo\Pdo::class,
60-
'pdo_mysql' => Driver\Pdo\Pdo::class,
61-
'pdomysql' => Driver\Pdo\Pdo::class,
62-
'pdodriver' => Driver\Pdo\Pdo::class,
63-
'pdo' => Driver\Pdo\Pdo::class,
64-
DriverInterface::class => Driver\Mysqli\Mysqli::class,
65-
PdoDriverInterface::class => Driver\Pdo\Pdo::class,
66-
Profiler\ProfilerInterface::class => Profiler\Profiler::class,
67-
ResultSet\ResultSetInterface::class => ResultSet\ResultSet::class,
59+
'aliases' => [
60+
'MySqli' => Driver\Mysqli\Mysqli::class,
61+
'MySQLi' => Driver\Mysqli\Mysqli::class,
62+
'Mysqli' => Driver\Mysqli\Mysqli::class,
63+
'mysqli' => Driver\Mysqli\Mysqli::class,
64+
'PDO_MySQL' => Driver\Pdo\Pdo::class,
65+
'Pdo_MySQL' => Driver\Pdo\Pdo::class,
66+
'Pdo_Mysql' => Driver\Pdo\Pdo::class,
67+
'pdo_mysql' => Driver\Pdo\Pdo::class,
68+
'pdomysql' => Driver\Pdo\Pdo::class,
69+
'pdodriver' => Driver\Pdo\Pdo::class,
70+
'pdo' => Driver\Pdo\Pdo::class,
71+
DriverInterface::class => Driver\Mysqli\Mysqli::class,
72+
PdoDriverInterface::class => Driver\Pdo\Pdo::class,
73+
Profiler\ProfilerInterface::class => Profiler\Profiler::class,
74+
ResultSet\ResultSetInterface::class => ResultSet\ResultSet::class,
75+
ConnectionInterfaceFactoryFactoryInterface::class => Container\ConnectionInterfaceFactoryFactory::class,
76+
DriverInterfaceFactoryFactoryInterface::class => Container\DriverInterfaceFactoryFactory::class,
77+
PlatformInterfaceFactoryFactoryInterface::class => Container\PlatformInterfaceFactoryFactory::class,
6878
],
69-
'factories' => [
79+
'factories' => [
7080
AdapterInterface::class => Container\AdapterFactory::class,
7181
Driver\Mysqli\Mysqli::class => Container\MysqliDriverFactory::class,
7282
Driver\Mysqli\Connection::class => Container\MysqliConnectionFactory::class,
@@ -80,6 +90,14 @@ public function getAdapterManagerConfig(): array
8090
Profiler\Profiler::class => InvokableFactory::class,
8191
ResultSet\ResultSet::class => InvokableFactory::class,
8292
],
93+
'invokables' => [
94+
Container\ConnectionInterfaceFactoryFactory::class
95+
=> Container\ConnectionInterfaceFactoryFactory::class,
96+
Container\DriverInterfaceFactoryFactory::class
97+
=> Container\DriverInterfaceFactoryFactory::class,
98+
Container\PlatformInterfaceFactoryFactory::class
99+
=> Container\PlatformInterfaceFactoryFactory::class,
100+
],
83101
];
84102
}
85103
}

src/Container/AdapterManagerDelegator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace PhpDb\Adapter\Mysql\Container;
66

77
use Laminas\ServiceManager\Factory\DelegatorFactoryInterface;
8-
use PhpDB\Adapter\Mysql\ConfigProvider;
8+
use PhpDb\Adapter\Mysql\ConfigProvider;
99
use PhpDb\Container\AdapterManager;
1010
use Psr\Container\ContainerInterface;
1111

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Adapter\Mysql\Container;
6+
7+
use PhpDb\Adapter\Mysql\Container\MysqliConnectionFactory;
8+
use PhpDb\Adapter\Mysql\Container\PdoConnectionFactory;
9+
use PhpDb\Adapter\Mysql\Driver\Mysqli\Mysqli;
10+
use PhpDb\Adapter\Mysql\Driver\Pdo\Pdo;
11+
use PhpDb\Container\AdapterManager;
12+
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
13+
use Psr\Container\ContainerInterface;
14+
use RuntimeException;
15+
16+
use function array_key_exists;
17+
use function sprintf;
18+
19+
final class ConnectionInterfaceFactoryFactory implements FactoryFactoryInterface
20+
{
21+
public function __invoke(
22+
?ContainerInterface $container = null,
23+
?string $requestedName = null
24+
): callable {
25+
$adapterConfig = $container->get('config')['db']['adapters'] ?? [];
26+
if (! isset($adapterConfig[$requestedName]['driver'])) {
27+
throw new RuntimeException(sprintf(
28+
'Named adapter "%s" is not configured with a driver',
29+
$requestedName
30+
));
31+
}
32+
$adapterServices = $container->get('config')[AdapterManager::class];
33+
$configuredDriver = $adapterConfig[$requestedName]['driver'];
34+
if (array_key_exists($configuredDriver, $adapterServices['aliases'])) {
35+
$aliasTo = $adapterServices['aliases'][$configuredDriver];
36+
} else {
37+
$aliasTo = $configuredDriver;
38+
}
39+
return match ($aliasTo) {
40+
Mysqli::class => new MysqliConnectionFactory(),
41+
Pdo::class => new PdoConnectionFactory(),
42+
default => throw new RuntimeException(sprintf(
43+
'No connection factory found for driver "%s"',
44+
$configuredDriver
45+
)),
46+
};
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Adapter\Mysql\Container;
6+
7+
use PhpDb\Container\AdapterManager;
8+
use PhpDb\Container\DriverInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
9+
use Psr\Container\ContainerInterface;
10+
use RuntimeException;
11+
12+
use function sprintf;
13+
14+
final class DriverInterfaceFactoryFactory implements FactoryFactoryInterface
15+
{
16+
public function __invoke(
17+
?ContainerInterface $container = null,
18+
?string $requestedName = null
19+
): callable {
20+
$adapterConfig = $container->get('config')['db']['adapters'] ?? [];
21+
if (! isset($adapterConfig[$requestedName]['driver'])) {
22+
throw new RuntimeException(sprintf(
23+
'Named adapter "%s" is not configured with a driver',
24+
$requestedName
25+
));
26+
}
27+
$adapterServices = $container->get('config')[AdapterManager::class];
28+
29+
$configuredDriver = $adapterConfig[$requestedName]['driver'];
30+
$aliasTo ??= $adapterServices['aliases'][$configuredDriver] ?? $configuredDriver;
31+
$driverFactory = $adapterServices['factories'][$aliasTo];
32+
return new $driverFactory();
33+
}
34+
}

src/Container/MysqliConnectionFactory.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44

55
namespace PhpDb\Adapter\Mysql\Container;
66

7+
use Laminas\ServiceManager\Factory\FactoryInterface;
78
use PhpDb\Adapter\Driver\ConnectionInterface;
89
use PhpDb\Adapter\Mysql\Driver\Mysqli\Connection;
910
use Psr\Container\ContainerInterface;
1011

11-
final class MysqliConnectionFactory
12+
final class MysqliConnectionFactory implements FactoryInterface
1213
{
13-
public function __invoke(ContainerInterface $container): ConnectionInterface&Connection
14-
{
14+
public function __invoke(
15+
ContainerInterface $container,
16+
string $requestedName,
17+
?array $options = null
18+
): ConnectionInterface&Connection {
1519
/** @var array $config */
1620
$config = $container->get('config');
1721

@@ -23,4 +27,12 @@ public function __invoke(ContainerInterface $container): ConnectionInterface&Con
2327

2428
return new Connection($connectionConfig);
2529
}
30+
31+
public static function createFromConfig(
32+
ContainerInterface $container,
33+
string $requestedName
34+
): ConnectionInterface&Connection {
35+
$adapterConfig = $container->get('config')['db']['adapters'][$requestedName] ?? [];
36+
return new Connection($adapterConfig['connection'] ?? []);
37+
}
2638
}

src/Container/MysqliDriverFactory.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,28 @@ public function __invoke(ContainerInterface $container): Driver\DriverInterface&
4141
$options
4242
);
4343
}
44+
45+
public static function createFromConfig(
46+
ContainerInterface $container,
47+
string $requestedName,
48+
): Driver\DriverInterface&Mysqli\Mysqli {
49+
/** @var AdapterManager $adapterManager */
50+
$adapterManager = $container->get(AdapterManager::class);
51+
$connectionFactory = (
52+
$adapterManager->get(ConnectionInterfaceFactoryFactory::class)
53+
)($container, $requestedName);
54+
/** @var array $config */
55+
$config = $container->get('config');
56+
/** @var array $dbConfig */
57+
$dbConfig = $config['db'] ?? [];
58+
/** @var array $adapterConfig */
59+
$adapterConfig = $dbConfig['adapters'][$requestedName] ?? [];
60+
61+
return new Mysqli\Mysqli(
62+
$connectionFactory::createFromConfig($container, $requestedName),
63+
$adapterManager->get(Mysqli\Statement::class),
64+
$adapterManager->get(Mysqli\Result::class),
65+
$adapterConfig['options'] ?? []
66+
);
67+
}
4468
}

src/Container/PdoConnectionFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ public function __invoke(ContainerInterface $container): ConnectionInterface&Con
2323

2424
return new Connection($connectionConfig);
2525
}
26+
27+
public static function createFromConfig(
28+
ContainerInterface $container,
29+
string $requestedName
30+
): ConnectionInterface&Connection {
31+
$adapterConfig = $container->get('config')['db']['adapters'][$requestedName] ?? [];
32+
return new Connection($adapterConfig['connection'] ?? []);
33+
}
2634
}

src/Container/PdoDriverFactory.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,39 @@ public function __invoke(ContainerInterface $container): PdoDriverInterface&PdoD
3737
$resultInstance
3838
);
3939
}
40+
41+
public static function createFromConfig(
42+
ContainerInterface $container,
43+
string $requestedName,
44+
): PdoDriverInterface&PdoDriver {
45+
/** @var AdapterManager $adapterManager */
46+
$adapterManager = $container->get(AdapterManager::class);
47+
$connectionFactory = (
48+
$adapterManager->get(ConnectionInterfaceFactoryFactory::class)
49+
)($container, $requestedName);
50+
/** @var array $config */
51+
$config = $container->get('config');
52+
/** @var array $dbConfig */
53+
$dbConfig = $config['db'] ?? [];
54+
/** @var array $adapterConfig */
55+
$adapterConfig = $dbConfig['adapters'][$requestedName] ?? [];
56+
/** @var array $options */
57+
$options = $adapterConfig['options'] ?? [];
58+
59+
/** @var ConnectionInterface&Connection $connectionInstance */
60+
$connectionInstance = $connectionFactory::createFromConfig($container, $requestedName);
61+
62+
/** @var StatementInterface&Statement $statementInstance */
63+
$statementInstance = $adapterManager->get(Statement::class);
64+
65+
/** @var ResultInterface&Result $resultInstance */
66+
$resultInstance = $adapterManager->get(Result::class);
67+
68+
return new PdoDriver(
69+
$connectionInstance,
70+
$statementInstance,
71+
$resultInstance,
72+
$options
73+
);
74+
}
4075
}

src/Container/PlatformInterfaceFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ public function __invoke(ContainerInterface $container): PlatformInterface&Mysql
3333

3434
return new Mysql($driverInstance);
3535
}
36+
37+
public static function fromDriver(DriverInterface $driverInstance): PlatformInterface&Mysql
38+
{
39+
return new Mysql($driverInstance);
40+
}
3641
}

0 commit comments

Comments
 (0)