1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Connect plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Allows you to connect to external databases and perform db queries |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2018 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\connect\variables; |
12
|
|
|
|
13
|
|
|
use nystudio107\connect\Connect; |
14
|
|
|
use nystudio107\connect\db\Query; |
15
|
|
|
use nystudio107\connect\db\Schema; |
16
|
|
|
use nystudio107\connect\models\Settings; |
17
|
|
|
|
18
|
|
|
use Craft; |
19
|
|
|
|
20
|
|
|
use yii\db\Connection; |
21
|
|
|
use yii\db\Exception; |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author nystudio107 |
|
|
|
|
25
|
|
|
* @package Connect |
|
|
|
|
26
|
|
|
* @since 1.0.0 |
|
|
|
|
27
|
|
|
*/ |
|
|
|
|
28
|
|
|
class ConnectVariable |
29
|
|
|
{ |
30
|
|
|
// Public Methods |
31
|
|
|
// ========================================================================= |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Open a new database connection |
35
|
|
|
* |
36
|
|
|
* @param string $configName |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* @return null|Connection |
39
|
|
|
*/ |
40
|
|
|
public function open(string $configName) |
41
|
|
|
{ |
42
|
|
|
$connection = null; |
43
|
|
|
$config = $this->getDbConfig($configName); |
44
|
|
|
if (!empty($config)) { |
45
|
|
|
// Create a connection to the db |
46
|
|
|
$connection = new Connection($config); |
47
|
|
|
try { |
48
|
|
|
$connection->open(); |
49
|
|
|
} catch (Exception $e) { |
50
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $connection; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns a new generic query. |
59
|
|
|
* |
60
|
|
|
* @param null|Connection $connection |
|
|
|
|
61
|
|
|
* |
62
|
|
|
* @return Query |
63
|
|
|
*/ |
64
|
|
|
public function query($connection = null): Query |
65
|
|
|
{ |
66
|
|
|
return new Query([ |
|
|
|
|
67
|
|
|
'db' => $connection, |
68
|
|
|
]); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns a new generic schema. |
73
|
|
|
* |
74
|
|
|
* @param null|Connection $connection |
|
|
|
|
75
|
|
|
* |
76
|
|
|
* @return Schema |
77
|
|
|
*/ |
78
|
|
|
public function schema($connection = null): Schema |
79
|
|
|
{ |
80
|
|
|
return new Schema([ |
|
|
|
|
81
|
|
|
'db' => $connection, |
82
|
|
|
]); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Protected Methods |
86
|
|
|
// ========================================================================= |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return a database config with the key $configName from the Settings |
90
|
|
|
* |
91
|
|
|
* @param string $configName |
|
|
|
|
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function getDbConfig(string $configName): array |
96
|
|
|
{ |
97
|
|
|
$config = []; |
98
|
|
|
/* @var Settings $settings */ |
99
|
|
|
$settings = Connect::$plugin->getSettings(); |
100
|
|
|
if ($settings !== null && !empty($settings->connections[$configName])) { |
101
|
|
|
$dbConnection = $settings->connections[$configName]; |
102
|
|
|
$config = [ |
103
|
|
|
'dsn' => "{$dbConnection['driver']}:" |
104
|
|
|
."host={$dbConnection['server']};" |
105
|
|
|
."dbname={$dbConnection['database']};" |
106
|
|
|
."port={$dbConnection['port']};", |
107
|
|
|
'username' => $dbConnection['user'], |
108
|
|
|
'password' => $dbConnection['password'], |
109
|
|
|
'tablePrefix' => $dbConnection['tablePrefix'], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $config; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|