1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Mtolhuys\LaravelSchematics\Models\Migration; |
6
|
|
|
use Mtolhuys\LaravelSchematics\Services\RelationMapper; |
7
|
|
|
use Mtolhuys\LaravelSchematics\Services\ModelMapper; |
8
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Illuminate\Contracts\View\Factory; |
11
|
|
|
use Illuminate\Support\Facades\Schema; |
12
|
|
|
use Illuminate\Support\Facades\Cache; |
13
|
|
|
use Illuminate\Routing\Controller; |
14
|
|
|
use Illuminate\Support\Facades\DB; |
15
|
|
|
use Illuminate\View\View; |
16
|
|
|
use ReflectionException; |
17
|
|
|
|
18
|
|
|
class SchematicsController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return Factory|View |
22
|
|
|
* @throws ReflectionException |
23
|
|
|
*/ |
24
|
|
|
public function index() |
25
|
|
|
{ |
26
|
|
|
return view('schematics::index', $this->schematics()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return ResponseFactory|\Illuminate\Http\Response|Response |
31
|
|
|
*/ |
32
|
|
|
public function clearCache() |
33
|
|
|
{ |
34
|
|
|
Cache::forget('schematics'); |
35
|
|
|
Cache::forget('schematics-exception'); |
36
|
|
|
|
37
|
|
|
return response('Cache cleared', 200); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $table |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
public function details($table): array |
45
|
|
|
{ |
46
|
|
|
$exists = Schema::hasTable($table); |
47
|
|
|
|
48
|
|
|
return $exists ? DB::select(DB::raw("SHOW COLUMNS FROM {$table}")) : []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function migrations(): array |
55
|
|
|
{ |
56
|
|
|
$redundant = []; |
57
|
|
|
$ran = Migration::all(); |
58
|
|
|
$created = glob(base_path('database/migrations').'/*.php'); |
59
|
|
|
|
60
|
|
|
foreach($ran as $migration) { |
61
|
|
|
if (! in_array(base_path("database/migrations/$migration->migration.php"), $created, true)) { |
62
|
|
|
$redundant[] = $migration; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return [ |
67
|
|
|
'redundant' => count($redundant), |
68
|
|
|
'created' => count($created), |
69
|
|
|
'run' => count($ran), |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
* @throws ReflectionException |
76
|
|
|
*/ |
77
|
|
|
public function refresh(): array |
78
|
|
|
{ |
79
|
|
|
return $this->schematics(false); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param bool $cache |
84
|
|
|
* @return array |
85
|
|
|
* @throws ReflectionException |
86
|
|
|
*/ |
87
|
|
|
public function schematics($cache = true): array |
88
|
|
|
{ |
89
|
|
|
if ($cache && Cache::has('schematics')) { |
90
|
|
|
return Cache::get('schematics'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$models = ModelMapper::map(); |
94
|
|
|
|
95
|
|
|
$data = [ |
96
|
|
|
'models' => $models, |
97
|
|
|
'relations' => RelationMapper::map($models), |
98
|
|
|
'migrations' => $this->migrations(), |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
Cache::put('schematics', $data, 1440); |
102
|
|
|
|
103
|
|
|
return $data; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|