From dd00556c5341232a85689282c73d30fcb8fce949 Mon Sep 17 00:00:00 2001 From: Marc Heffels Date: Thu, 2 May 2024 17:34:29 +0200 Subject: [PATCH 1/2] Added Persist prefix --- src/Concerns/Persist.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Concerns/Persist.php b/src/Concerns/Persist.php index 4eb53180..20097369 100644 --- a/src/Concerns/Persist.php +++ b/src/Concerns/Persist.php @@ -11,12 +11,15 @@ trait Persist { public array $persist = []; + public string $persistPrefix = ''; + /** * $tableItems: 'filters', 'columns', 'sorting' */ - public function persist(array $tableItems): PowerGridComponent + public function persist(array $tableItems, string $persistPrefix = ''): PowerGridComponent { - $this->persist = $tableItems; + $this->persist = $tableItems; + $this->persistPrefix = $persistPrefix; return $this; } @@ -52,9 +55,9 @@ protected function persistState(string $tableItem): void } if ($this->getPersistDriverConfig() === 'session') { - Session::put('pg:' . $this->tableName, strval(json_encode($state))); + Session::put($this->getPersistName(), strval(json_encode($state))); } elseif ($this->getPersistDriverConfig() === 'cookies') { - Cookie::queue('pg:' . $this->tableName, strval(json_encode($state)), now()->addYears(5)->unix()); + Cookie::queue($this->getPersistName(), strval(json_encode($state)), now()->addYears(5)->unix()); } } @@ -71,10 +74,10 @@ private function restoreState(): void if ($this->getPersistDriverConfig() === 'session') { /** @var null|string $cookieOrSession */ - $cookieOrSession = Session::get('pg:' . $this->tableName); + $cookieOrSession = Session::get($this->getPersistName()); } elseif ($this->getPersistDriverConfig() === 'cookies') { /** @var null|string $cookieOrSession */ - $cookieOrSession = Cookie::get('pg:' . $this->tableName); + $cookieOrSession = Cookie::get($this->getPersistName()); } if (is_null($cookieOrSession)) { @@ -119,4 +122,13 @@ private function getPersistDriverConfig(): string return $persistDriver; } + + private function getPersistName(): string + { + if (!empty($this->persistPrefix)) { + return 'pg:' . $this->persistPrefix . '-' . $this->tableName; + } + + return 'pg:' . $this->tableName; + } } From 0d467acd0051a873ab9760b6033b64d4769db89d Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Mon, 6 May 2024 06:40:48 -0300 Subject: [PATCH 2/2] refactor --- src/Concerns/Persist.php | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/Concerns/Persist.php b/src/Concerns/Persist.php index 20097369..a16484ba 100644 --- a/src/Concerns/Persist.php +++ b/src/Concerns/Persist.php @@ -14,12 +14,13 @@ trait Persist public string $persistPrefix = ''; /** - * $tableItems: 'filters', 'columns', 'sorting' + * $tableItems: 'filters', 'columns', 'sorting', + * $prefix: Add prefix to the persist storage key */ - public function persist(array $tableItems, string $persistPrefix = ''): PowerGridComponent + public function persist(array $tableItems, string $prefix = ''): PowerGridComponent { $this->persist = $tableItems; - $this->persistPrefix = $persistPrefix; + $this->persistPrefix = $prefix; return $this; } @@ -54,11 +55,10 @@ protected function persistState(string $tableItem): void return; } - if ($this->getPersistDriverConfig() === 'session') { - Session::put($this->getPersistName(), strval(json_encode($state))); - } elseif ($this->getPersistDriverConfig() === 'cookies') { - Cookie::queue($this->getPersistName(), strval(json_encode($state)), now()->addYears(5)->unix()); - } + match ($this->getPersistDriverConfig()) { + 'session' => Session::put($this->getPersistKeyName(), strval(json_encode($state))), + default => Cookie::queue($this->getPersistKeyName(), strval(json_encode($state)), now()->addYears(5)->unix()) + }; } /** @@ -70,21 +70,12 @@ private function restoreState(): void return; } - $cookieOrSession = null; - - if ($this->getPersistDriverConfig() === 'session') { - /** @var null|string $cookieOrSession */ - $cookieOrSession = Session::get($this->getPersistName()); - } elseif ($this->getPersistDriverConfig() === 'cookies') { - /** @var null|string $cookieOrSession */ - $cookieOrSession = Cookie::get($this->getPersistName()); - } - - if (is_null($cookieOrSession)) { - return; - } + $storage = match ($this->getPersistDriverConfig()) { + 'session' => Session::get($this->getPersistKeyName()), + default => Cookie::get($this->getPersistKeyName()) + }; - $state = (array) json_decode(strval($cookieOrSession), true); + $state = (array) json_decode(strval($storage), true); if (in_array('columns', $this->persist) && array_key_exists('columns', $state)) { $this->columns = collect($this->columns)->map(function ($column) use ($state) { @@ -123,7 +114,7 @@ private function getPersistDriverConfig(): string return $persistDriver; } - private function getPersistName(): string + private function getPersistKeyName(): string { if (!empty($this->persistPrefix)) { return 'pg:' . $this->persistPrefix . '-' . $this->tableName;