Skip to content

Commit 028f39a

Browse files
committed
Slight refactor to align code with php-db/phpdb
Removes non class-name service identifiers Implements changes needed for AbstractAdapterServiceFactory to initialize a Pdo instance. Signed-off-by: Joey Smith <jsmith@webinertia.net> Signed-off-by: Joey Smith <jsmith@webinertia.net>
1 parent 9f7d523 commit 028f39a

9 files changed

+99
-46
lines changed

src/ConfigProvider.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use PhpDb\Adapter\Profiler;
1717
use PhpDb\Container\AdapterAbstractServiceFactory;
1818
use PhpDb\Container\AdapterManager;
19+
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface;
20+
use PhpDb\Container\DriverInterfaceFactoryFactoryInterface;
21+
use PhpDb\Container\PlatformInterfaceFactoryFactoryInterface;
1922
use PhpDb\Container\MetadataFactory;
2023
use PhpDb\Metadata\MetadataInterface;
2124
use PhpDb\ResultSet;
@@ -54,24 +57,24 @@ public function getAdapterManagerConfig(): array
5457
{
5558
return [
5659
'aliases' => [
57-
'MySqli' => Driver\Mysqli\Mysqli::class,
58-
'MySQLi' => Driver\Mysqli\Mysqli::class,
59-
'Mysqli' => Driver\Mysqli\Mysqli::class,
60-
'mysqli' => Driver\Mysqli\Mysqli::class,
61-
'PDO_MySQL' => Driver\Pdo\Pdo::class,
62-
'Pdo_MySQL' => Driver\Pdo\Pdo::class,
63-
'Pdo_Mysql' => Driver\Pdo\Pdo::class,
64-
'pdo_mysql' => Driver\Pdo\Pdo::class,
65-
'pdomysql' => Driver\Pdo\Pdo::class,
66-
'pdodriver' => Driver\Pdo\Pdo::class,
67-
'pdo' => Driver\Pdo\Pdo::class,
68-
DriverInterface::class => Driver\Mysqli\Mysqli::class,
69-
PdoDriverInterface::class => Driver\Pdo\Pdo::class,
70-
Profiler\ProfilerInterface::class => Profiler\Profiler::class,
71-
ResultSet\ResultSetInterface::class => ResultSet\ResultSet::class,
72-
'ConnectionFactoryFactory' => Container\ConnectionFactoryFactory::class,
73-
'DriverFactoryFactory' => Container\DriverFactoryFactory::class,
74-
'PlatformFactoryFactory' => Container\PlatformFactoryFactory::class,
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,
7578
],
7679
'factories' => [
7780
AdapterInterface::class => Container\AdapterFactory::class,
@@ -88,9 +91,9 @@ public function getAdapterManagerConfig(): array
8891
ResultSet\ResultSet::class => InvokableFactory::class,
8992
],
9093
'invokables' => [
91-
Container\ConnectionFactoryFactory::class => Container\ConnectionFactoryFactory::class,
92-
Container\DriverFactoryFactory::class => Container\DriverFactoryFactory::class,
93-
Container\PlatformFactoryFactory::class => Container\PlatformFactoryFactory::class,
94+
Container\ConnectionInterfaceFactoryFactory::class => Container\ConnectionInterfaceFactoryFactory::class,
95+
Container\DriverInterfaceFactoryFactory::class => Container\DriverInterfaceFactoryFactory::class,
96+
Container\PlatformInterfaceFactoryFactory::class => Container\PlatformInterfaceFactoryFactory::class,
9497
],
9598
];
9699
}

src/Container/ConnectionFactoryFactory.php renamed to src/Container/ConnectionInterfaceFactoryFactory.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44

55
namespace PhpDb\Adapter\Mysql\Container;
66

7-
use PhpDb\Container\AdapterManager;
87
use PhpDb\Adapter\Mysql\Container\MysqliConnectionFactory;
98
use PhpDb\Adapter\Mysql\Container\PdoConnectionFactory;
109
use PhpDb\Adapter\Mysql\Driver\Mysqli\Mysqli;
1110
use PhpDb\Adapter\Mysql\Driver\Pdo\Pdo;
11+
use PhpDb\Container\AdapterManager;
12+
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
1213
use Psr\Container\ContainerInterface;
1314
use RuntimeException;
1415

1516
use function array_key_exists;
1617
use function sprintf;
1718

