|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mtolhuys\LaravelSchematics\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
|
6
|
|
|
use Mtolhuys\LaravelSchematics\Actions\CreateFormRequestAction; |
|
7
|
|
|
use Mtolhuys\LaravelSchematics\Actions\CreateMigrationAction; |
|
8
|
|
|
use Mtolhuys\LaravelSchematics\Actions\CreateResourceControllerAction; |
|
9
|
|
|
use Mtolhuys\LaravelSchematics\Actions\DeleteMigrationAction; |
|
10
|
|
|
use Mtolhuys\LaravelSchematics\Http\Requests\CreateModelRequest; |
|
11
|
|
|
use Mtolhuys\LaravelSchematics\Http\Requests\DeleteModelRequest; |
|
12
|
|
|
use Mtolhuys\LaravelSchematics\Actions\CreateModelAction; |
|
13
|
|
|
use Mtolhuys\LaravelSchematics\Actions\DeleteModelAction; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use Illuminate\Support\Facades\Cache; |
|
16
|
|
|
use Illuminate\Routing\Controller; |
|
17
|
|
|
|
|
18
|
|
|
class ModelsController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param $request |
|
22
|
|
|
* @return ResponseFactory|\Illuminate\Http\Response|Response |
|
23
|
|
|
*/ |
|
24
|
|
|
public function create(CreateModelRequest $request) |
|
25
|
|
|
{ |
|
26
|
|
|
(new CreateModelAction())->execute($request); |
|
27
|
|
|
|
|
28
|
|
|
$this->createOptional($request); |
|
29
|
|
|
|
|
30
|
|
|
Cache::forget('schematics'); |
|
31
|
|
|
|
|
32
|
|
|
return response('Model created', 200); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param DeleteModelRequest $request |
|
37
|
|
|
* @return ResponseFactory|\Illuminate\Http\Response|Response |
|
38
|
|
|
* @throws \ReflectionException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function delete(DeleteModelRequest $request) |
|
41
|
|
|
{ |
|
42
|
|
|
(new DeleteModelAction())->execute($request); |
|
43
|
|
|
|
|
44
|
|
|
$this->deleteOptional($request); |
|
45
|
|
|
|
|
46
|
|
|
Cache::forget('schematics'); |
|
47
|
|
|
|
|
48
|
|
|
return response('Model deleted', 200); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $request |
|
53
|
|
|
*/ |
|
54
|
|
|
public function createOptional($request) |
|
55
|
|
|
{ |
|
56
|
|
|
foreach ($request['options'] as $option => $shouldUse) { |
|
57
|
|
|
if (json_decode($shouldUse, false)) { |
|
58
|
|
|
$this->getCreateAction($option)->execute([ |
|
59
|
|
|
'name' => $request['name'], |
|
60
|
|
|
'model' => config('schematics.namespace') . $request['name'], |
|
61
|
|
|
'fields' => self::getFields($request['fields']) |
|
62
|
|
|
]); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param $request |
|
69
|
|
|
*/ |
|
70
|
|
|
public function deleteOptional($request) |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($request['options'] as $option => $shouldUse) { |
|
73
|
|
|
if (json_decode($shouldUse, false)) { |
|
74
|
|
|
$this->getDeleteAction($option)->execute([ |
|
75
|
|
|
'name' => $request['name'], |
|
76
|
|
|
]); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $option |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getCreateAction($option) |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
|
|
'hasMigration' => new CreateMigrationAction, |
|
89
|
|
|
'hasFormRequest' => new CreateFormRequestAction, |
|
90
|
|
|
'hasResource' => new CreateResourceControllerAction, |
|
91
|
|
|
][$option]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $option |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
private function getDeleteAction($option) |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
101
|
|
|
'hasMigration' => new DeleteMigrationAction, |
|
102
|
|
|
][$option]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param $fields |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
private static function getFields($fields): array |
|
110
|
|
|
{ |
|
111
|
|
|
return array_merge( |
|
112
|
|
|
...array_values(array_map(static function ($field) { |
|
113
|
|
|
return [$field['name'] => self::getFieldType($field['type'])]; |
|
114
|
|
|
}, $fields)) |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param $type |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function getFieldType(string $type): string |
|
123
|
|
|
{ |
|
124
|
|
|
return $type === '' ? 'string|max:255' : $type; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|