diff --git a/src/Screen/Screen.php b/src/Screen/Screen.php index b7a7d3dd9..53b8e901c 100644 --- a/src/Screen/Screen.php +++ b/src/Screen/Screen.php @@ -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; @@ -26,7 +27,7 @@ */ abstract class Screen extends Controller { - use Commander; + use Commander, SerializesModels; /** * @param \Illuminate\Http\Request $request @@ -78,11 +79,6 @@ public function permission(): ?iterable : null; } - /** - * @var Repository - */ - private $source; - /** * The command buttons for this screen. * @@ -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([ @@ -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) @@ -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)); } /** @@ -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); } /** @@ -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. * @@ -482,7 +454,7 @@ private function backWithCurrentState(): RedirectResponse return back(); } - return $this->backWith($currentState->all()); + return back()->with('_state', $this->serializableState()); } /** @@ -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()); } /**