1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection\Factory; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Configuration; |
6
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
7
|
|
|
use Bdf\Prime\Connection\SimpleConnection; |
8
|
|
|
use Bdf\Prime\Exception\DBALException; |
9
|
|
|
use Bdf\Prime\MongoDB\Driver\MongoConnection; |
|
|
|
|
10
|
|
|
use Bdf\Prime\MongoDB\Driver\MongoDriver; |
|
|
|
|
11
|
|
|
use Doctrine\Common\EventManager; |
12
|
|
|
use Doctrine\DBAL\Exception as DoctrineDBALException; |
13
|
|
|
use Doctrine\DBAL\DriverManager; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ConnectionFactory |
17
|
|
|
* |
18
|
|
|
* Create simple connection instance |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
class ConnectionFactory implements ConnectionFactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The drivers map |
24
|
|
|
* |
25
|
|
|
* @var array<string, array{0: class-string<\Doctrine\DBAL\Driver>, 1: class-string<\Doctrine\DBAL\Driver\Connection>}> |
|
|
|
|
26
|
|
|
* |
27
|
|
|
* @psalm-suppress UndefinedClass |
|
|
|
|
28
|
|
|
* @psalm-suppress InvalidPropertyAssignmentValue |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
static private $driversMap = [ |
|
|
|
|
31
|
|
|
'mongodb' => [MongoDriver::class, MongoConnection::class], |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
169 |
|
public function create(string $connectionName, array $parameters, ?Configuration $config = null): ConnectionInterface |
38
|
|
|
{ |
39
|
169 |
|
$connection = $this->createConnection($parameters, $config); |
40
|
|
|
|
41
|
|
|
// Store connection and return adapter instance |
42
|
169 |
|
$connection->setName($connectionName); |
43
|
|
|
|
44
|
169 |
|
return $connection; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
68 |
|
public function support(string $connectionName, array $parameters): bool |
51
|
|
|
{ |
52
|
68 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create the instance of the connection |
57
|
|
|
* |
58
|
|
|
* @param array{wrapperClass?: class-string<T>} $parameters |
|
|
|
|
59
|
|
|
* @param Configuration|null $config |
60
|
|
|
* @param EventManager|null $eventManager The event manager, optional. |
61
|
|
|
* |
62
|
|
|
* @return ConnectionInterface |
63
|
|
|
* @throws DBALException |
64
|
|
|
* |
65
|
|
|
* @template T as ConnectionInterface |
66
|
|
|
*/ |
67
|
169 |
|
private function createConnection(array $parameters, Configuration $config = null, EventManager $eventManager = null): ConnectionInterface |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
// Set the custom driver class + wrapper |
70
|
169 |
|
if (isset($parameters['driver']) && isset(self::$driversMap[$parameters['driver']])) { |
71
|
|
|
list($parameters['driverClass'], $parameters['wrapperClass']) = self::$driversMap[$parameters['driver']]; |
72
|
|
|
unset($parameters['driver']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Replace 'adapter' with 'driver' and add 'pdo_' |
76
|
169 |
|
if (isset($parameters['adapter'])) { |
77
|
135 |
|
$parameters['driver'] = 'pdo_' . $parameters['adapter']; |
78
|
135 |
|
unset($parameters['adapter']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// default charset |
|
|
|
|
82
|
169 |
|
if (!isset($parameters['charset'])) { |
83
|
163 |
|
$parameters['charset'] = 'utf8'; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// default wrapper |
|
|
|
|
87
|
169 |
|
if (!isset($parameters['wrapperClass'])) { |
88
|
169 |
|
$parameters['wrapperClass'] = SimpleConnection::class; |
89
|
|
|
} |
90
|
|
|
|
91
|
169 |
|
if ($config === null) { |
92
|
75 |
|
$config = new Configuration(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
/** |
|
|
|
|
97
|
|
|
* @var T |
98
|
|
|
* @psalm-suppress InvalidArgument |
99
|
|
|
*/ |
100
|
169 |
|
return DriverManager::getConnection($parameters, $config, $eventManager); |
|
|
|
|
101
|
|
|
} catch (DoctrineDBALException $e) { |
102
|
|
|
/** @psalm-suppress InvalidScalarArgument */ |
|
|
|
|
103
|
|
|
throw new DBALException('Cannot create the connection : '.$e->getMessage(), $e->getCode(), $e); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Register a global driver map |
109
|
|
|
* |
110
|
|
|
* @param string $name |
111
|
|
|
* @param class-string<\Doctrine\DBAL\Driver> $driver |
|
|
|
|
112
|
|
|
* @param class-string<\Doctrine\DBAL\Driver\Connection>|null $wrapper |
|
|
|
|
113
|
|
|
*/ |
114
|
1 |
|
public static function registerDriverMap(string $name, string $driver, ?string $wrapper = null): void |
115
|
|
|
{ |
116
|
1 |
|
self::$driversMap[$name] = [$driver, $wrapper]; |
117
|
1 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get a global driver map |
121
|
|
|
* |
122
|
|
|
* @param string $name |
123
|
|
|
* |
124
|
|
|
* @return array{0: class-string<\Doctrine\DBAL\Driver>, 1: class-string<\Doctrine\DBAL\Driver\Connection>}|null |
|
|
|
|
125
|
|
|
*/ |
126
|
1 |
|
public static function getDriverMap(string $name): ?array |
127
|
|
|
{ |
128
|
1 |
|
return self::$driversMap[$name] ?? null; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Unregister a global driver map |
133
|
|
|
* |
134
|
|
|
* @param string $name |
135
|
|
|
*/ |
136
|
1 |
|
public static function unregisterDriverMap(string $name): void |
137
|
|
|
{ |
138
|
1 |
|
unset(self::$driversMap[$name]); |
139
|
1 |
|
} |
140
|
|
|
} |
141
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths