1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larafast\Fastapi\Console\Contracts; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
|
9
|
|
|
abstract class GeneratorCommand extends \Illuminate\Console\GeneratorCommand |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The filesystem instance. |
13
|
|
|
* |
14
|
|
|
* @var Filesystem |
15
|
|
|
*/ |
16
|
|
|
protected $files; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The type of class being generated. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the stub file for the generator. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Execute the console command. |
34
|
|
|
* |
35
|
|
|
* @return bool|null |
36
|
|
|
*/ |
37
|
|
|
public function handle() |
38
|
|
|
{ |
39
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
40
|
|
|
$path = $this->getPath($name); |
41
|
|
|
// First we will check to see if the class already exists. If it does, we don't want |
42
|
|
|
// to create the class and overwrite the user's code. So, we will bail out so the |
43
|
|
|
// code is untouched. Otherwise, we will continue generating this class' files. |
44
|
|
|
if ((!$this->hasOption('force') || |
45
|
|
|
!$this->option('force')) && |
46
|
|
|
$this->alreadyExists($this->getNameInput()) |
47
|
|
|
) { |
48
|
|
|
$this->error($this->type . ' already exists!'); |
49
|
|
|
|
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Next, we will generate the path to the location where this class' file should get |
54
|
|
|
// written. Then, we will build the class and make the proper replacements on the |
55
|
|
|
// stub files so that it gets the correctly formatted namespace and class name. |
56
|
|
|
$this->makeDirectory($path); |
57
|
|
|
|
58
|
|
|
$this->files->put($path, $this->buildClass($name)); |
59
|
|
|
|
60
|
|
|
//$displayPath = str_replace($this->laravel->basePath(), '', $path); |
61
|
|
|
|
62
|
|
|
$this->info($this->type . ' created successfully'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Parse the class name and format according to the root namespace. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
protected function qualifyClass($name) |
73
|
|
|
{ |
74
|
|
|
$name = ltrim($name, '\\/'); |
75
|
|
|
|
76
|
|
|
$rootNamespace = $this->rootNamespace(); |
77
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
78
|
|
|
return $name; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$name = str_replace('/', '\\', $name); |
82
|
|
|
|
83
|
|
|
return $this->qualifyClass( |
84
|
|
|
$this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the root namespace for the class. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
protected function rootNamespace() |
94
|
|
|
{ |
95
|
|
|
return $this->laravel->getNamespace(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the default namespace for the class. |
100
|
|
|
* |
101
|
|
|
* @param string $rootNamespace |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getDefaultNamespace($rootNamespace) |
106
|
|
|
{ |
107
|
|
|
return $rootNamespace; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the desired class name from the input. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function getNameInput() |
116
|
|
|
{ |
117
|
|
|
return trim($this->argument('name')); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the destination class path. |
122
|
|
|
* |
123
|
|
|
* @param string $name |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function getPath($name) |
128
|
|
|
{ |
129
|
|
|
$name = Str::replaceFirst($this->rootNamespace(), '', $name); |
130
|
|
|
|
131
|
|
|
return $this->laravel['path'] . '/' . str_replace('\\', '/', $name) . '.php'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Determine if the class already exists. |
136
|
|
|
* |
137
|
|
|
* @param string $rawName |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
protected function alreadyExists($rawName) |
142
|
|
|
{ |
143
|
|
|
return $this->files->exists($this->getPath($this->qualifyClass($rawName))); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Build the directory for the class if necessary. |
148
|
|
|
* |
149
|
|
|
* @param string $path |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function makeDirectory($path) |
154
|
|
|
{ |
155
|
|
|
if (!$this->files->isDirectory(dirname($path))) { |
156
|
|
|
$this->files->makeDirectory(dirname($path), 0777, true, true); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $path; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Build the class with the given name. |
164
|
|
|
* |
165
|
|
|
* @param string $name |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
protected function buildClass($name) |
170
|
|
|
{ |
171
|
|
|
$stub = $this->files->get($this->getStub()); |
172
|
|
|
|
173
|
|
|
$class = $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); |
174
|
|
|
|
175
|
|
|
return $class; |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Replace the class name for the given stub. |
181
|
|
|
* |
182
|
|
|
* @param string $stub |
183
|
|
|
* @param string $name |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
protected function replaceClass($stub, $name) |
188
|
|
|
{ |
189
|
|
|
$class = str_replace($this->getNamespace($name) . '\\', '', $name); |
190
|
|
|
|
191
|
|
|
return str_replace('DummyClass', $class, $stub); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the full namespace for a given class, without the class name. |
196
|
|
|
* |
197
|
|
|
* @param string $name |
198
|
|
|
* |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
|
|
protected function getNamespace($name) |
202
|
|
|
{ |
203
|
|
|
return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Replace the namespace for the given stub. |
208
|
|
|
* |
209
|
|
|
* @param string $stub |
210
|
|
|
* @param string $name |
211
|
|
|
* |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
|
|
protected function replaceNamespace(&$stub, $name) |
215
|
|
|
{ |
216
|
|
|
$namespace = $this->getNamespace($name); |
217
|
|
|
|
218
|
|
|
$stub = str_replace( |
219
|
|
|
['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel'], |
220
|
|
|
[$namespace, $this->rootNamespace(), config('auth.providers.users.model')], |
221
|
|
|
$stub |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Get the console command arguments. |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
protected function getArguments() |
233
|
|
|
{ |
234
|
|
|
return [ |
235
|
|
|
['name', InputArgument::REQUIRED, 'The name of the class'], |
236
|
|
|
]; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
protected function getRelativePath($path) |
240
|
|
|
{ |
241
|
|
|
return str_replace(base_path() . '/', '', $path); |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|