Skip to content

Commit afec95b

Browse files
Merge branch '7.x'
2 parents 3767a68 + bcea891 commit afec95b

File tree

15 files changed

+196
-33
lines changed

15 files changed

+196
-33
lines changed

src/Illuminate/Auth/Access/Gate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ protected function callbackAllowsGuests($callback)
450450
*/
451451
protected function parameterAllowsGuests($parameter)
452452
{
453-
return ($parameter->getClass() && $parameter->allowsNull()) ||
453+
return ($parameter->hasType() && $parameter->allowsNull()) ||
454454
($parameter->isDefaultValueAvailable() && is_null($parameter->getDefaultValue()));
455455
}
456456

src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
99
use Illuminate\Contracts\Routing\BindingRegistrar;
1010
use Illuminate\Contracts\Routing\UrlRoutable;
11+
use Illuminate\Support\Reflector;
1112
use Illuminate\Support\Str;
1213
use ReflectionClass;
1314
use ReflectionFunction;
@@ -204,9 +205,9 @@ protected function resolveImplicitBindingIfPossible($key, $value, $callbackParam
204205
continue;
205206
}
206207

207-
$instance = $parameter->getClass()->newInstance();
208+
$className = Reflector::getParameterClassName($parameter);
208209

209-
if (! $model = $instance->resolveRouteBinding($value)) {
210+
if (is_null($model = (new $className)->resolveRouteBinding($value))) {
210211
throw new AccessDeniedHttpException;
211212
}
212213

@@ -225,8 +226,8 @@ protected function resolveImplicitBindingIfPossible($key, $value, $callbackParam
225226
*/
226227
protected function isImplicitlyBindable($key, $parameter)
227228
{
228-
return $parameter->name === $key && $parameter->getClass() &&
229-
$parameter->getClass()->isSubclassOf(UrlRoutable::class);
229+
return $parameter->getName() === $key &&
230+
Reflector::isParameterSubclassOf($parameter, UrlRoutable::class);
230231
}
231232

232233
/**

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
*

src/Illuminate/Container/BoundMethod.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,23 @@ protected static function getCallReflector($callback)
158158
protected static function addDependencyForCallParameter($container, $parameter,
159159
array &$parameters, &$dependencies)
160160
{
161-
if (array_key_exists($parameter->name, $parameters)) {
162-
$dependencies[] = $parameters[$parameter->name];
161+
if (array_key_exists($parameter->getName(), $parameters)) {
162+
$paramName = $parameter->getName();
163163

164-
unset($parameters[$parameter->name]);
165-
} elseif ($parameter->getClass() && array_key_exists($parameter->getClass()->name, $parameters)) {
166-
$dependencies[] = $parameters[$parameter->getClass()->name];
164+
$dependencies[] = $parameters[$paramName];
167165

168-
unset($parameters[$parameter->getClass()->name]);
169-
} elseif ($parameter->getClass()) {
170-
$dependencies[] = $container->make($parameter->getClass()->name);
166+
unset($parameters[$paramName]);
167+
} elseif (! is_null($className = Util::getParameterClassName($parameter))) {
168+
if (array_key_exists($className, $parameters)) {
169+
$dependencies[] = $parameters[$className];
170+
171+
unset($parameters[$className]);
172+
} else {
173+
$dependencies[] = $container->make($className);
174+
}
171175
} elseif ($parameter->isDefaultValueAvailable()) {
172176
$dependencies[] = $parameter->getDefaultValue();
173-
} elseif (! $parameter->isOptional() && ! array_key_exists($parameter->name, $parameters)) {
177+
} elseif (! $parameter->isOptional() && ! array_key_exists($paramName, $parameters)) {
174178
$message = "Unable to resolve dependency [{$parameter}] in class {$parameter->getDeclaringClass()->getName()}";
175179

176180
throw new BindingResolutionException($message);

src/Illuminate/Container/Container.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ public function build($concrete)
848848
/**
849849
* Resolve all of the dependencies from the ReflectionParameters.
850850
*
851-
* @param array $dependencies
851+
* @param \ReflectionParameter[] $dependencies
852852
* @return array
853853
*
854854
* @throws \Illuminate\Contracts\Container\BindingResolutionException
@@ -870,7 +870,7 @@ protected function resolveDependencies(array $dependencies)
870870
// If the class is null, it means the dependency is a string or some other
871871
// primitive type which we can not resolve since it is not a class and
872872
// we will just bomb out with an error since we have no-where to go.
873-
$result = is_null($dependency->getClass())
873+
$result = is_null(Util::getParameterClassName($dependency))
874874
? $this->resolvePrimitive($dependency)
875875
: $this->resolveClass($dependency);
876876

@@ -928,7 +928,7 @@ protected function getLastParameterOverride()
928928
*/
929929
protected function resolvePrimitive(ReflectionParameter $parameter)
930930
{
931-
if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
931+
if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->getName()))) {
932932
return $concrete instanceof Closure ? $concrete($this) : $concrete;
933933
}
934934