18-
final class ConnectionFactoryFactory
19+
final class ConnectionInterfaceFactoryFactory implements FactoryFactoryInterface
1920
{
2021
public function __invoke(
21-
ContainerInterface $container,
22-
string $requestedName
22+
?ContainerInterface $container = null,
23+
?string $requestedName = null
2324
): callable {
2425
$adapterConfig = $container->get('config')['db']['adapters'] ?? [];
2526
if (! isset($adapterConfig[$requestedName]['driver'])) {

src/Container/DriverFactoryFactory.php renamed to src/Container/DriverInterfaceFactoryFactory.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
namespace PhpDb\Adapter\Mysql\Container;
66

77
use PhpDb\Container\AdapterManager;
8+
use PhpDb\Container\DriverInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
89
use Psr\Container\ContainerInterface;
910

1011
use function sprintf;
1112

12-
final class DriverFactoryFactory
13+
final class DriverInterfaceFactoryFactory implements FactoryFactoryInterface
1314
{
14-
public function __invoke(ContainerInterface $container, string $requestedName): callable
15-
{
15+
public function __invoke(
16+
?ContainerInterface $container = null,
17+
?string $requestedName = null
18+
): callable {
1619
$adapterConfig = $container->get('config')['db']['adapters'] ?? [];
1720
if (! isset($adapterConfig[$requestedName]['driver'])) {
1821
throw new \RuntimeException(sprintf(

src/Container/MysqliConnectionFactory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public function __invoke(
2828
return new Connection($connectionConfig);
2929
}
3030

31-
public static function createFromConfig(ContainerInterface $container, string $requestedName): ConnectionInterface&Connection
32-
{
31+
public static function createFromConfig(
32+
ContainerInterface $container,
33+
string $requestedName
34+
): ConnectionInterface&Connection {
3335
$adapterConfig = $container->get('config')['db']['adapters'][$requestedName] ?? [];
3436
return new Connection($adapterConfig['connection'] ?? []);
3537
}

src/Container/MysqliDriverFactory.php

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

77
use PhpDb\Adapter\Driver;
8+
use PhpDb\Adapter\Mysql\Container\ConnectionInterfaceFactoryFactory as ConnectionFactoryFactory;
89
use PhpDb\Adapter\Mysql\Driver\Mysqli;
910
use PhpDb\Container\AdapterManager;
1011
use Psr\Container\ContainerInterface;
@@ -49,7 +50,7 @@ public static function createFromConfig(
4950

5051
/** @var AdapterManager $adapterManager */
5152
$adapterManager = $container->get(AdapterManager::class);
52-
$connectionFactory = ($adapterManager->get('ConnectionFactoryFactory'))($container, $requestedName);
53+
$connectionFactory = ($adapterManager->get(ConnectionInterfaceFactoryFactory::class))($container, $requestedName);
5354
/** @var array $config */
5455
$config = $container->get('config');
5556
/** @var array $dbConfig */

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,38 @@ 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+
46+
/** @var AdapterManager $adapterManager */
47+
$adapterManager = $container->get(AdapterManager::class);
48+
$connectionFactory = ($adapterManager->get(ConnectionInterfaceFactoryFactory::class ))($container, $requestedName);
49+
/** @var array $config */
50+
$config = $container->get('config');
51+
/** @var array $dbConfig */
52+
$dbConfig = $config['db'] ?? [];
53+
/** @var array $adapterConfig */
54+
$adapterConfig = $dbConfig['adapters'][$requestedName] ?? [];
55+
/** @var array $options */
56+
$options = $adapterConfig['options'] ?? [];
57+
58+
/** @var ConnectionInterface&Connection $connectionInstance */
59+
$connectionInstance = $connectionFactory::createFromConfig($container, $requestedName);
60+
61+
/** @var StatementInterface&Statement $statementInstance */
62+
$statementInstance = $adapterManager->get(Statement::class);
63+
64+
/** @var ResultInterface&Result $resultInstance */
65+
$resultInstance = $adapterManager->get(Result::class);
66+
67+
return new PdoDriver(
68+
$connectionInstance,
69+
$statementInstance,
70+
$resultInstance,
71+
$options
72+
);
73+
}
4074
}

src/Container/PlatformFactoryFactory.php

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Adapter\Mysql\Container;
6+
7+
use PhpDb\Adapter\Mysql\Container\PlatformInterfaceFactory;
8+
use PhpDb\Container\PlatformInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
9+
10+
final class PlatformInterfaceFactoryFactory implements FactoryFactoryInterface
11+
{
12+
public function __invoke(): callable
13+
{
14+
return new PlatformInterfaceFactory();
15+
}
16+
}

0 commit comments

Comments
 (0)