/
ConnectTrait.php
83 lines (69 loc) · 2.4 KB
/
ConnectTrait.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
75
76
77
78
79
80
81
82
83
<?php
/**
* @file
* Contains Drupal\Console\Command\Shared\ConnectTrait.
*/
namespace Drupal\Console\Command\Shared;
use Drupal\Core\Database\Database;
trait ConnectTrait
{
protected $supportedDrivers = ['mysql', 'pgsql', 'sqlite'];
public function resolveConnection($key = 'default', $target = 'default')
{
$connectionInfo = Database::getConnectionInfo($key);
if (empty($connectionInfo[$target])) {
throw new \Exception(sprintf(
$this->trans('commands.database.connect.messages.database-not-found'),
$key,
$target
));
}
else if (!in_array($connectionInfo[$target]['driver'], $this->supportedDrivers)) {
throw new \Exception(sprintf(
$this->trans('commands.database.connect.messages.database-not-supported'),
$connectionInfo[$target]['driver']
));
}
return $connectionInfo[$target];
}
public function getRedBeanConnection($database = 'default')
{
$connectionInfo = Database::getConnectionInfo();
$databaseConnection = $connectionInfo[$database];
if ($databaseConnection['driver'] == 'mysql') {
$dsn = sprintf(
'mysql:host=%s;dbname=%s',
$databaseConnection['host'],
$databaseConnection['database']
);
$this->redBean->setup(
$dsn,
$databaseConnection['username'],
$databaseConnection['password'],
true
);
return $this->redBean;
}
return null;
}
public function getConnectionString($databaseConnection) {
return sprintf(
'%s -A --database=%s --user=%s --password=%s --host=%s --port=%s',
$databaseConnection['driver'],
$databaseConnection['database'],
$databaseConnection['username'],
$databaseConnection['password'],
$databaseConnection['host'],
$databaseConnection['port']
);
}
public function escapeConnection($databaseConnection) {
$settings = [
'driver', 'database', 'username', 'password', 'host', 'port'
];
foreach ($settings as $setting) {
$databaseConnection[$setting] = escapeshellcmd($databaseConnection[$setting]);
}
return $databaseConnection;
}
}