@@ -952,7 +952,7 @@ protected function resolveClass(ReflectionParameter $parameter)
952952
try {
953953
return $parameter->isVariadic()
954954
? $this->resolveVariadicClass($parameter)
955-
: $this->make($parameter->getClass()->name);
955+
: $this->make(Util::getParameterClassName($parameter));
956956
}
957957

958958
// If we can not resolve the class instance, we will check to see if the value
@@ -979,10 +979,12 @@ protected function resolveClass(ReflectionParameter $parameter)
979979
*/
980980
protected function resolveVariadicClass(ReflectionParameter $parameter)
981981
{
982-
$abstract = $this->getAlias($parameter->getClass()->name);
982+
$className = Util::getParameterClassName($parameter);
983+
984+
$abstract = $this->getAlias($className);
983985

984986
if (! is_array($concrete = $this->getContextualConcrete($abstract))) {
985-
return $this->make($parameter->getClass()->name);
987+
return $this->make($className);
986988
}
987989

988990
return array_map(function ($abstract) {

src/Illuminate/Container/Util.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ public static function unwrapIfClosure($value)
3535
{
3636
return $value instanceof Closure ? $value() : $value;
3737
}
38+
39+
/**
40+
* Get the class name of the given parameter's type, if possible.
41+
*
42+
* From Reflector::getParameterClassName() in Illuminate\Support.
43+
*
44+
* @param \ReflectionParameter $parameter
45+
* @return string|null
46+
*/
47+
public static function getParameterClassName($parameter)
48+
{
49+
$type = $parameter->getType();
50+
51+
return ($type && ! $type->isBuiltin()) ? $type->getName() : null;
52+
}
3853
}

src/Illuminate/Foundation/Console/ClosureCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
4646
$parameters = [];
4747

4848
foreach ((new ReflectionFunction($this->callback))->getParameters() as $parameter) {
49-
if (isset($inputs[$parameter->name])) {
50-
$parameters[$parameter->name] = $inputs[$parameter->name];
49+
if (isset($inputs[$parameter->getName()])) {
50+
$parameters[$parameter->getName()] = $inputs[$parameter->getName()];
5151
}
5252
}
5353

src/Illuminate/Foundation/Events/DiscoverEvents.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Foundation\Events;
44

5+
use Illuminate\Support\Reflector;
56
use Illuminate\Support\Str;
67
use ReflectionClass;
78
use ReflectionException;
@@ -58,7 +59,7 @@ protected static function getListenerEvents($listeners, $basePath)
5859
}
5960

6061
$listenerEvents[$listener->name.'@'.$method->name] =
61-
optional($method->getParameters()[0]->getClass())->name;
62+
Reflector::getParameterClassName($method->getParameters()[0]);
6263
}
6364
}
6465

src/Illuminate/Routing/ImplicitRouteBinding.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Routing\UrlRoutable;
66
use Illuminate\Database\Eloquent\ModelNotFoundException;
7+
use Illuminate\Support\Reflector;
78
use Illuminate\Support\Str;
89

910
class ImplicitRouteBinding
@@ -22,7 +23,7 @@ public static function resolveForRoute($container, $route)
2223
$parameters = $route->parameters();
2324

2425
foreach ($route->signatureParameters(UrlRoutable::class) as $parameter) {
25-
if (! $parameterName = static::getParameterName($parameter->name, $parameters)) {
26+
if (! $parameterName = static::getParameterName($parameter->getName(), $parameters)) {
2627
continue;
2728
}
2829

@@ -32,7 +33,7 @@ public static function resolveForRoute($container, $route)
3233
continue;
3334
}
3435

35-
$instance = $container->make($parameter->getClass()->name);
36+
$instance = $container->make(Reflector::getParameterClassName($parameter));
3637

3738
$parent = $route->parentOfParameter($parameterName);
3839

src/Illuminate/Routing/RouteDependencyResolverTrait.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Routing;
44

55
use Illuminate\Collections\Arr;
6+
use Illuminate\Support\Reflector;
67
use ReflectionFunctionAbstract;
78
use ReflectionMethod;
89
use ReflectionParameter;
@@ -69,15 +70,13 @@ public function resolveMethodDependencies(array $parameters, ReflectionFunctionA
6970
*/
7071
protected function transformDependency(ReflectionParameter $parameter, $parameters, $skippableValue)
7172
{
72-
$class = $parameter->getClass();
73+
$className = Reflector::getParameterClassName($parameter);
7374

7475
// If the parameter has a type-hinted class, we will check to see if it is already in
7576
// the list of parameters. If it is we will just skip it as it is probably a model
7677
// binding and we do not want to mess with those; otherwise, we resolve it here.
77-
if ($class && ! $this->alreadyInParameters($class->name, $parameters)) {
78-
return $parameter->isDefaultValueAvailable()
79-
? null
80-
: $this->container->make($class->name);
78+
if ($className && ! $this->alreadyInParameters($className, $parameters)) {
79+
return $parameter->isDefaultValueAvailable() ? null : $this->container->make($className);
8180
}
8281

8382
return $skippableValue;

0 commit comments

Comments
 (0)