forked from php-db/phpdb-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapterFactory.php
More file actions
83 lines (66 loc) · 2.69 KB
/
AdapterFactory.php
File metadata and controls
83 lines (66 loc) · 2.69 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace PhpDb\Adapter\Sqlite\Container;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use PhpDb\Adapter\Adapter;
use PhpDb\Adapter\AdapterInterface;
use PhpDb\Adapter\Driver\DriverInterface;
use PhpDb\Adapter\Driver\PdoDriverInterface;
use PhpDb\Adapter\Exception\RuntimeException;
use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Adapter\Profiler\ProfilerInterface;
use PhpDb\Container\AdapterManager;
use PhpDb\ResultSet\ResultSetInterface;
use Psr\Container\ContainerInterface;
use function sprintf;
final class AdapterFactory
{
public function __invoke(ContainerInterface $container): AdapterInterface
{
/** @var AdapterManager $adapterManager */
$adapterManager = $container->get(AdapterManager::class);
/** @var array $config */
$config = $container->get('config');
/** @var array $dbConfig */
$dbConfig = $config['db'] ?? [];
if (! isset($dbConfig['driver'])) {
throw new RuntimeException('Database driver configuration is missing.');
}
/** @var string $driver */
$driver = $dbConfig['driver'];
if (! $adapterManager->has($driver)) {
throw new ServiceNotFoundException(sprintf(
'Database driver "%s" is not registered in the adapter manager.',
$driver
));
}
/** @var DriverInterface|PdoDriverInterface $driverInstance */
$driverInstance = $adapterManager->get($driver);
if (! $adapterManager->has(PlatformInterface::class)) {
throw new ServiceNotFoundException(sprintf(
'Database platform "%s" is not registered in the adapter manager.',
PlatformInterface::class
));
}
/** @var PlatformInterface $platformInstance */
$platformInstance = $adapterManager->get(PlatformInterface::class);
if (! $adapterManager->has(ResultSetInterface::class)) {
throw new ServiceNotFoundException(sprintf(
'ResultSet "%s" is not registered in the adapter manager.',
ResultSetInterface::class
));
}
/** @var ResultSetInterface $resultSetInstance */
$resultSetInstance = $adapterManager->get(ResultSetInterface::class);
/** @var ProfilerInterface|null $profilerInstanceOrNull */
$profilerInstanceOrNull = $adapterManager->has(ProfilerInterface::class)
? $adapterManager->get(ProfilerInterface::class)
: null;
return new Adapter(
$driverInstance,
$platformInstance,
$resultSetInstance,
$profilerInstanceOrNull
);
}
}