1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Factory\Service\Connection; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Config\ConnectionConfigs; |
8
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManager; |
9
|
|
|
use Arp\LaminasDoctrine\Service\Configuration\ConfigurationManagerInterface; |
10
|
|
|
use Arp\LaminasDoctrine\Service\Connection\ConnectionFactory; |
11
|
|
|
use Arp\LaminasFactory\AbstractFactory; |
12
|
|
|
use Doctrine\DBAL\Driver\PDO\MySQL\Driver; |
13
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
14
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
15
|
|
|
use Psr\Container\ContainerExceptionInterface; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
|
18
|
|
|
final class ConnectionFactoryFactory extends AbstractFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array<string, mixed> |
22
|
|
|
*/ |
23
|
|
|
private array $defaultConnectionConfig = [ |
24
|
|
|
'driverClass' => Driver::class, |
25
|
|
|
'driverOptions' => null, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ContainerInterface $container |
30
|
|
|
* @param string $requestedName |
31
|
|
|
* @param array<mixed>|null $options |
32
|
|
|
* |
33
|
|
|
* @return ConnectionFactory |
34
|
|
|
* |
35
|
|
|
* @throws ServiceNotCreatedException |
36
|
|
|
* @throws ServiceNotFoundException |
37
|
|
|
* @throws ContainerExceptionInterface |
38
|
|
|
*/ |
39
|
|
|
public function __invoke( |
40
|
|
|
ContainerInterface $container, |
41
|
|
|
string $requestedName, |
42
|
|
|
array $options = null |
43
|
|
|
): ConnectionFactory { |
44
|
|
|
if (null === $options) { |
45
|
|
|
/** @var ConnectionConfigs $connectionConfigs */ |
46
|
|
|
$connectionConfigs = $this->getService($container, ConnectionConfigs::class, $requestedName); |
47
|
|
|
|
48
|
|
|
$options = $connectionConfigs->hasConnectionConfig($requestedName) |
49
|
|
|
? $connectionConfigs->getConnectionConfig($requestedName) |
50
|
|
|
: []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$connectionFactory = $options['factory'] ?? null; |
54
|
|
|
if (null !== $connectionFactory && !is_callable($connectionFactory)) { |
55
|
|
|
throw new ServiceNotCreatedException( |
56
|
|
|
sprintf( |
57
|
|
|
'The \'factory\' must be of type \'callable\'; \'%s\' provided for service \'%s\'', |
58
|
|
|
is_object($connectionFactory) ? get_class($connectionFactory) : gettype($connectionFactory), |
59
|
|
|
$requestedName |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @var ConfigurationManager $configurationManager */ |
65
|
|
|
$configurationManager = $this->getService( |
66
|
|
|
$container, |
67
|
|
|
$options['manager'] ?? ConfigurationManagerInterface::class, |
68
|
|
|
$requestedName |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
return new ConnectionFactory($configurationManager, $connectionFactory, $this->defaultConnectionConfig); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array<mixed> $defaultConnectionConfig |
76
|
|
|
*/ |
77
|
|
|
public function setDefaultConnectionConfig(array $defaultConnectionConfig): void |
78
|
|
|
{ |
79
|
|
|
$this->defaultConnectionConfig = $defaultConnectionConfig; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|