Skip to content

Commit

Permalink
Implement Request $request typehint handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Feb 18, 2025
1 parent d8a20ac commit 8745f9a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/Http/Controllers/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Statamic\Http\Controllers;

use Closure;
use Illuminate\Contracts\View\View as IlluminateView;
use Illuminate\Http\Request;
use ReflectionFunction;
use Statamic\Auth\Protect\Protection;
use Statamic\Exceptions\NotFoundHttpException;
use Statamic\Facades\Data;
Expand Down Expand Up @@ -47,7 +49,7 @@ public function route(Request $request, ...$args)
throw_if(is_callable($view) && $data, new \Exception('Parameter [$data] not supported with [$view] closure!'));

if (is_callable($view)) {
$resolvedView = $view(...$params);
$resolvedView = $view(...static::prepareParams($params, $view, $request));
}

if (isset($resolvedView) && $resolvedView instanceof IlluminateView) {
Expand All @@ -57,7 +59,9 @@ public function route(Request $request, ...$args)
return $resolvedView;
}

$data = array_merge($params, is_callable($data) ? $data(...$params) : $data);
$data = array_merge($params, is_callable($data)
? $data(...static::prepareParams($params, $data, $request))
: $data);

$view = app(View::class)
->template($view)
Expand Down Expand Up @@ -89,4 +93,19 @@ private function getLoadedRouteItem($data)
return $data;
}
}

private function prepareParams(array $params, Closure $closure, Request $request): array
{
$reflect = new ReflectionFunction($closure);

return collect($reflect->getParameters())
->map(function ($param) use ($params, $request) {
if ($param->hasType() && $param->getType()->getName() === Request::class) {
return $request;
}

return $params[$param->getName()];
})
->all();
}
}

0 comments on commit 8745f9a

Please sign in to comment.