@@ -22,6 +22,24 @@ abstract class GeneratorCommand extends Command
22
22
*/
23
23
protected $ type ;
24
24
25
+ /**
26
+ * Reserved names that cannot be used for generation.
27
+ *
28
+ * @var array
29
+ */
30
+ protected $ reservedNames = [
31
+ '__halt_compiler ' , 'break ' , 'clone ' , 'die ' , 'empty ' , 'endswitch ' , 'final ' ,
32
+ 'function ' , 'include ' , 'isset ' , 'print ' , 'require_once ' , 'trait ' , 'while ' ,
33
+ 'abstract ' , 'callable ' , 'const ' , 'do ' , 'enddeclare ' , 'endwhile ' , 'finally ' ,
34
+ 'global ' , 'include_once ' , 'list ' , 'private ' , 'return ' , 'try ' , 'xor ' , 'and ' ,
35
+ 'case ' , 'continue ' , 'echo ' , 'endfor ' , 'eval ' , 'fn ' , 'goto ' , 'instanceof ' ,
36
+ 'namespace ' , 'protected ' , 'static ' , 'unset ' , 'yield ' , 'array ' , 'catch ' ,
37
+ 'declare ' , 'else ' , 'endforeach ' , 'exit ' , 'for ' , 'if ' , 'insteadof ' , 'new ' ,
38
+ 'public ' , 'switch ' , 'use ' , 'as ' , 'class ' , 'default ' , 'elseif ' , 'endif ' ,
39
+ 'extends ' , 'foreach ' , 'implements ' , 'interface ' , 'or ' , 'require ' , 'throw ' ,
40
+ 'var ' ,
41
+ ];
42
+
25
43
/**
26
44
* Create a new controller creator command instance.
27
45
*
@@ -51,11 +69,20 @@ abstract protected function getStub();
51
69
*/
52
70
public function handle ()
53
71
{
72
+ // First we will check whether the name can be found in the reserved names.
73
+ // We have so called "reserved names" to ensure that no files are generated
74
+ // using PHP keywords for example, because that would cause errors.
75
+ if ($ this ->isReservedName ($ this ->getNameInput ())) {
76
+ $ this ->error ('The name " ' .$ this ->getNameInput ().'" is reserved. Please pick something else. ' );
77
+
78
+ return false ;
79
+ }
80
+
54
81
$ name = $ this ->qualifyClass ($ this ->getNameInput ());
55
82
56
83
$ path = $ this ->getPath ($ name );
57
84
58
- // First we will check to see if the class already exists. If it does, we don't want
85
+ // We will check to see if the class already exists. If it does, we don't want
59
86
// to create the class and overwrite the user's code. So, we will bail out so the
60
87
// code is untouched. Otherwise, we will continue generating this class' files.
61
88
if ((! $ this ->hasOption ('force ' ) ||
@@ -279,4 +306,17 @@ protected function getArguments()
279
306
['name ' , InputArgument::REQUIRED , 'The name of the class ' ],
280
307
];
281
308
}
309
+
310
+ /**
311
+ * Checks whether the given name is reserved.
312
+ *
313
+ * @param string $name
314
+ * @return bool
315
+ */
316
+ protected function isReservedName ($ name )
317
+ {
318
+ $ name = strtolower ($ name );
319
+
320
+ return in_array ($ name , $ this ->reservedNames );
321
+ }
282
322
}
0 commit comments