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