|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of emanci/lumen-request package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) emanci <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Emanci\LumenRequest\Console; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Console\Command; |
|
15
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
16
|
|
|
use Illuminate\Support\Str; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
18
|
|
|
|
|
19
|
|
|
abstract class GeneratorCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The filesystem instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $files; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The type of class being generated. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $type; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a new controller creator command instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Filesystem $files) |
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct(); |
|
43
|
|
|
|
|
44
|
|
|
$this->files = $files; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Execute the console command. |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public function handle() |
|
53
|
|
|
{ |
|
54
|
|
|
$name = $this->qualifyClass($this->getNameInput()); |
|
55
|
|
|
|
|
56
|
|
|
$path = $this->getPath($name); |
|
57
|
|
|
|
|
58
|
|
|
// First we will check to see if the class already exists. If it does, we don't want |
|
59
|
|
|
// to create the class and overwrite the user's code. So, we will bail out so the |
|
60
|
|
|
// code is untouched. Otherwise, we will continue generating this class' files. |
|
61
|
|
|
if ($this->alreadyExists($this->getNameInput())) { |
|
62
|
|
|
$this->error($this->type.' already exists!'); |
|
63
|
|
|
|
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Next, we will generate the path to the location where this class' file should get |
|
68
|
|
|
// written. Then, we will build the class and make the proper replacements on the |
|
69
|
|
|
// stub files so that it gets the correctly formatted namespace and class name. |
|
70
|
|
|
$this->makeDirectory($path); |
|
71
|
|
|
|
|
72
|
|
|
$this->files->put($path, $this->buildClass($name)); |
|
73
|
|
|
|
|
74
|
|
|
$this->info($this->type.' created successfully.'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get the stub file for the generator. |
|
79
|
|
|
* |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract protected function getStub(); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Parse the class name and format according to the root namespace. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $name |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function qualifyClass($name) |
|
92
|
|
|
{ |
|
93
|
|
|
$name = ltrim($name, '\\/'); |
|
94
|
|
|
|
|
95
|
|
|
$rootNamespace = $this->rootNamespace(); |
|
96
|
|
|
|
|
97
|
|
|
if (Str::startsWith($name, $rootNamespace)) { |
|
98
|
|
|
return $name; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$name = str_replace('/', '\\', $name); |
|
102
|
|
|
|
|
103
|
|
|
return $this->qualifyClass( |
|
104
|
|
|
$this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the default namespace for the class. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $rootNamespace |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getDefaultNamespace($rootNamespace) |
|
116
|
|
|
{ |
|
117
|
|
|
return $rootNamespace; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Determine if the class already exists. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $rawName |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function alreadyExists($rawName) |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->files->exists($this->getPath($this->qualifyClass($rawName))); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get the destination class path. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getPath($name) |
|
140
|
|
|
{ |
|
141
|
|
|
$name = Str::replaceFirst($this->rootNamespace(), '', $name); |
|
142
|
|
|
|
|
143
|
|
|
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php'; |
|
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
|
|
|
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Replace the namespace for the given stub. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $stub |
|
180
|
|
|
* @param string $name |
|
181
|
|
|
* |
|
182
|
|
|
* @return $this |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function replaceNamespace(&$stub, $name) |
|
185
|
|
|
{ |
|
186
|
|
|
$stub = str_replace( |
|
187
|
|
|
['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel'], |
|
188
|
|
|
[$this->getNamespace($name), $this->rootNamespace(), config('auth.providers.users.model')], |
|
189
|
|
|
$stub |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Get the full namespace for a given class, without the class name. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $name |
|
199
|
|
|
* |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function getNamespace($name) |
|
203
|
|
|
{ |
|
204
|
|
|
return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\'); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Replace the class name for the given stub. |
|
209
|
|
|
* |
|
210
|
|
|
* @param string $stub |
|
211
|
|
|
* @param string $name |
|
212
|
|
|
* |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function replaceClass($stub, $name) |
|
216
|
|
|
{ |
|
217
|
|
|
$class = str_replace($this->getNamespace($name).'\\', '', $name); |
|
218
|
|
|
|
|
219
|
|
|
return str_replace('DummyClass', $class, $stub); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Get the desired class name from the input. |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
|
|
protected function getNameInput() |
|
228
|
|
|
{ |
|
229
|
|
|
return trim($this->argument('name')); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get the root namespace for the class. |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function rootNamespace() |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->laravel->getNamespace(); |
|
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Get the console command arguments. |
|
244
|
|
|
* |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
protected function getArguments() |
|
248
|
|
|
{ |
|
249
|
|
|
return [ |
|
250
|
|
|
['name', InputArgument::REQUIRED, 'The name of the class'], |
|
251
|
|
|
]; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.