forked from php-db/phpdb-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionInterfaceFactoryFactory.php
More file actions
44 lines (39 loc) · 1.51 KB
/
ConnectionInterfaceFactoryFactory.php
File metadata and controls
44 lines (39 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace PhpDb\Adapter\Sqlite\Container;
use PhpDb\Adapter\Sqlite\Driver\Pdo\Pdo;
use PhpDb\Container\AdapterManager;
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;
use function array_key_exists;
use function sprintf;
final class ConnectionInterfaceFactoryFactory implements FactoryFactoryInterface
{
public function __invoke(
?ContainerInterface $container = null,
?string $requestedName = null
): callable {
$adapterConfig = $container->get('config')['db']['adapters'] ?? [];
if (! isset($adapterConfig[$requestedName]['driver'])) {
throw new RuntimeException(sprintf(
'Named adapter "%s" is not configured with a driver',
$requestedName
));
}
$adapterServices = $container->get('config')[AdapterManager::class];
$configuredDriver = $adapterConfig[$requestedName]['driver'];
if (array_key_exists($configuredDriver, $adapterServices['aliases'])) {
$aliasTo = $adapterServices['aliases'][$configuredDriver];
} else {
$aliasTo = $configuredDriver;
}
return match ($aliasTo) {
Pdo::class => new PdoConnectionFactory(),
default => throw new RuntimeException(sprintf(
'No connection factory found for driver "%s"',
$configuredDriver
)),
};
}
}