ActionCommand.php
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Dcat\Admin\Console;
class ActionCommand extends GeneratorCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admin:action';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make a admin action';
/**
* @var string
*/
protected $choice;
/**
* @var string
*/
protected $className;
/**
* @var string
*/
protected $namespace;
/**
* @var array
*/
protected $namespaceMap = [
'grid-batch' => 'Grid',
'grid-row' => 'Grid',
'grid-tool' => 'Grid',
'form-tool' => 'Form',
'show-tool' => 'Show',
'tree-row' => 'Tree',
'tree-tool' => 'Tree',
];
public function handle()
{
$this->choice = $this->choice(
'Which type of action would you like to make?',
$choices = $this->actionTyps()
);
INPUT_NAME:
$this->className = ucfirst(trim($this->ask('Please enter a name of action class')));
if (! $this->className) {
goto INPUT_NAME;
}
$this->namespace = ucfirst(trim($this->ask('Please enter the namespace of action class', $this->getDefaultNamespace(null))));
$this->askBaseDirectory();
return parent::handle();
}
/**
* @return array
*/
protected function actionTyps()
{
return [
'default',
'grid-batch',
'grid-row',
'grid-tool',
'form-tool',
'show-tool',
'tree-row',
'tree-tool',
];
}
/**
* Replace the class name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceClass($stub, $name)
{
$stub = parent::replaceClass($stub, $name);
return str_replace(
[
'DummyName',
],
[
$this->className,
],
$stub
);
}
/**
* Get the stub file for the generator.
*
* @return string
*/
public function getStub()
{
return __DIR__."/stubs/actions/{$this->choice}.stub";
}
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
if ($this->namespace) {
return $this->namespace;
}
$segments = explode('\\', config('admin.route.namespace'));
array_pop($segments);
array_push($segments, 'Actions');
if (isset($this->namespaceMap[$this->choice])) {
array_push($segments, $this->namespaceMap[$this->choice]);
}
return implode('\\', $segments);
}
/**
* Get the desired class name from the input.
*
* @return string
*/
protected function getNameInput()
{
$this->type = $this->qualifyClass($this->className);
return $this->className;
}
}