|
4 | 4 |
|
5 | 5 | namespace PhpDb\Mysql\Container; |
6 | 6 |
|
| 7 | +use Laminas\ServiceManager\ServiceManager; |
7 | 8 | use PhpDb\Adapter\Driver\DriverInterface; |
| 9 | +use PhpDb\Adapter\Driver\ResultInterface; |
| 10 | +use PhpDb\Exception\ContainerException; |
8 | 11 | use PhpDb\Mysql\Connection; |
9 | 12 | use PhpDb\Mysql\Driver; |
10 | 13 | use PhpDb\Mysql\Result; |
|
13 | 16 |
|
14 | 17 | final class DriverInterfaceFactory |
15 | 18 | { |
16 | | - public function __invoke(ContainerInterface $container): DriverInterface&Driver |
17 | | - { |
18 | | - /** @var array $config */ |
19 | | - $config = $container->get('config'); |
20 | | - |
21 | | - /** @var array $dbConfig */ |
22 | | - $dbConfig = $config['db'] ?? []; |
23 | | - |
24 | | - /** @var array $options */ |
25 | | - $options = $dbConfig['options'] ?? []; |
| 19 | + public function __invoke( |
| 20 | + ContainerInterface&ServiceManager $container, |
| 21 | + string $requestedName, |
| 22 | + ?array $options = null |
| 23 | + ): DriverInterface&Driver { |
| 24 | + if (! isset($options['connection'])) { |
| 25 | + throw ContainerException::forService( |
| 26 | + Driver::class, |
| 27 | + self::class, |
| 28 | + '$options["connection"] must containe an array of connection configuration.' |
| 29 | + ); |
| 30 | + } |
26 | 31 |
|
27 | 32 | /** @var Driver\ConnectionInterface&Connection $connectionInstance */ |
28 | | - $connectionInstance = $container->get(Connection::class); |
| 33 | + $connectionInstance = $container->build(Connection::class, $options); |
29 | 34 |
|
30 | 35 | /** @var Driver\StatementInterface&Statement $statementInstance */ |
31 | | - $statementInstance = $container->get(Statement::class); |
| 36 | + $statementInstance = $container->build( |
| 37 | + Statement::class, |
| 38 | + $options['options'] ?? [] |
| 39 | + ); |
| 40 | + |
32 | 41 | /** @var Driver\ResultInterface&Result $resultInstance */ |
33 | | - $resultInstance = $container->get(Result::class); |
| 42 | + $resultInstance = $container->has(ResultInterface::class) |
| 43 | + ? $container->get(ResultInterface::class) |
| 44 | + : new Result(); |
34 | 45 |
|
35 | 46 | return new Driver( |
36 | 47 | $connectionInstance, |
|
0 commit comments