Skip to content

Commit 1af0632

Browse files
[6.x] Support PHP 8's reflection API (#33039)
* Support PHP 8's reflection API * Fixes
1 parent 744d01d commit 1af0632

File tree

11 files changed

+87
-26
lines changed

11 files changed

+87
-26
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\Routing\BindingRegistrar;
99
use Illuminate\Contracts\Routing\UrlRoutable;
1010
use Illuminate\Support\Arr;
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/Container/BoundMethod.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,20 @@ protected static function getCallReflector($callback)
157157
protected static function addDependencyForCallParameter($container, $parameter,
158158
array &$parameters, &$dependencies)
159159
{
160-
if (array_key_exists($parameter->name, $parameters)) {
161-
$dependencies[] = $parameters[$parameter->name];
160+
if (array_key_exists($parameter->getName(), $parameters)) {
161+
$paramName = $parameter->getName();
162162

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

167-
unset($parameters[$parameter->getClass()->name]);
168-
} elseif ($parameter->getClass()) {
169-
$dependencies[] = $container->make($parameter->getClass()->name);
165+
unset($parameters[$paramName]);
166+
} elseif (! is_null($className = Util::getParameterClassName($parameter))) {
167+
if (array_key_exists($className, $parameters)) {
168+
$dependencies[] = $parameters[$className];
169+
170+
unset($parameters[$className]);
171+
} else {
172+
$dependencies[] = $container->make($className);
173+
}
170174
} elseif ($parameter->isDefaultValueAvailable()) {
171175
$dependencies[] = $parameter->getDefaultValue();
172176
}

src/Illuminate/Container/Container.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ public function build($concrete)
846846
/**
847847
* Resolve all of the dependencies from the ReflectionParameters.
848848
*
849-
* @param array $dependencies
849+
* @param \ReflectionParameter[] $dependencies
850850
* @return array
851851
*
852852
* @throws \Illuminate\Contracts\Container\BindingResolutionException
@@ -868,7 +868,7 @@ protected function resolveDependencies(array $dependencies)
868868
// If the class is null, it means the dependency is a string or some other
869869
// primitive type which we can not resolve since it is not a class and
870870
// we will just bomb out with an error since we have no-where to go.
871-
$results[] = is_null($dependency->getClass())
871+
$results[] = is_null(Util::getParameterClassName($dependency))
872872
? $this->resolvePrimitive($dependency)
873873
: $this->resolveClass($dependency);
874874
}
@@ -920,7 +920,7 @@ protected function getLastParameterOverride()
920920
*/
921921
protected function resolvePrimitive(ReflectionParameter $parameter)
922922
{
923-
if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
923+
if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->getName()))) {
924924
return $concrete instanceof Closure ? $concrete($this) : $concrete;
925925
}
926926

@@ -942,7 +942,7 @@ protected function resolvePrimitive(ReflectionParameter $parameter)
942942
protected function resolveClass(ReflectionParameter $parameter)
943943
{
944944
try {
945-
return $this->make($parameter->getClass()->name);
945+
return $this->make(Util::getParameterClassName($parameter));
946946
}
947947

948948
// If we can not resolve the class instance, we will check to see if the value

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
if (! $model = $instance->resolveRouteBinding($parameterValue)) {
3839
throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]);

src/Illuminate/Routing/RouteDependencyResolverTrait.php

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

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

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

src/Illuminate/Routing/RouteSignatureParameters.php

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

33
namespace Illuminate\Routing;
44

5+
use Illuminate\Support\Reflector;
56
use Illuminate\Support\Str;
67
use ReflectionFunction;
78
use ReflectionMethod;
@@ -22,7 +23,7 @@ public static function fromAction(array $action, $subClass = null)
2223
: (new ReflectionFunction($action['uses']))->getParameters();
2324

2425
return is_null($subClass) ? $parameters : array_filter($parameters, function ($p) use ($subClass) {
25-
return $p->getClass() && $p->getClass()->isSubclassOf($subClass);
26+
return Reflector::isParameterSubclassOf($p, $subClass);
2627
});
2728
}
2829

0 commit comments

Comments
 (0)