1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Factory; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerExceptionInterface; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use RuntimeException; |
11
|
|
|
use Spiral\Database\Config\DatabaseConfig; |
12
|
|
|
use Spiral\Database\DatabaseManager; |
13
|
|
|
use Spiral\Database\Driver\Driver; |
14
|
|
|
use Yiisoft\Aliases\Aliases; |
15
|
|
|
|
16
|
|
|
final class DbalFactory |
17
|
|
|
{ |
18
|
|
|
/** @var array|DatabaseConfig */ |
19
|
|
|
private $dbalConfig; |
20
|
|
|
/** @var null|string|LoggerInterface */ |
21
|
|
|
private $logger = null; |
22
|
|
|
private ?ContainerInterface $container = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array|DatabaseConfig $config |
26
|
|
|
*/ |
27
|
|
|
public function __construct($config) |
28
|
|
|
{ |
29
|
|
|
if (is_array($config) && array_key_exists('query-logger', $config)) { |
30
|
|
|
$this->logger = $config['query-logger']; |
31
|
|
|
unset($config['query-logger']); |
32
|
|
|
} |
33
|
|
|
$this->dbalConfig = $config; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __invoke(ContainerInterface $container) |
37
|
|
|
{ |
38
|
|
|
$this->container = $container; |
39
|
|
|
$conf = $this->prepareConfig($this->dbalConfig); |
40
|
|
|
$dbal = new DatabaseManager($conf); |
41
|
|
|
|
42
|
|
|
if ($this->logger !== null) { |
43
|
|
|
$logger = $this->prepareLogger($this->logger); |
44
|
|
|
$dbal->setLogger($logger); |
45
|
|
|
/** Remove when issue is resolved {@link https://github.com/cycle/orm/issues/60} */ |
46
|
|
|
$drivers = $dbal->getDrivers(); |
47
|
|
|
array_walk($drivers, static fn (Driver $driver) => $driver->setLogger($logger)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $dbal; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string|LoggerInterface $logger |
55
|
|
|
* @return LoggerInterface |
56
|
|
|
* @throws RuntimeException |
57
|
|
|
* @throws ContainerExceptionInterface |
58
|
|
|
*/ |
59
|
|
|
private function prepareLogger($logger): LoggerInterface |
60
|
|
|
{ |
61
|
|
|
if (is_string($logger)) { |
62
|
|
|
$logger = $this->container->get($logger); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
if (!$logger instanceof LoggerInterface) { |
65
|
|
|
throw new RuntimeException( |
66
|
|
|
sprintf('Logger definition should be subclass of %s.', LoggerInterface::class) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
return $logger; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array|DatabaseConfig $config |
74
|
|
|
* @return DatabaseConfig |
75
|
|
|
*/ |
76
|
|
|
private function prepareConfig($config): DatabaseConfig |
77
|
|
|
{ |
78
|
|
|
if ($config instanceof DatabaseConfig) { |
79
|
|
|
return $config; |
80
|
|
|
} |
81
|
|
|
if (isset($config['connections'])) { |
82
|
|
|
// prepare connections |
83
|
|
|
foreach ($config['connections'] as &$connection) { |
84
|
|
|
$connection = $this->prepareConnection($connection); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return new DatabaseConfig($config); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function prepareConnection(array $connection): array |
92
|
|
|
{ |
93
|
|
|
// if connection option contain alias in path |
94
|
|
|
if (isset($connection['connection']) && preg_match('/^(?<proto>\w+:)?@/', $connection['connection'], $m)) { |
95
|
|
|
$proto = $m['proto']; |
96
|
|
|
$path = $this->getAlias(substr($connection['connection'], strlen($proto))); |
97
|
|
|
$connection['connection'] = $proto . $path; |
98
|
|
|
} |
99
|
|
|
return $connection; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getAlias(string $alias): string |
103
|
|
|
{ |
104
|
|
|
return $this->container->get(Aliases::class)->get($alias); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.