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
|
|
|
|
10
|
|
|
abstract class CommandBase extends GeneratorCommand |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var StubParser |
15
|
|
|
*/ |
16
|
|
|
private $stubParser; |
17
|
|
|
|
18
|
|
|
protected $path; |
19
|
|
|
|
20
|
|
|
public function getDefaultNamespace($rootNamespace) |
21
|
|
|
{ |
22
|
|
|
$name = ucfirst($this->getNameInput()); |
23
|
|
|
$this->path = parent::getDefaultNamespace($rootNamespace).DIRECTORY_SEPARATOR."Http".DIRECTORY_SEPARATOR."Livewire".DIRECTORY_SEPARATOR."Admin".DIRECTORY_SEPARATOR."$name"; |
24
|
|
|
|
25
|
|
|
return $this->path; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function buildClass($name) |
29
|
|
|
{ |
30
|
|
|
$stub = parent::buildClass($name); |
31
|
|
|
$stub = $this->stubParser->replaceModel($stub); |
32
|
|
|
|
33
|
|
|
return $stub; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function getPath($name) |
37
|
|
|
{ |
38
|
|
|
$fileName = ucfirst($this->file); |
39
|
|
|
return "{$this->path}".DIRECTORY_SEPARATOR."{$fileName}.php"; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function getStub() |
43
|
|
|
{ |
44
|
|
|
return $this->resolveStubPath("{$this->file}.stub"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function buildBlade() |
48
|
|
|
{ |
49
|
|
|
$stub = $this->files->get($this->resolveStubPath("blade".DIRECTORY_SEPARATOR."{$this->file}.blade.stub")); |
50
|
|
|
$newStub = $this->stubParser->parseBlade($stub); |
51
|
|
|
|
52
|
|
|
$path = $this->viewPath("livewire".DIRECTORY_SEPARATOR."admin".DIRECTORY_SEPARATOR."{$this->getNameInput()}".DIRECTORY_SEPARATOR."{$this->file}.blade.php"); |
53
|
|
|
|
54
|
|
|
if (! $this->files->isDirectory(dirname($path))) { |
55
|
|
|
$this->files->makeDirectory(dirname($path), 0755, true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->files->put($path, $newStub); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function handle() |
62
|
|
|
{ |
63
|
|
|
|
64
|
|
|
$this->setStubParser(); |
65
|
|
|
|
66
|
|
|
if ($this->isReservedName($this->getNameInput())) { |
67
|
|
|
$this->error("The name '{$this->getNameInput()}' is reserved by PHP."); |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
72
|
|
|
$path = $this->getPath($name); |
73
|
|
|
$path = str_replace('App', 'app', $path); |
74
|
|
|
|
75
|
|
|
if ($this->alreadyExists($this->getNameInput()) and !$this->option('force')) { |
76
|
|
|
$this->line("<options=bold,reverse;fg=red> • {$this->getNameInput()} {$this->type} already exists! </> \n"); |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->makeDirectory($path); |
82
|
|
|
|
83
|
|
|
$this->files->put($path, $this->sortImports($this->buildClass($name))); |
84
|
|
|
|
85
|
|
|
$this->buildBlade(); |
86
|
|
|
$this->line("<options=bold,reverse;fg=green> {$this->getNameInput()} {$this->type} created successfully. </> 🤙\n"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function getOptions() |
90
|
|
|
{ |
91
|
|
|
return [ |
92
|
|
|
['force', 'f', InputOption::VALUE_NONE, 'force mode'] |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function setStubParser() |
97
|
|
|
{ |
98
|
|
|
$model = config("easy_panel.crud.{$this->getNameInput()}.model"); |
99
|
|
|
$parsedModel = $this->qualifyModel($model); |
100
|
|
|
$this->stubParser = new StubParser($this->getNameInput(), $parsedModel); |
101
|
|
|
$this->setDataToParser(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function resolveStubPath($stub) |
105
|
|
|
{ |
106
|
|
|
return file_exists($customPath = $this->laravel->basePath(trim("stubs/panel/".$stub, '/'))) |
107
|
|
|
? $customPath |
108
|
|
|
: __DIR__.'/../stub/'.$stub; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function qualifyModel($model) |
112
|
|
|
{ |
113
|
|
|
$model = ltrim($model, '\\/'); |
114
|
|
|
|
115
|
|
|
$model = str_replace('/', '\\', $model); |
116
|
|
|
|
117
|
|
|
$rootNamespace = $this->rootNamespace(); |
118
|
|
|
|
119
|
|
|
if (Str::startsWith($model, $rootNamespace)) { |
120
|
|
|
return $model; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return is_dir(app_path('Models')) |
124
|
|
|
? $rootNamespace.'Models\\'.$model |
125
|
|
|
: $rootNamespace.$model; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function setDataToParser(): void |
129
|
|
|
{ |
130
|
|
|
$config = config("easy_panel.crud." . $this->getNameInput()); |
131
|
|
|
$this->stubParser->setAuthType($config['with_auth']); |
132
|
|
|
$this->stubParser->setInputs($config['fields']); |
133
|
|
|
$this->stubParser->setFields($config['show']); |
134
|
|
|
$this->stubParser->setStore($config['store']); |
135
|
|
|
$this->stubParser->setValidationRules($config['validation']); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|