1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Database; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Support\LazyCollection; |
7
|
|
|
|
8
|
|
|
final class DatabaseSchema |
9
|
|
|
{ |
10
|
|
|
private static $schema; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Return the schema for the table. |
14
|
|
|
* |
15
|
|
|
* @param string $connection |
16
|
|
|
* @param string $table |
17
|
|
|
*/ |
18
|
|
|
public static function getForTable(string $connection, string $table) |
19
|
|
|
{ |
20
|
|
|
self::generateDatabaseSchema($connection, $table); |
21
|
|
|
|
22
|
|
|
return self::$schema[$connection][$table] ?? null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generates and store the database schema. |
27
|
|
|
* |
28
|
|
|
* @param string $connection |
29
|
|
|
* @param string $table |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
private static function generateDatabaseSchema(string $connection, string $table) |
33
|
|
|
{ |
34
|
|
|
if (! isset(self::$schema[$connection]) || ! isset(self::$schema[$connection][$table])) { |
35
|
|
|
self::$schema[$connection] = self::mapTables($connection); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Map the tables from raw db values into an usable array. |
41
|
|
|
* |
42
|
|
|
* @param string $connection |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
private static function mapTables(string $connection) |
46
|
|
|
{ |
47
|
|
|
return LazyCollection::make(self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) { |
|
|
|
|
48
|
|
|
$tableName = is_array($table) ? $table['name'] : $table->getName(); |
49
|
|
|
|
50
|
|
|
if (self::$schema[$connection][$tableName] ?? false) { |
51
|
|
|
return [$tableName => self::$schema[$connection][$tableName]]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (is_array($table)) { |
55
|
|
|
$table = new Table(self::mapTableColumns($connection, $tableName)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return [$tableName => $table]; |
59
|
|
|
})->toArray(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private static function getIndexColumnNames($connection, $table) |
63
|
|
|
{ |
64
|
|
|
$schemaManager = self::getSchemaManager($connection); |
65
|
|
|
$indexes = method_exists($schemaManager, 'listTableIndexes') ? $schemaManager->listTableIndexes($table) : $schemaManager->getIndexes($table); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$indexes = array_map(function ($index) { |
68
|
|
|
return is_array($index) ? $index['columns'] : $index->getColumns(); |
69
|
|
|
}, $indexes); |
70
|
|
|
|
71
|
|
|
$indexes = \Illuminate\Support\Arr::flatten($indexes); |
72
|
|
|
|
73
|
|
|
return array_unique($indexes); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private static function mapTableColumns($connection, $table) |
77
|
|
|
{ |
78
|
|
|
$indexedColumns = self::getIndexColumnNames($connection, $table); |
79
|
|
|
|
80
|
|
|
return LazyCollection::make(self::getSchemaManager($connection)->getColumns($table))->mapWithKeys(function ($column, $key) use ($indexedColumns) { |
|
|
|
|
81
|
|
|
$column['index'] = array_key_exists($column['name'], $indexedColumns) ? true : false; |
82
|
|
|
|
83
|
|
|
return [$column['name'] => $column]; |
84
|
|
|
})->toArray(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private static function getCreateSchema(string $connection) |
88
|
|
|
{ |
89
|
|
|
$schemaManager = self::getSchemaManager($connection); |
90
|
|
|
|
91
|
|
|
return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager; |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private static function getSchemaManager(string $connection) |
95
|
|
|
{ |
96
|
|
|
$connection = DB::connection($connection); |
97
|
|
|
|
98
|
|
|
return method_exists($connection, 'getDoctrineSchemaManager') ? $connection->getDoctrineSchemaManager() : $connection->getSchemaBuilder(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function listTableColumnsNames(string $connection, string $table) |
102
|
|
|
{ |
103
|
|
|
$table = self::getForTable($connection, $table); |
104
|
|
|
|
105
|
|
|
return array_keys($table->getColumns()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function listTableIndexes(string $connection, string $table) |
109
|
|
|
{ |
110
|
|
|
return self::getIndexColumnNames($connection, $table); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.