/
ConnectToDatabase.php
61 lines (51 loc) · 1.54 KB
/
ConnectToDatabase.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
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Install\Steps;
use Flarum\Install\DatabaseConfig;
use Flarum\Install\Step;
use Illuminate\Database\Connectors\MySqlConnector;
use Illuminate\Database\MySqlConnection;
use Illuminate\Support\Str;
use RangeException;
class ConnectToDatabase implements Step
{
private $dbConfig;
private $store;
public function __construct(DatabaseConfig $dbConfig, callable $store)
{
$this->dbConfig = $dbConfig;
$this->store = $store;
}
public function getMessage()
{
return 'Connecting to database';
}
public function run()
{
$config = $this->dbConfig->toArray();
$pdo = (new MySqlConnector)->connect($config);
$version = $pdo->query('SELECT VERSION()')->fetchColumn();
if (Str::contains($version, 'MariaDB')) {
if (version_compare($version, '10.0.5', '<')) {
throw new RangeException('MariaDB version too low. You need at least MariaDB 10.0.5');
}
} else {
if (version_compare($version, '5.6.0', '<')) {
throw new RangeException('MySQL version too low. You need at least MySQL 5.6.');
}
}
($this->store)(
new MySqlConnection(
$pdo,
$config['database'],
$config['prefix'],
$config
)
);
}
}