@@ -22,6 +22,82 @@ 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 ' ,
32
+ 'abstract ' ,
33
+ 'and ' ,
34
+ 'array ' ,
35
+ 'as ' ,
36
+ 'break ' ,
37
+ 'callable ' ,
38
+ 'case ' ,
39
+ 'catch ' ,
40
+ 'class ' ,
41
+ 'clone ' ,
42
+ 'const ' ,
43
+ 'continue ' ,
44
+ 'declare ' ,
45
+ 'default ' ,
46
+ 'die ' ,
47
+ 'do ' ,
48
+ 'echo ' ,
49
+ 'else ' ,
50
+ 'elseif ' ,
51
+ 'empty ' ,
52
+ 'enddeclare ' ,
53
+ 'endfor ' ,
54
+ 'endforeach ' ,
55
+ 'endif ' ,
56
+ 'endswitch ' ,
57
+ 'endwhile ' ,
58
+ 'eval ' ,
59
+ 'exit ' ,
60
+ 'extends ' ,
61
+ 'final ' ,
62
+ 'finally ' ,
63
+ 'fn ' ,
64
+ 'for ' ,
65
+ 'foreach ' ,
66
+ 'function ' ,
67
+ 'global ' ,
68
+ 'goto ' ,
69
+ 'if ' ,
70
+ 'implements ' ,
71
+ 'include ' ,
72
+ 'include_once ' ,
73
+ 'instanceof ' ,
74
+ 'insteadof ' ,
75
+ 'interface ' ,
76
+ 'isset ' ,
77
+ 'list ' ,
78
+ 'namespace ' ,
79
+ 'new ' ,
80
+ 'or ' ,
81
+ 'print ' ,
82
+ 'private ' ,
83
+ 'protected ' ,
84
+ 'public ' ,
85
+ 'require ' ,
86
+ 'require_once ' ,
87
+ 'return ' ,
88
+ 'static ' ,
89
+ 'switch ' ,
90
+ 'throw ' ,
91
+ 'trait ' ,
92
+ 'try ' ,
93
+ 'unset ' ,
94
+ 'use ' ,
95
+ 'var ' ,
96
+ 'while ' ,
97
+ 'xor ' ,
98
+ 'yield ' ,
99
+ ];
100
+
25
101
/**
26
102
* Create a new controller creator command instance.
27
103
*
@@ -51,11 +127,20 @@ abstract protected function getStub();
51
127
*/
52
128
public function handle ()
53
129
{
130
+ // First we need to ensure that the given name is not a reserved word within the PHP
131
+ // language and that the class name will actually be valid. If it is not valid we
132
+ // can error now and prevent from polluting the filesystem using invalid files.
133
+ if ($ this ->isReservedName ($ this ->getNameInput ())) {
134
+ $ this ->error ('The name " ' .$ this ->getNameInput ().'" is reserved by PHP. ' );
135
+
136
+ return false ;
137
+ }
138
+
54
139
$ name = $ this ->qualifyClass ($ this ->getNameInput ());
55
140
56
141
$ path = $ this ->getPath ($ name );
57
142
58
- // First we will check to see if the class already exists. If it does, we don't want
143
+ // Next, We will check to see if the class already exists. If it does, we don't want
59
144
// to create the class and overwrite the user's code. So, we will bail out so the
60
145
// code is untouched. Otherwise, we will continue generating this class' files.
61
146
if ((! $ this ->hasOption ('force ' ) ||
@@ -268,6 +353,19 @@ protected function userProviderModel()
268
353
return $ config ->get ("auth.providers. {$ provider }.model " );
269
354
}
270
355
356
+ /**
357
+ * Checks whether the given name is reserved.
358
+ *
359
+ * @param string $name
360
+ * @return bool
361
+ */
362
+ protected function isReservedName ($ name )
363
+ {
364
+ $ name = strtolower ($ name );
365
+
366
+ return in_array ($ name , $ this ->reservedNames );
367
+ }
368
+
271
369
/**
272
370
* Get the console command arguments.
273
371
*
0 commit comments