Skip to content

Commit b2a8f7a

Browse files
authored
Merge pull request #18 from tyrsson/fix-abstract-adapter
Fixes the AdapterAbstractServiceFactory usage.
2 parents e50c723 + 64964ce commit b2a8f7a

8 files changed

+154
-0
lines changed

src/ConfigProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
use PhpDb\Adapter\Platform\PlatformInterface;
1818
use PhpDb\Adapter\Profiler\Profiler;
1919
use PhpDb\Adapter\Profiler\ProfilerInterface;
20+
use PhpDb\Container\AdapterAbstractServiceFactory;
2021
use PhpDb\Container\AdapterManager;
22+
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface;
23+
use PhpDb\Container\DriverInterfaceFactoryFactoryInterface;
24+
use PhpDb\Container\MetadataFactory;
25+
use PhpDb\Container\PlatformInterfaceFactoryFactoryInterface;
2126
use PhpDb\Metadata\MetadataInterface;
2227
use PhpDb\ResultSet;
2328

@@ -34,6 +39,9 @@ public function __invoke(): array
3439
public function getDependencies(): array
3540
{
3641
return [
42+
'abstract_factories' => [
43+
AdapterAbstractServiceFactory::class,
44+
],
3745
'aliases' => [
3846
MetadataInterface::class => Metadata\Source\SqliteMetadata::class,
3947
],
@@ -68,6 +76,9 @@ public function getAdapterManagerConfig(): array
6876
ResultInterface::class => Result::class,
6977
ResultSet\ResultSetInterface::class => ResultSet\ResultSet::class,
7078
StatementInterface::class => Statement::class,
79+
ConnectionInterfaceFactoryFactoryInterface::class => Container\ConnectionInterfaceFactoryFactory::class,
80+
DriverInterfaceFactoryFactoryInterface::class => Container\DriverInterfaceFactoryFactory::class,
81+
PlatformInterfaceFactoryFactoryInterface::class => Container\PlatformInterfaceFactoryFactory::class,
7182
],
7283
'factories' => [
7384
AdapterInterface::class => Container\AdapterFactory::class,
@@ -79,6 +90,14 @@ public function getAdapterManagerConfig(): array
7990
Profiler::class => InvokableFactory::class,
8091
ResultSet\ResultSet::class => InvokableFactory::class,
8192
],
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+
],
82101
];
83102
}
84103
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpDb\Adapter\Sqlite\Container;
6+
7+
use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo;
8+
use PhpDb\Container\AdapterManager;
9+
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
10+
use Psr\Container\ContainerInterface;
11+
use RuntimeException;
12+
13+
use function array_key_exists;
14+
use function sprintf;
15+
16+
final class ConnectionInterfaceFactoryFactory implements FactoryFactoryInterface
17+
{
18+
public function __invoke(
19+
?ContainerInterface $container = null,
20+
?string $requestedName = null
21+
): callable {
22+
$adapterConfig = $container->get('config')['db']['adapters'] ?? [];
23+
if (! isset($adapterConfig[$requestedName]['driver'])) {
24+
throw new RuntimeException(sprintf(
25+
'Named adapter "%s" is not configured with a driver',
26+
$requestedName
27+
));
28+
}
29+
$adapterServices = $container->get('config')[AdapterManager::class];
30+
$configuredDriver = $adapterConfig[$requestedName]['driver'];
31+
if (array_key_exists($configuredDriver, $adapterServices['aliases'])) {
32+
$aliasTo = $adapterServices['aliases'][$configuredDriver];
33+
} else {
34+
$aliasTo = $configuredDriver;
35+
}
36+
return match ($aliasTo) {
37+
Pdo::class => new PdoConnectionFactory(),
38+
default => throw new RuntimeException(sprintf(
39+
'No connection factory found for driver "%s"',
40+
$configuredDriver
41+
)),
42+
};
43+
}
44+
}
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\Sqlite\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/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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,31 @@ public function __invoke(ContainerInterface $container): PdoDriverInterface&PdoD
3939
[new SqliteRowCounter()],
4040
);
4141
}
42+
43+
public static function createFromConfig(
44+
ContainerInterface $container,
45+
string $requestedName,
46+
): PdoDriverInterface&PdoDriver {
47+
/** @var AdapterManager $adapterManager */
48+
$adapterManager = $container->get(AdapterManager::class);
49+
$connectionFactory = (
50+
$adapterManager->get(ConnectionInterfaceFactoryFactory::class)
51+
)($container, $requestedName);
52+
53+
/** @var ConnectionInterface&Connection $connectionInstance */
54+
$connectionInstance = $connectionFactory::createFromConfig($container, $requestedName);
55+
56+
/** @var StatementInterface&Statement $statementInstance */
57+
$statementInstance = $adapterManager->get(Statement::class);
58+
59+
/** @var ResultInterface&Result $resultInstance */
60+
$resultInstance = $adapterManager->get(Result::class);
61+
62+
return new PdoDriver(
63+
$connectionInstance,
64+
$statementInstance,
65+
$resultInstance,
66+
[new SqliteRowCounter()],
67+
);
68+
}
4269
}

src/Container/PlatformInterfaceFactory.php

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

3333
return new Sqlite($driverInstance);
3434
}
35+
36+
public static function fromDriver(PdoDriverInterface $driverInstance): PlatformInterface&Sqlite
37+
{
38+
return new Sqlite($driverInstance);
39+
}
3540
}
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\Sqlite\Container;
6+
7+
use PhpDb\Adapter\Sqlite\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+
}

src/Driver/Pdo/Connection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
class Connection extends AbstractPdoConnection
2323
{
2424
public final const CURRENT_SCHEMA = 'main';
25+
2526
/**
2627
* {@inheritDoc}
2728
*/

0 commit comments

Comments
 (0)