Simplify logic for checking for missing unbound models

This commit is contained in:
Dane Everitt 2021-08-07 11:15:30 -07:00
parent 4d1a7e6637
commit 74426a97f4
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53

View file

@ -3,10 +3,10 @@
namespace Pterodactyl\Http\Middleware\Api; namespace Pterodactyl\Http\Middleware\Api;
use Closure; use Closure;
use Exception;
use Illuminate\Support\Reflector; use Illuminate\Support\Reflector;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Routing\UrlRoutable; use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Container\BindingResolutionException;
class PreventUnboundModels class PreventUnboundModels
{ {
@ -19,27 +19,25 @@ class PreventUnboundModels
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @param \Closure $next * @param \Closure $next
* @return mixed * @return mixed
*
* @throws \Exception
*/ */
public function handle($request, Closure $next) public function handle($request, Closure $next)
{ {
$route = $request->route(); $route = $request->route();
$parameters = $route->parameters() ?? [];
$parameters = $route->signatureParameters(UrlRoutable::class); /** @var \ReflectionParameter[] $signatures */
for ($i = 0; $i < count($route->parameters()); $i++) { $signatures = $route->signatureParameters(UrlRoutable::class);
$class = Reflector::getParameterClassName($parameters[$i + 1]); foreach ($signatures as $signature) {
$class = Reflector::getParameterClassName($signature);
// Skip anything that isn't explicitly requested as a model.
if (is_null($class) || !is_subclass_of($class, Model::class)) { if (is_null($class) || !is_subclass_of($class, Model::class)) {
continue; continue;
} }
if (!array_values($route->parameters())[$i] instanceof $class) { if (!$parameters[$signature->getName()] instanceof Model) {
throw new BindingResolutionException( throw new Exception(
sprintf( sprintf('No parameter binding has been defined for model [%s] using route parameter key "%s".', $class, $signature->getName())
'No parameter binding has been defined for model [%s] using route parameter key "%s".',
$class,
array_keys($route->parameters())[$i]
)
); );
} }
} }