Skip to content

Commit

Permalink
Reduce Data Transfer for State Restore (#2872)
Browse files Browse the repository at this point in the history
* Changed screen state to restore model

* Fixed code style

---------

Co-authored-by: tabuna <tabuna@users.noreply.github.com>
  • Loading branch information
tabuna and tabuna authored Aug 18, 2024
1 parent 5b0a86e commit ec699cc
Showing 1 changed file with 13 additions and 41 deletions.
54 changes: 13 additions & 41 deletions src/Screen/Screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
Expand All @@ -26,7 +27,7 @@
*/
abstract class Screen extends Controller
{
use Commander;
use Commander, SerializesModels;

/**
* @param \Illuminate\Http\Request $request
Expand Down Expand Up @@ -78,11 +79,6 @@ public function permission(): ?iterable
: null;
}

/**
* @var Repository
*/
private $source;

/**
* The command buttons for this screen.
*
Expand Down Expand Up @@ -131,7 +127,6 @@ public function asyncBuild(string $method, string $slug)
abort_unless($this->checkAccess(request()), static::unaccessed());

$state = $this->extractState();

$this->fillPublicProperty($state);

$parameters = request()->collect()->merge([
Expand Down Expand Up @@ -166,11 +161,12 @@ public function asyncParticalLayout(Layout $layout, Request $request)
abort_unless($this->checkAccess(request()), static::unaccessed());

$state = $this->extractState();
$this->fillPublicProperty($state);

$repository = $layout->handle($state, $request);

$view = $layout->build($repository).view('platform::partials.state', [
'state' => $this->serializeStateWithPublicProperties($state),
'state' => $this->serializableState(),
]);

return response($view)
Expand All @@ -197,7 +193,9 @@ protected function extractState(): Repository
}

//deserialize '_state' parameter
return Crypt::decrypt($state);
$screen = Crypt::decrypt($state);

return new Repository(get_object_vars($screen));
}

/**
Expand All @@ -218,25 +216,19 @@ public function view(array|Repository $httpQueryArguments = [])
'layouts' => $this->build($repository),
'formValidateMessage' => $this->formValidateMessage(),
'needPreventsAbandonment' => $this->needPreventsAbandonment(),
'state' => $this->serializeStateWithPublicProperties($repository),
'state' => $this->serializableState(),
'controller' => $this->frontendController(),
]);
}

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

return Crypt::encrypt($repository);
return Crypt::encrypt($this);
}

/**
Expand All @@ -254,26 +246,6 @@ protected function buildQueryRepository(array $httpQueryArguments = []): Reposit
return tap(new Repository($query), fn (Repository $repository) => $this->fillPublicProperty($repository));
}

/**
* Serializes the state of the object using the public properties specified in the given repository.
*
* @param \Orchid\Screen\Repository $repository The repository containing the public properties to be serialized.
*
* @throws \Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException
*
* @return string The serialized state of the object.
*/
public function serializeStateWithPublicProperties(Repository $repository): string
{
if ($this->isScreenFullStatePreserved()) {
return $this->serializableState($repository);
}

$propertiesToSerialize = $repository->getMany($this->getPublicPropertyNames()->toArray());

return $this->serializableState(new Repository($propertiesToSerialize));
}

/**
* Fills the public properties of the object with values from the given repository.
*
Expand Down Expand Up @@ -482,7 +454,7 @@ private function backWithCurrentState(): RedirectResponse
return back();
}

return $this->backWith($currentState->all());
return back()->with('_state', $this->serializableState());
}

/**
Expand All @@ -496,9 +468,9 @@ private function backWithCurrentState(): RedirectResponse
*/
public function backWith(array $data): RedirectResponse
{
$repository = new Repository($data);
$this->fillPublicProperty(new Repository($data));

return back()->with('_state', $this->serializableState($repository));
return back()->with('_state', $this->serializableState());
}

/**
Expand Down

0 comments on commit ec699cc

Please sign in to comment.