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/ |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) 2018 nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\connect\variables; |
||
12 | |||
13 | use nystudio107\connect\Connect; |
||
14 | use nystudio107\connect\db\Query; |
||
15 | use nystudio107\connect\models\Settings; |
||
16 | |||
17 | use Craft; |
||
18 | |||
19 | use yii\db\Connection; |
||
20 | use yii\db\Exception; |
||
21 | |||
22 | /** |
||
0 ignored issues
–
show
|
|||
23 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
24 | * @package Connect |
||
0 ignored issues
–
show
|
|||
25 | * @since 1.0.0 |
||
0 ignored issues
–
show
|
|||
26 | */ |
||
0 ignored issues
–
show
|
|||
27 | class ConnectVariable |
||
28 | { |
||
29 | // Public Methods |
||
30 | // ========================================================================= |
||
31 | |||
32 | /** |
||
33 | * Open a new database connection |
||
34 | * |
||
35 | * @param string $configName |
||
0 ignored issues
–
show
|
|||
36 | * |
||
37 | * @return null|Connection |
||
38 | */ |
||
39 | public function open(string $configName) |
||
40 | { |
||
41 | $connection = null; |
||
42 | $config = $this->getDbConfig($configName); |
||
43 | if (!empty($config)) { |
||
44 | // Create a connection to the db |
||
45 | $connection = new Connection($config); |
||
46 | try { |
||
47 | $connection->open(); |
||
48 | } catch (Exception $e) { |
||
49 | Craft::error($e->getMessage(), __METHOD__); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | return $connection; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns a new generic query. |
||
58 | * |
||
59 | * @param null|Connection $connection |
||
0 ignored issues
–
show
|
|||
60 | * |
||
61 | * @return Query |
||
62 | */ |
||
63 | public function query($connection = null): Query |
||
64 | { |
||
65 | return new Query([ |
||
0 ignored issues
–
show
|
|||
66 | 'db' => $connection, |
||
67 | ]); |
||
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
![]() |
|||
68 | } |
||
69 | |||
70 | // Protected Methods |
||
71 | // ========================================================================= |
||
72 | |||
73 | /** |
||
74 | * Return a database config with the key $configName from the Settings |
||
75 | * |
||
76 | * @param string $configName |
||
0 ignored issues
–
show
|
|||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | protected function getDbConfig(string $configName): array |
||
81 | { |
||
82 | $config = []; |
||
83 | /* @var Settings $settings */ |
||
84 | $settings = Connect::$plugin->getSettings(); |
||
85 | if ($settings !== null && !empty($settings->connections[$configName])) { |
||
86 | $dbConnection = $settings->connections[$configName]; |
||
87 | $config = [ |
||
88 | 'dsn' => "{$dbConnection['driver']}:" |
||
89 | ."host={$dbConnection['server']};" |
||
90 | ."dbname={$dbConnection['database']};" |
||
91 | ."port={$dbConnection['port']};", |
||
92 | 'username' => $dbConnection['user'], |
||
93 | 'password' => $dbConnection['password'], |
||
94 | 'tablePrefix' => $dbConnection['tablePrefix'], |
||
95 | ]; |
||
96 | } |
||
97 | |||
98 | return $config; |
||
99 | } |
||
100 | } |
||
101 |