-
Notifications
You must be signed in to change notification settings - Fork 65
/
ConnectionFactory.php
74 lines (62 loc) · 2.21 KB
/
ConnectionFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Liuggio\Fastest\Doctrine\DBAL;
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory as BaseConnectionFactory;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration;
use Liuggio\Fastest\Process\EnvCommandCreator;
/**
* Creates a connection taking the db name from the env, this is great if you want to run parallel functional tests.
*/
class ConnectionFactory extends BaseConnectionFactory
{
/**
* Create a connection by name.
*
* @param array $params
* @param Configuration $config
* @param EventManager $eventManager
* @param array $mappingTypes
*
* @return \Doctrine\DBAL\Connection
*/
public function createConnection(array $params, Configuration $config = null, EventManager $eventManager = null, array $mappingTypes = [])
{
if (isset($params['dbname'])) {
$dbName = $this->getDbNameFromEnv($params['dbname']);
} else {
$dbName = $this->getDbNameFromEnv($params['master']['dbname']);
}
if ('pdo_sqlite' === $params['driver']) {
if (isset($params['path'])) {
$params['path'] = str_replace('__DBNAME__', $dbName, $params['path']);
}
if (isset($params['master']['path'])) {
$params['master']['path'] = str_replace('__DBNAME__', $dbName, $params['master']['path']);
}
if (!empty($params['slaves'])) {
foreach ($params['slaves'] as &$slave) {
$slave['path'] = str_replace('__DBNAME__', $dbName, $slave['path']);
}
}
} else {
$params['dbname'] = $dbName;
}
return parent::createConnection($params, $config, $eventManager, $mappingTypes);
}
protected function getDbNameFromEnv($dbName)
{
if ($this->issetDbNameEnvValue()) {
return $dbName.'_'.$this->getDbNameEnvValue();
}
return $dbName;
}
protected function issetDbNameEnvValue()
{
$dbName = $this->getDbNameEnvValue();
return !empty($dbName);
}
protected function getDbNameEnvValue()
{
return getenv(EnvCommandCreator::ENV_TEST_CHANNEL_READABLE);
}
}