|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\MySql; |
|
4
|
|
|
|
|
5
|
|
|
use UniMan\Core\Driver\AbstractDriver; |
|
6
|
|
|
use UniMan\Core\Exception\ConnectException; |
|
7
|
|
|
use UniMan\Drivers\MySql\Forms\MySqlCredentialsForm; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
use PDOException; |
|
10
|
|
|
|
|
11
|
|
|
class MySqlDriver extends AbstractDriver |
|
12
|
|
|
{ |
|
13
|
|
|
const TYPE_TABLE = 'table'; |
|
14
|
|
|
const TYPE_VIEW = 'view'; |
|
15
|
|
|
|
|
16
|
|
|
private $connection; |
|
17
|
|
|
|
|
18
|
2 |
|
public function extensions() |
|
19
|
|
|
{ |
|
20
|
2 |
|
return ['pdo_mysql']; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
2 |
|
public function type() |
|
24
|
|
|
{ |
|
25
|
2 |
|
return 'mysql'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
public function defaultCredentials() |
|
29
|
|
|
{ |
|
30
|
|
|
return [ |
|
31
|
2 |
|
'server' => 'localhost:3306', |
|
32
|
1 |
|
'user' => 'root', |
|
33
|
1 |
|
'password' => '', |
|
34
|
1 |
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
public function getCredentialsForm() |
|
38
|
|
|
{ |
|
39
|
2 |
|
return new MySqlCredentialsForm(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function connect(array $credentials) |
|
43
|
|
|
{ |
|
44
|
2 |
|
$host = $credentials['server']; |
|
45
|
2 |
|
$port = '3306'; |
|
46
|
2 |
|
if (strpos($credentials['server'], ':') !== false) { |
|
47
|
2 |
|
list($host, $port) = explode(':', $credentials['server'], 2); |
|
48
|
1 |
|
} |
|
49
|
2 |
|
$dsn = 'mysql:;host=' . $host . ';port=' . $port . ';charset=utf8'; |
|
50
|
|
|
try { |
|
51
|
2 |
|
$this->connection = new PDO($dsn, $credentials['user'], $credentials['password']); |
|
52
|
2 |
|
$this->connection->query("SET SESSION sql_mode = ''"); |
|
53
|
1 |
|
} catch (PDOException $e) { |
|
54
|
|
|
throw new ConnectException($e->getMessage()); |
|
55
|
|
|
} |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
protected function getFormManager() |
|
59
|
|
|
{ |
|
60
|
2 |
|
return new MySqlFormManager($this->connection, $this->dataManager()); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
protected function getHeaderManager() |
|
64
|
|
|
{ |
|
65
|
2 |
|
return new MySqlHeaderManager($this->dataManager()); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
protected function getPermissions() |
|
69
|
|
|
{ |
|
70
|
2 |
|
return new MySqlPermissions(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
protected function getDataManager() |
|
74
|
|
|
{ |
|
75
|
2 |
|
return new MySqlDataManager($this->connection); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.