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