|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Adminerng\Drivers\MySql; |
|
4
|
|
|
|
|
5
|
|
|
use Adminerng\Core\Column; |
|
6
|
|
|
use Adminerng\Core\ListingHeaders\HeaderManagerInterface; |
|
7
|
|
|
|
|
8
|
|
|
class MySqlHeaderManager implements HeaderManagerInterface |
|
9
|
|
|
{ |
|
10
|
|
|
private $dataManager; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(MySqlDataManager $dataManager) |
|
13
|
|
|
{ |
|
14
|
|
|
$this->dataManager = $dataManager; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function databasesHeaders() |
|
18
|
|
|
{ |
|
19
|
|
|
$columns = []; |
|
20
|
|
|
$columns[] = (new Column('database', 'mysql.headers.databases.database')) |
|
21
|
|
|
->setIsSortable(true); |
|
22
|
|
|
|
|
23
|
|
|
$columns[] = (new Column('charset', 'mysql.headers.databases.charset')) |
|
24
|
|
|
->setIsSortable(true); |
|
25
|
|
|
|
|
26
|
|
|
$columns[] = (new Column('collation', 'mysql.headers.databases.collation')) |
|
27
|
|
|
->setIsSortable(true); |
|
28
|
|
|
|
|
29
|
|
|
$columns[] = (new Column('tables_count', 'mysql.headers.databases.tables')) |
|
30
|
|
|
->setIsSortable(true) |
|
31
|
|
|
->setIsNumeric(true); |
|
32
|
|
|
|
|
33
|
|
|
$columns[] = (new Column('size', 'mysql.headers.databases.size')) |
|
34
|
|
|
->setIsSortable(true) |
|
35
|
|
|
->setIsNumeric(true) |
|
36
|
|
|
->setIsSize(true); |
|
37
|
|
|
return $columns; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function tablesHeaders() |
|
41
|
|
|
{ |
|
42
|
|
|
$tableFields = [ |
|
43
|
|
|
'table' => [], |
|
44
|
|
|
'engine' => [], |
|
45
|
|
|
'collation' => [], |
|
46
|
|
|
'data_length' => ['is_numeric' => true, 'is_size' => true], |
|
47
|
|
|
'index_length' => ['is_numeric' => true, 'is_size' => true], |
|
48
|
|
|
'data_free' => ['is_numeric' => true, 'is_size' => true], |
|
49
|
|
|
'autoincrement' => ['is_numeric' => true], |
|
50
|
|
|
'rows' => ['is_numeric' => true], |
|
51
|
|
|
]; |
|
52
|
|
|
$tableColumns = []; |
|
53
|
|
|
foreach ($tableFields as $key => $settings) { |
|
54
|
|
|
$column = (new Column($key, 'mysql.headers.tables.' . $key)) |
|
55
|
|
|
->setIsSortable(true); |
|
56
|
|
|
if (isset($settings['is_numeric'])) { |
|
57
|
|
|
$column->setIsNumeric($settings['is_numeric']); |
|
58
|
|
|
} |
|
59
|
|
|
if (isset($settings['is_size'])) { |
|
60
|
|
|
$column->setIsSize($settings['is_size']); |
|
61
|
|
|
} |
|
62
|
|
|
$tableColumns[] = $column; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$viewFields = [ |
|
66
|
|
|
'view' => [], |
|
67
|
|
|
'check_option' => [], |
|
68
|
|
|
'is_updatable' => [], |
|
69
|
|
|
'definer' => [], |
|
70
|
|
|
'security_type' => [], |
|
71
|
|
|
'character_set' => [], |
|
72
|
|
|
'collation' => [], |
|
73
|
|
|
]; |
|
74
|
|
|
$viewColumns = []; |
|
75
|
|
|
foreach ($viewFields as $key => $settings) { |
|
76
|
|
|
$column = (new Column($key, 'mysql.headers.views.' . $key)) |
|
77
|
|
|
->setIsSortable(true); |
|
78
|
|
|
if (isset($settings['is_numeric'])) { |
|
79
|
|
|
$column->setIsNumeric($settings['is_numeric']); |
|
80
|
|
|
} |
|
81
|
|
|
$viewColumns[] = $column; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return [ |
|
|
|
|
|
|
85
|
|
|
MySqlDriver::TYPE_TABLE => $tableColumns, |
|
86
|
|
|
MySqlDriver::TYPE_VIEW => $viewColumns, |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function itemsHeaders($type, $table) |
|
91
|
|
|
{ |
|
92
|
|
|
$columns = []; |
|
93
|
|
|
foreach ($this->dataManager->getColumns($type, $table) as $column => $definition) { |
|
94
|
|
|
$col = (new Column($column, $column)) |
|
95
|
|
|
->setIsSortable(true) |
|
96
|
|
|
->setIsFilterable(true) |
|
97
|
|
|
->setIsNumeric($this->isNumeric($definition)) |
|
98
|
|
|
->setDecimals($this->getDecimals($definition)) |
|
99
|
|
|
->setInfo($definition['Comment']); |
|
100
|
|
|
if ($definition['key_info'] && $definition['key_info']['REFERENCED_TABLE_NAME']) { |
|
101
|
|
|
$col->setExternal( |
|
102
|
|
|
$definition['key_info']['REFERENCED_TABLE_SCHEMA'], |
|
103
|
|
|
$definition['key_info']['REFERENCED_TABLE_NAME'], |
|
104
|
|
|
function ($value) { |
|
105
|
|
|
return md5($value); |
|
106
|
|
|
} |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
$columns[] = $col; |
|
110
|
|
|
} |
|
111
|
|
|
return $columns; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function isNumeric(array $definition) |
|
115
|
|
|
{ |
|
116
|
|
|
if ($definition['key_info']) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
if (strpos($definition['Type'], 'int') !== false) { |
|
120
|
|
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
if (strpos($definition['Type'], 'float') !== false) { |
|
123
|
|
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
if (strpos($definition['Type'], 'double') !== false) { |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
if (strpos($definition['Type'], 'decimal') !== false) { |
|
129
|
|
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
return false; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function getDecimals(array $column) |
|
135
|
|
|
{ |
|
136
|
|
|
if (!$this->isNumeric($column)) { |
|
137
|
|
|
return 0; |
|
138
|
|
|
} |
|
139
|
|
|
if (strpos($column['Type'], ',') === false) { |
|
140
|
|
|
return 0; |
|
141
|
|
|
} |
|
142
|
|
|
$pattern = '/(.*?)\((.*?),(.*?)\)/'; |
|
143
|
|
|
preg_match($pattern, $column['Type'], $match); |
|
144
|
|
|
return isset($match[3]) ? $match[3] : 0; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.