1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyPanel\Commands\Actions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\GeneratorCommand; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
|
9
|
|
|
class MakeCRUDConfig extends GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $name = 'panel:config'; |
13
|
|
|
protected $type = 'Create a config'; |
14
|
|
|
protected $description = 'Make a crud config'; |
15
|
|
|
|
16
|
|
|
protected function getStub() |
17
|
|
|
{ |
18
|
|
|
return $this->resolveStubPath('crud.stub'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function handle() |
22
|
|
|
{ |
23
|
|
|
$name = strtolower($this->getNameInput()); |
24
|
|
|
|
25
|
|
|
$path = resource_path("cruds/{$name}.php"); |
26
|
|
|
|
27
|
|
|
if($this->files->exists($path) and !$this->option('force')){ |
28
|
|
|
$this->warn("'{$name}.php' already exists in CRUDs config"); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (! $this->files->isDirectory(dirname($path))) { |
33
|
|
|
$this->files->makeDirectory(dirname($path), 0755, true); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$stub = $this->files->get($this->getStub()); |
37
|
|
|
$newStub = $this->parseStub($stub); |
38
|
|
|
|
39
|
|
|
$this->files->put($path, $newStub); |
40
|
|
|
$this->info("{$name} config file was created for {$this->getNameInput()} model\nYou can manage it in : resources/cruds/{$name}.php"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function parseStub($stub) |
44
|
|
|
{ |
45
|
|
|
$array = [ |
46
|
|
|
'{{ model }}' => $this->parseModel(), |
47
|
|
|
'{{ withAuth }}' => $this->withAuth(), |
48
|
|
|
'{{ searchFields }}' => $this->parseSearchFields(), |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
return str_replace(array_keys($array), array_values($array), $stub); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getOptions() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'force mode'], |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function parseModel() |
62
|
|
|
{ |
63
|
|
|
$model = $this->qualifyModel($this->getNameInput()); |
64
|
|
|
|
65
|
|
|
if(!class_exists($model)){ |
66
|
|
|
$this->warn("Model option should be valid and model should be exist"); |
67
|
|
|
die(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $model; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function withAuth() |
74
|
|
|
{ |
75
|
|
|
$fillableList = $this->getFillableList(); |
76
|
|
|
if(!in_array('user_id', $fillableList)){ |
77
|
|
|
return 'true'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return 'false'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getFillableList() |
84
|
|
|
{ |
85
|
|
|
$modelNamespace = $this->qualifyModel($this->getNameInput()); |
86
|
|
|
$modelInstance = new $modelNamespace; |
87
|
|
|
return $modelInstance->getFillable(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function parseSearchFields() |
91
|
|
|
{ |
92
|
|
|
$fillableList = $this->getFillableList(); |
93
|
|
|
$array = []; |
94
|
|
|
foreach ($fillableList as $fillable){ |
95
|
|
|
if(!Str::contains($fillable, 'id')){ |
96
|
|
|
$array[] = "'$fillable'"; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return implode(', ', $array); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function resolveStubPath($stub) |
104
|
|
|
{ |
105
|
|
|
return file_exists($customPath = $this->laravel->basePath(trim("stubs/panel/".$stub, '/'))) |
106
|
|
|
? $customPath |
107
|
|
|
: __DIR__.'/../stub/'.$stub; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function qualifyModel($model) |
111
|
|
|
{ |
112
|
|
|
$model = ltrim($model, '\\/'); |
113
|
|
|
|
114
|
|
|
$model = str_replace('/', '\\', $model); |
115
|
|
|
|
116
|
|
|
$rootNamespace = $this->rootNamespace(); |
117
|
|
|
|
118
|
|
|
if (Str::startsWith($model, $rootNamespace)) { |
119
|
|
|
return $model; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return is_dir(app_path('Models')) |
123
|
|
|
? $rootNamespace.'Models\\'.$model |
124
|
|
|
: $rootNamespace.$model; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.