Skip to content

Commit

Permalink
Changed state transmission
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Jul 5, 2023
1 parent 8b8ced1 commit b095137
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
14 changes: 0 additions & 14 deletions config/platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,6 @@

'prevents_abandonment' => true,

/*
|--------------------------------------------------------------------------
| Preserve Entire Screen State
|--------------------------------------------------------------------------
|
| This option determines whether the entire screen state should be preserved
| between requests. Enabling this option allows for the full state of the screen
| to be retained, including private fields, which can be useful for listeners.
| Disabling this option will only retain public properties.
|
*/

'full_state' => true,

/*
|--------------------------------------------------------------------------
| Service Provider
Expand Down
18 changes: 18 additions & 0 deletions src/Screen/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ public function findBySlug(string $slug)
->first();
}

/**
* @return Layout|null
*/
public function findByType(string $type)
{
if (is_subclass_of($this, $type)) {
return $this;
}

// Trying to find the right layer inside
return collect($this->layouts)
->flatten()
->map(fn ($layout) => is_object($layout) ? $layout : resolve($layout))
->map(fn (Layout $layout) => $layout->findByType($type))
->filter()
->first();
}

public function jsonSerialize(): array
{
$props = collect(get_object_vars($this));
Expand Down
8 changes: 8 additions & 0 deletions src/Screen/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public function set($key, $value = null)

return $this;
}

/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->count() === 0;
}
}
36 changes: 18 additions & 18 deletions src/Screen/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Route;
use Laravel\SerializableClosure\SerializableClosure;
use Orchid\Platform\Http\Controllers\Controller;
use Orchid\Screen\Layouts\Listener;
use Orchid\Support\Facades\Dashboard;

/**
Expand Down Expand Up @@ -188,22 +188,15 @@ public function asyncParticalLayout(Layout $layout, Request $request)
protected function extractState(): Repository
{
// Check if the '_state' parameter is missing
if (request()->missing('_state') && session()->missing('_state')) {
if (!request()->request->has('_state') && session()->missing('_state')) {
// Return an empty Repository object
return new Repository();
}

$raw = request()->get('_state') ?? session()->get('_state');

// Extract the encrypted state from the '_state' parameter, and deserialize it
$data = config('platform.state.crypt', false) === true
? Crypt::decryptString($raw)
: base64_decode($raw);

$state = unserialize($data);
$raw = request()->post('_state') ?? session()->get('_state');

// Return the deserialized state
return $state();
return Crypt::decrypt($raw);
}

/**
Expand All @@ -229,19 +222,19 @@ public function view(array|Repository $httpQueryArguments = [])
}

/**
* @param $values
* @param $repository
*
* @throws \Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException
*
* @return string
*/
protected function serializableState($values): string
protected function serializableState(Repository $repository): string
{
$state = serialize(new SerializableClosure(fn () => $values));
if ($repository->isEmpty()) {
return '';
}

return config('platform.state.crypt', false) === true
? Crypt::encryptString($state)
: base64_encode($state);
return Crypt::encrypt($repository);
}

/**
Expand Down Expand Up @@ -388,7 +381,14 @@ public function needPreventsAbandonment(): bool
*/
public function isScreenFullStatePreserved(): bool
{
return config('platform.full_state', true);
/** @var Layout $layout */
$existListenerLayout = collect($this->layout())
->map(fn ($layout) => is_object($layout) ? $layout : resolve($layout))
->map(fn (Layout $layout) => $layout->findByType(Listener::class))
->filter()
->isNotEmpty();

return config('platform.full_state', $existListenerLayout);
}

/**
Expand Down

0 comments on commit b095137

Please sign in to comment.