|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpDb\Container; |
| 6 | + |
| 7 | +use Laminas\ServiceManager\Factory\AbstractFactoryInterface; |
| 8 | +use Laminas\ServiceManager\ServiceManager; |
| 9 | +use PhpDb\Adapter\Adapter; |
| 10 | +use PhpDb\Adapter\AdapterInterface; |
| 11 | +use PhpDb\Adapter\Driver\DriverInterface; |
| 12 | +use PhpDb\Adapter\Driver\PdoDriverInterface; |
| 13 | +use PhpDb\Adapter\Platform\PlatformInterface; |
| 14 | +use PhpDb\Adapter\Profiler\ProfilerInterface; |
| 15 | +use PhpDb\ConfigProvider; |
| 16 | +use PhpDb\Exception\ContainerException; |
| 17 | +use PhpDb\ResultSet\ResultSetInterface; |
| 18 | +use PSpell\Config; |
| 19 | +use Psr\Container\ContainerInterface; |
| 20 | + |
| 21 | +use function is_array; |
| 22 | + |
| 23 | +/** |
| 24 | + * Database adapter abstract service factory. |
| 25 | + * |
| 26 | + * Allows configuring several database instances (such as writer and reader). |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +class AbstractAdapterInterfaceFactory implements AbstractFactoryInterface |
| 31 | +{ |
| 32 | + protected ?array $config = null; |
| 33 | + |
| 34 | + /** |
| 35 | + * Can we create an adapter by the requested name? |
| 36 | + * |
| 37 | + * @param string $requestedName |
| 38 | + */ |
| 39 | + public function canCreate(ContainerInterface $container, $requestedName): bool |
| 40 | + { |
| 41 | + $config = $this->getConfig($container); |
| 42 | + |
| 43 | + if ($config === []) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + return isset($config[$requestedName]) |
| 48 | + && is_array($config[$requestedName]) |
| 49 | + && ! empty($config[$requestedName]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create a DB adapter |
| 54 | + * |
| 55 | + * @param string $requestedName |
| 56 | + */ |
| 57 | + public function __invoke( |
| 58 | + ContainerInterface&ServiceManager $container, |
| 59 | + $requestedName, |
| 60 | + ?array $options = null |
| 61 | + ): AdapterInterface&Adapter { |
| 62 | + /** @var string|null $driverClass */ |
| 63 | + $driverClass = $this->config[$requestedName]['driver'] ?? null; |
| 64 | + |
| 65 | + if ($driverClass === null) { |
| 66 | + throw ContainerException::forService( |
| 67 | + $requestedName, |
| 68 | + self::class, |
| 69 | + 'no driver configured' |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** @var DriverInterface|PdoDriverInterface $driver */ |
| 74 | + $driver = $container->build($driverClass, $this->config[$requestedName]); |
| 75 | + /** @var PlatformInterface&Pgsql\AdapterPlatform $platform */ |
| 76 | + $platform = $container->build(PlatformInterface::class, ['driver' => $driver]); |
| 77 | + /** @var ResultSetInterface|null $resultSet */ |
| 78 | + $resultSet = $container->has(ResultSetInterface::class) |
| 79 | + ? $container->build(ResultSetInterface::class) |
| 80 | + : null; |
| 81 | + /** @var ProfilerInterface|null $profiler */ |
| 82 | + $profiler = $container->has(ProfilerInterface::class) |
| 83 | + ? $container->build(ProfilerInterface::class) |
| 84 | + : null; |
| 85 | + |
| 86 | + return match (true) { |
| 87 | + $resultSet !== null && $profiler !== null => new Adapter( |
| 88 | + driver: $driver, |
| 89 | + platform: $platform, |
| 90 | + queryResultSetPrototype: $resultSet, |
| 91 | + profiler: $profiler, |
| 92 | + ), |
| 93 | + $resultSet !== null => new Adapter( |
| 94 | + driver: $driver, |
| 95 | + platform: $platform, |
| 96 | + queryResultSetPrototype: $resultSet, |
| 97 | + ), |
| 98 | + $profiler !== null => new Adapter( |
| 99 | + driver: $driver, |
| 100 | + platform: $platform, |
| 101 | + profiler: $profiler, |
| 102 | + ), |
| 103 | + default => new Adapter( |
| 104 | + driver: $driver, |
| 105 | + platform: $platform, |
| 106 | + ), |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get db configuration, if any |
| 112 | + * todo: refactor to use PhpDb\ConfigProvider::NAMED_ADAPTER_KEY instead of hardcoding 'adapters' |
| 113 | + */ |
| 114 | + protected function getConfig(ContainerInterface $container): array |
| 115 | + { |
| 116 | + if ($this->config !== null) { |
| 117 | + return $this->config; |
| 118 | + } |
| 119 | + |
| 120 | + if (! $container->has('config')) { |
| 121 | + $this->config = []; |
| 122 | + return $this->config; |
| 123 | + } |
| 124 | + |
| 125 | + $config = $container->get('config'); |
| 126 | + $this->config = $config[AdapterInterface::class][ConfigProvider::NAMED_ADAPTER_KEY] ?? []; |
| 127 | + return $this->config; |
| 128 | + } |
| 129 | +} |
0 commit comments