Skip to content

Commit bcea891

Browse files
Merge branch '7.x' of github.com:laravel/framework into 7.x
2 parents 6018c1d + 24425c6 commit bcea891

File tree

1 file changed

+99
-1
lines changed

1 file changed

+99
-1
lines changed

src/Illuminate/Console/GeneratorCommand.php

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,82 @@ abstract class GeneratorCommand extends Command
2222
*/
2323
protected $type;
2424

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+
25101
/**
26102
* Create a new controller creator command instance.
27103
*
@@ -51,11 +127,20 @@ abstract protected function getStub();
51127
*/
52128
public function handle()
53129
{
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+
54139
$name = $this->qualifyClass($this->getNameInput());
55140

56141
$path = $this->getPath($name);
57142

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
59144
// to create the class and overwrite the user's code. So, we will bail out so the
60145
// code is untouched. Otherwise, we will continue generating this class' files.
61146
if ((! $this->hasOption('force') ||
@@ -268,6 +353,19 @@ protected function userProviderModel()
268353
return $config->get("auth.providers.{$provider}.model");
269354
}
270355

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+
271369
/**
272370
* Get the console command arguments.
273371
*

0 commit comments

Comments
 (0)