|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyPanel\Commands\CRUDActions; |
|
4
|
|
|
|
|
5
|
|
|
use EasyPanel\Parsers\StubParser; |
|
6
|
|
|
use Illuminate\Console\GeneratorCommand; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Illuminate\Support\Str; |
|
9
|
|
|
use EasyPanel\Contracts\CRUDComponent; |
|
10
|
|
|
|
|
11
|
|
|
abstract class CommandBase extends GeneratorCommand |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var StubParser |
|
16
|
|
|
*/ |
|
17
|
|
|
private $stubParser; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var CRUDComponent |
|
21
|
|
|
*/ |
|
22
|
|
|
private $crudInstance; |
|
23
|
|
|
|
|
24
|
|
|
protected $path; |
|
25
|
|
|
|
|
26
|
|
|
public function getDefaultNamespace($rootNamespace) |
|
27
|
|
|
{ |
|
28
|
|
|
$name = ucfirst($this->getNameInput()); |
|
29
|
|
|
$this->path = parent::getDefaultNamespace($rootNamespace).DIRECTORY_SEPARATOR."Http".DIRECTORY_SEPARATOR."Livewire".DIRECTORY_SEPARATOR."Admin".DIRECTORY_SEPARATOR."$name"; |
|
30
|
|
|
|
|
31
|
|
|
return $this->path; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function buildClass($name) |
|
35
|
|
|
{ |
|
36
|
|
|
$stub = parent::buildClass($name); |
|
37
|
|
|
$stub = $this->stubParser->replaceModel($stub); |
|
38
|
|
|
|
|
39
|
|
|
return $stub; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function getPath($name) |
|
43
|
|
|
{ |
|
44
|
|
|
$fileName = ucfirst($this->file); |
|
45
|
|
|
return "{$this->path}".DIRECTORY_SEPARATOR."{$fileName}.php"; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getStub() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->resolveStubPath("{$this->file}.stub"); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function buildBlade() |
|
54
|
|
|
{ |
|
55
|
|
|
$stub = $this->files->get($this->resolveStubPath("blade".DIRECTORY_SEPARATOR."{$this->file}.blade.stub")); |
|
56
|
|
|
$newStub = $this->stubParser->parseBlade($stub); |
|
57
|
|
|
|
|
58
|
|
|
$directoryName = strtolower($this->getNameInput()); |
|
59
|
|
|
$path = $this->viewPath("livewire".DIRECTORY_SEPARATOR."admin".DIRECTORY_SEPARATOR."{$directoryName}".DIRECTORY_SEPARATOR."{$this->file}.blade.php"); |
|
60
|
|
|
|
|
61
|
|
|
$this->makeDirectory($path); |
|
62
|
|
|
|
|
63
|
|
|
$this->files->put($path, $newStub); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function handle() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->setCRUDInstance(); |
|
69
|
|
|
$this->setStubParser(); |
|
70
|
|
|
|
|
71
|
|
|
if ($this->isReservedName($this->getNameInput())) { |
|
72
|
|
|
$this->error("The name '{$this->getNameInput()}' is reserved by PHP."); |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
|
77
|
|
|
$path = $this->getPath($name); |
|
78
|
|
|
$path = str_replace('App', 'app', $path); |
|
79
|
|
|
|
|
80
|
|
|
if ($this->alreadyExists($this->getNameInput()) and !$this->option('force')) { |
|
81
|
|
|
$this->line("<options=bold,reverse;fg=red> • {$this->getNameInput()} {$this->type} already exists! </> \n"); |
|
82
|
|
|
|
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->makeDirectory(base_path($path)); |
|
87
|
|
|
|
|
88
|
|
|
$component = $this->sortImports($this->buildClass($name)); |
|
89
|
|
|
$this->files->put(base_path($path), $component); |
|
90
|
|
|
|
|
91
|
|
|
$this->buildBlade(); |
|
92
|
|
|
$this->line("<options=bold,reverse;fg=green> {$this->getNameInput()} {$this->type} created successfully. </> 🤙\n"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function getOptions() |
|
96
|
|
|
{ |
|
97
|
|
|
return [ |
|
98
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'force mode'] |
|
99
|
|
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function setCRUDInstance(){ |
|
103
|
|
|
$modelName = $this->getNameInput(); |
|
104
|
|
|
|
|
105
|
|
|
return $this->crudInstance = getCrudConfig($modelName); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function setStubParser() |
|
109
|
|
|
{ |
|
110
|
|
|
$model = $this->crudInstance->getModel(); |
|
111
|
|
|
$parsedModel = $this->qualifyModel($model); |
|
112
|
|
|
$this->stubParser = new StubParser($this->getNameInput(), $parsedModel); |
|
113
|
|
|
$this->setDataToParser(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function resolveStubPath($stub) |
|
117
|
|
|
{ |
|
118
|
|
|
return file_exists($customPath = base_path(trim("stubs/panel/".$stub, '/'))) |
|
119
|
|
|
? $customPath |
|
120
|
|
|
: __DIR__.'/../stub/'.$stub; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function qualifyModel($model) |
|
124
|
|
|
{ |
|
125
|
|
|
if (class_exists($model)){ |
|
126
|
|
|
return $model; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$model = ltrim($model, '\\/'); |
|
130
|
|
|
|
|
131
|
|
|
$model = str_replace('/', '\\', $model); |
|
132
|
|
|
|
|
133
|
|
|
$rootNamespace = $this->rootNamespace(); |
|
134
|
|
|
|
|
135
|
|
|
if (Str::startsWith($model, $rootNamespace)) { |
|
136
|
|
|
return $model; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return is_dir(app_path('Models')) |
|
140
|
|
|
? $rootNamespace.'Models\\'.$model |
|
141
|
|
|
: $rootNamespace.$model; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function setDataToParser() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->stubParser->setAuthType($this->crudInstance->with_user_id ?? false); |
|
|
|
|
|
|
147
|
|
|
$this->stubParser->setInputs($this->crudInstance->inputs()); |
|
148
|
|
|
$this->stubParser->setFields($this->crudInstance->fields()); |
|
149
|
|
|
$this->stubParser->setStore($this->crudInstance->storePaths()); |
|
150
|
|
|
$this->stubParser->setValidationRules($this->crudInstance->validationRules()); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|