1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Mtolhuys\LaravelSchematics\Services\RelationMapper; |
6
|
|
|
use Mtolhuys\LaravelSchematics\Services\ModelMapper; |
7
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Illuminate\Contracts\View\Factory; |
10
|
|
|
use Illuminate\Support\Facades\Schema; |
11
|
|
|
use Illuminate\Support\Facades\Cache; |
12
|
|
|
use Illuminate\Routing\Controller; |
13
|
|
|
use Illuminate\Support\Facades\DB; |
14
|
|
|
use Illuminate\View\View; |
15
|
|
|
use ReflectionException; |
16
|
|
|
|
17
|
|
|
class SchematicsController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @return Factory|View |
21
|
|
|
* @throws ReflectionException |
22
|
|
|
*/ |
23
|
|
|
public function index() |
24
|
|
|
{ |
25
|
|
|
return view('schematics::index', $this |
26
|
|
|
->modelsWithRelations(ModelMapper::map())); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return ResponseFactory|\Illuminate\Http\Response|Response |
31
|
|
|
*/ |
32
|
|
|
public function clearCache() |
33
|
|
|
{ |
34
|
|
|
Cache::forget('schematics'); |
35
|
|
|
|
36
|
|
|
return response('Cache cleared', 200); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $table |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function details($table) |
44
|
|
|
{ |
45
|
|
|
$exists = Schema::hasTable($table); |
46
|
|
|
|
47
|
|
|
return $exists ? DB::select(DB::raw("SHOW COLUMNS FROM {$table}")) : []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $models |
52
|
|
|
* @return array |
53
|
|
|
* @throws ReflectionException |
54
|
|
|
*/ |
55
|
|
|
public function modelsWithRelations(array $models = []): array |
56
|
|
|
{ |
57
|
|
|
if (Cache::has('schematics')) { |
58
|
|
|
return Cache::get('schematics'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (empty($models)) { |
62
|
|
|
$models = ModelMapper::map(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$data = [ |
66
|
|
|
'models' => $models, |
67
|
|
|
'relations' => RelationMapper::map($models), |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
Cache::put('schematics', $data, 1440); |
71
|
|
|
|
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|