Skip to content

Commit

Permalink
Fix props for different response types
Browse files Browse the repository at this point in the history
  • Loading branch information
emargareten committed Mar 7, 2024
1 parent f3d3e93 commit 993423f
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/Modal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace Emargareten\InertiaModal;

use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceResponse;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Route;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Inertia\Inertia;
use Inertia\LazyProp;
use Inertia\Response;

class Modal implements Responsable
Expand Down Expand Up @@ -166,7 +171,7 @@ protected function component(): array
return [
'component' => $this->component,
'redirectURL' => $this->redirectURL(),
'props' => $this->props,
'props' => $this->resolvePropertyInstances($this->props),
'key' => request()->header('X-Inertia-Modal-Key', (string) Str::uuid()),
];
}
Expand All @@ -188,6 +193,47 @@ protected function redirectURL(): string
return $this->baseURL;
}

/**
* Resolve all necessary class instances in the given props.
*/
public function resolvePropertyInstances(array $props, bool $unpackDotProps = true): array
{
foreach ($props as $key => $value) {
if ($value instanceof Closure) {

Check failure on line 202 in src/Modal.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Emargareten\InertiaModal\Closure not found.

Check failure on line 202 in src/Modal.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Emargareten\InertiaModal\Closure not found.
$value = App::call($value);
}

if ($value instanceof LazyProp) {
$value = App::call($value);
}

if ($value instanceof PromiseInterface) {

Check failure on line 210 in src/Modal.php

View workflow job for this annotation

GitHub Actions / phpstan

Class GuzzleHttp\Promise\PromiseInterface not found.

Check failure on line 210 in src/Modal.php

View workflow job for this annotation

GitHub Actions / phpstan

Class GuzzleHttp\Promise\PromiseInterface not found.
$value = $value->wait();

Check failure on line 211 in src/Modal.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method wait() on an unknown class GuzzleHttp\Promise\PromiseInterface.

Check failure on line 211 in src/Modal.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method wait() on an unknown class GuzzleHttp\Promise\PromiseInterface.
}

if ($value instanceof ResourceResponse || $value instanceof JsonResource) {
$value = $value->toResponse(request())->getData(true);
}

if ($value instanceof Arrayable) {
$value = $value->toArray();
}

if (is_array($value)) {
$value = $this->resolvePropertyInstances($value, false);
}

if ($unpackDotProps && str_contains($key, '.')) {
Arr::set($props, $key, $value);
unset($props[$key]);
} else {
$props[$key] = $value;
}
}

return $props;
}

public function toResponse($request)
{
$response = $this->render();
Expand Down

0 comments on commit 993423f

Please sign in to comment.