Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
use PhpDb\Adapter\Platform\PlatformInterface;
use PhpDb\Adapter\Profiler\Profiler;
use PhpDb\Adapter\Profiler\ProfilerInterface;
use PhpDb\Container\AdapterAbstractServiceFactory;
use PhpDb\Container\AdapterManager;
use PhpDb\Container\ConnectionInterfaceFactoryFactoryInterface;
use PhpDb\Container\DriverInterfaceFactoryFactoryInterface;
use PhpDb\Container\MetadataFactory;
use PhpDb\Container\PlatformInterfaceFactoryFactoryInterface;
use PhpDb\Metadata\MetadataInterface;
use PhpDb\ResultSet;

Expand All @@ -34,6 +39,9 @@ public function __invoke(): array
public function getDependencies(): array
{
return [
'abstract_factories' => [
AdapterAbstractServiceFactory::class,
],
'aliases' => [
MetadataInterface::class => Metadata\Source\SqliteMetadata::class,
],
Expand Down Expand Up @@ -68,6 +76,9 @@ public function getAdapterManagerConfig(): array
ResultInterface::class => Result::class,
ResultSet\ResultSetInterface::class => ResultSet\ResultSet::class,
StatementInterface::class => Statement::class,
ConnectionInterfaceFactoryFactoryInterface::class => Container\ConnectionInterfaceFactoryFactory::class,
DriverInterfaceFactoryFactoryInterface::class => Container\DriverInterfaceFactoryFactory::class,
PlatformInterfaceFactoryFactoryInterface::class => Container\PlatformInterfaceFactoryFactory::class,
],
'factories' => [
AdapterInterface::class => Container\AdapterFactory::class,
Expand All @@ -79,6 +90,14 @@ public function getAdapterManagerConfig(): array
Profiler::class => InvokableFactory::class,
ResultSet\ResultSet::class => InvokableFactory::class,
],
'invokables' => [
Container\ConnectionInterfaceFactoryFactory::class
=> Container\ConnectionInterfaceFactoryFactory::class,
Container\DriverInterfaceFactoryFactory::class
=> Container\DriverInterfaceFactoryFactory::class,
Container\PlatformInterfaceFactoryFactory::class
=> Container\PlatformInterfaceFactoryFactory::class,
],
];
}
}
44 changes: 44 additions & 0 deletions src/Container/ConnectionInterfaceFactoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
)),
};
}
}
34 changes: 34 additions & 0 deletions src/Container/DriverInterfaceFactoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace PhpDb\Adapter\Sqlite\Container;

use PhpDb\Container\AdapterManager;
use PhpDb\Container\DriverInterfaceFactoryFactoryInterface as FactoryFactoryInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;

use function sprintf;

final class DriverInterfaceFactoryFactory 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'];
$aliasTo ??= $adapterServices['aliases'][$configuredDriver] ?? $configuredDriver;
$driverFactory = $adapterServices['factories'][$aliasTo];
return new $driverFactory();
}
}
8 changes: 8 additions & 0 deletions src/Container/PdoConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public function __invoke(ContainerInterface $container): ConnectionInterface&Con

return new Connection($connectionConfig);
}

public static function createFromConfig(
ContainerInterface $container,
string $requestedName
): ConnectionInterface&Connection {
$adapterConfig = $container->get('config')['db']['adapters'][$requestedName] ?? [];
return new Connection($adapterConfig['connection'] ?? []);
}
}
27 changes: 27 additions & 0 deletions src/Container/PdoDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,31 @@ public function __invoke(ContainerInterface $container): PdoDriverInterface&PdoD
[new SqliteRowCounter()],
);
}

public static function createFromConfig(
ContainerInterface $container,
string $requestedName,
): PdoDriverInterface&PdoDriver {
/** @var AdapterManager $adapterManager */
$adapterManager = $container->get(AdapterManager::class);
$connectionFactory = (
$adapterManager->get(ConnectionInterfaceFactoryFactory::class)
)($container, $requestedName);

/** @var ConnectionInterface&Connection $connectionInstance */
$connectionInstance = $connectionFactory::createFromConfig($container, $requestedName);

/** @var StatementInterface&Statement $statementInstance */
$statementInstance = $adapterManager->get(Statement::class);

/** @var ResultInterface&Result $resultInstance */
$resultInstance = $adapterManager->get(Result::class);

return new PdoDriver(
$connectionInstance,
$statementInstance,
$resultInstance,
[new SqliteRowCounter()],
);
}
}
5 changes: 5 additions & 0 deletions src/Container/PlatformInterfaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public function __invoke(ContainerInterface $container): PlatformInterface&Sqlit

return new Sqlite($driverInstance);
}

public static function fromDriver(PdoDriverInterface $driverInstance): PlatformInterface&Sqlite
{
return new Sqlite($driverInstance);
}
}
16 changes: 16 additions & 0 deletions src/Container/PlatformInterfaceFactoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace PhpDb\Adapter\Sqlite\Container;

use PhpDb\Adapter\Sqlite\Container\PlatformInterfaceFactory;
use PhpDb\Container\PlatformInterfaceFactoryFactoryInterface as FactoryFactoryInterface;

final class PlatformInterfaceFactoryFactory implements FactoryFactoryInterface
{
public function __invoke(): callable
{
return new PlatformInterfaceFactory();
}
}
1 change: 1 addition & 0 deletions src/Driver/Pdo/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class Connection extends AbstractPdoConnection
{
public final const CURRENT_SCHEMA = 'main';

/**
* {@inheritDoc}
*/
Expand Down
Loading