1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Connection\Factory; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
6
|
|
|
use Bdf\Prime\Connection\SimpleConnection; |
7
|
|
|
use Bdf\Prime\Exception\DBALException; |
8
|
|
|
use Bdf\Prime\MongoDB\Driver\MongoConnection; |
|
|
|
|
9
|
|
|
use Bdf\Prime\MongoDB\Driver\MongoDriver; |
|
|
|
|
10
|
|
|
use Doctrine\Common\EventManager; |
11
|
|
|
use Doctrine\DBAL\Configuration; |
12
|
|
|
use Doctrine\DBAL\DBALException 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
|
|
|
static private $driversMap = [ |
|
|
|
|
28
|
|
|
'mongodb' => [MongoDriver::class, MongoConnection::class], |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
|
|
|
|
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
162 |
|
public function create(string $connectionName, array $parameters, Configuration $config): ConnectionInterface |
35
|
|
|
{ |
36
|
162 |
|
$connection = $this->createConnection($parameters, $config); |
37
|
|
|
|
38
|
|
|
// Store connection and return adapter instance |
39
|
162 |
|
if ($connection instanceof ConnectionInterface) { |
|
|
|
|
40
|
162 |
|
$connection->setName($connectionName); |
41
|
|
|
} |
42
|
|
|
|
43
|
162 |
|
return $connection; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
|
|
|
|
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
68 |
|
public function support(string $connectionName, array $parameters): bool |
50
|
|
|
{ |
51
|
68 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create the instance of the connection |
56
|
|
|
* |
57
|
|
|
* @param array $parameters |
58
|
|
|
* @param Configuration|null $config |
59
|
|
|
* @param EventManager|null $eventManager The event manager, optional. |
60
|
|
|
* |
61
|
|
|
* @return ConnectionInterface |
62
|
|
|
* @throws DBALException |
63
|
|
|
*/ |
64
|
162 |
|
private function createConnection(array $parameters, Configuration $config = null, EventManager $eventManager = null): ConnectionInterface |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
// Set the custom driver class + wrapper |
67
|
162 |
|
if (isset($parameters['driver']) && isset(self::$driversMap[$parameters['driver']])) { |
68
|
|
|
list($parameters['driverClass'], $parameters['wrapperClass']) = self::$driversMap[$parameters['driver']]; |
69
|
|
|
unset($parameters['driver']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Replace 'adapter' with 'driver' and add 'pdo_' |
73
|
162 |
|
if (isset($parameters['adapter'])) { |
74
|
133 |
|
$parameters['driver'] = 'pdo_' . $parameters['adapter']; |
75
|
133 |
|
unset($parameters['adapter']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// default charset |
|
|
|
|
79
|
162 |
|
if (!isset($parameters['charset'])) { |
80
|
159 |
|
$parameters['charset'] = 'utf8'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// default wrapper |
|
|
|
|
84
|
162 |
|
if (!isset($parameters['wrapperClass'])) { |
85
|
162 |
|
$parameters['wrapperClass'] = SimpleConnection::class; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
try { |
89
|
162 |
|
return DriverManager::getConnection($parameters, $config, $eventManager); |
|
|
|
|
90
|
|
|
} catch (DoctrineDBALException $e) { |
91
|
|
|
throw new DBALException('Cannot create the connection : '.$e->getMessage(), $e->getCode(), $e); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Register a global driver map |
97
|
|
|
* |
98
|
|
|
* @param string $name |
99
|
|
|
* @param string $driver |
100
|
|
|
* @param string|null $wrapper |
101
|
|
|
*/ |
102
|
1 |
|
public static function registerDriverMap($name, $driver, $wrapper = null) |
103
|
|
|
{ |
104
|
1 |
|
self::$driversMap[$name] = [$driver, $wrapper]; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get a global driver map |
109
|
|
|
* |
110
|
|
|
* @param string $name |
111
|
|
|
* |
112
|
|
|
* @return string|null |
113
|
|
|
*/ |
114
|
1 |
|
public static function getDriverMap($name) |
115
|
|
|
{ |
116
|
1 |
|
return isset(self::$driversMap[$name]) |
|
|
|
|
117
|
1 |
|
? self::$driversMap[$name] |
|
|
|
|
118
|
1 |
|
: null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Unregister a global driver map |
123
|
|
|
* |
124
|
|
|
* @param string $name |
125
|
|
|
*/ |
126
|
1 |
|
public static function unregisterDriverMap($name) |
127
|
|
|
{ |
128
|
1 |
|
unset(self::$driversMap[$name]); |
129
|
1 |
|
} |
130
|
|
|
} |
131
|
|
|
|
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