generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jon
committed
May 4, 2022
1 parent
4b7cc1f
commit 754e638
Showing
9 changed files
with
355 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Igerslike\InertiaFlash\Drivers; | ||
|
||
use Igerslike\InertiaFlash\Exceptions\PrimaryKeyNotFoundException; | ||
use Illuminate\Support\Collection; | ||
|
||
abstract class AbstractDriver | ||
{ | ||
protected ?string $primaryKey = null; | ||
|
||
/** | ||
* Get the data on the driver | ||
* @return Collection | ||
*/ | ||
abstract function get(): Collection; | ||
|
||
/** | ||
* Put the data into the driver | ||
* @param Collection $container | ||
* @return void | ||
*/ | ||
abstract function put(Collection $container): void; | ||
|
||
/** | ||
* Flush the data available on the driver | ||
* @return void | ||
*/ | ||
abstract function flush(): void; | ||
|
||
/** | ||
* Set the Primary Key | ||
* @param string $key | ||
* @return $this | ||
*/ | ||
public function setPrimaryKey(string $key): static | ||
{ | ||
$this->primaryKey = $key; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Gets & Generates the primary key | ||
* @return string | ||
* @throws PrimaryKeyNotFoundException | ||
*/ | ||
protected function key(): string | ||
{ | ||
if(null === $this->primaryKey){ | ||
throw new PrimaryKeyNotFoundException(); | ||
} | ||
|
||
return implode( | ||
'_', | ||
[ | ||
config('inertia-flash.prefix_key','inertia_container_'), | ||
$this->primaryKey | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Igerslike\InertiaFlash\Drivers; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Cache\CacheManager; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Traits\Macroable; | ||
|
||
class CacheDriver extends AbstractDriver | ||
{ | ||
use Macroable; | ||
|
||
protected array $tags = [ | ||
'inertia_container' | ||
]; | ||
|
||
public function __construct() | ||
{ | ||
$this->discoverPrimaryKey(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get(): Collection | ||
{ | ||
return collect($this->cache()->get($this->key())) ?? collect(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function put(Collection $container): void | ||
{ | ||
$this->cache()->remember( | ||
$this->key(), | ||
$this->cacheTime(), | ||
fn() => $container->toArray() | ||
); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function flush(): void | ||
{ | ||
$this->cache()->forget($this->key()); | ||
} | ||
|
||
/** | ||
* Attempts to discover the primary key of the container. | ||
* Leverages the auth system, when used on frontend | ||
* takes the first user id as primary key. | ||
* | ||
* @return void | ||
*/ | ||
protected function discoverPrimaryKey(): void | ||
{ | ||
if($this->primaryKey !== null){ | ||
return; | ||
} | ||
|
||
$key = auth()->id(); | ||
if($key === null){ | ||
$key = session()->getId(); | ||
} | ||
|
||
$this->setPrimaryKey($key); | ||
} | ||
|
||
/** | ||
* Get the cache manager instance. | ||
* If tags are support it will then tag before returning. | ||
* | ||
* @return CacheManager | ||
*/ | ||
protected function cache(): CacheManager | ||
{ | ||
$cache = cache(); | ||
if($cache->supportsTags()){ | ||
$cache->tags($this->tags); | ||
} | ||
return $cache; | ||
} | ||
|
||
/** | ||
* Use carbon to get the cache ttl. | ||
* | ||
* @return Carbon | ||
*/ | ||
protected function cacheTime(): Carbon | ||
{ | ||
return Carbon::now()->addSeconds(config('inertia-flash.cache-ttl', 60)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Igerslike\InertiaFlash\Drivers; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Traits\Macroable; | ||
|
||
class SessionDriver extends AbstractDriver | ||
{ | ||
use Macroable; | ||
|
||
protected ?string $primaryKey = 'session'; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get(): Collection | ||
{ | ||
return collect(session()->get($this->key(), [])); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function put(Collection $container): void | ||
{ | ||
session()->put($this->key(), $container->toArray()); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function flush(): void | ||
{ | ||
session()->forget($this->key()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Igerslike\InertiaFlash\Exceptions; | ||
|
||
class DriverNotSupportedException extends InertiaFlashException | ||
{ | ||
public function __construct($driver) | ||
{ | ||
parent::__construct("Driver '{$driver}' is not supported on Inertia Flash."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Igerslike\InertiaFlash\Exceptions; | ||
|
||
class InertiaFlashException extends \Exception | ||
{ | ||
public function __construct($driver) | ||
{ | ||
parent::__construct("Driver '{$driver}' does not support Sharing to user"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Igerslike\InertiaFlash\Exceptions; | ||
|
||
class PrimaryKeyNotFoundException extends InertiaFlashException | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct("A primary key is required to share with flash. Please set the primary key on your model."); | ||
} | ||
} |
Oops, something went wrong.