Skip to content

Commit

Permalink
Drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
jon committed May 4, 2022
1 parent 4b7cc1f commit 754e638
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 43 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,44 @@ php artisan vendor:publish --tag="inertia-flash-config"
This is the contents of the published config file:

```php
<?php
return [
/*
/*
|--------------------------------------------------------------------------
| Session Key
| Driver Configuration
|--------------------------------------------------------------------------
|
| Key to be used on session, when we flash the items. This should be a
| a reserved and unique key.
| You can configure inertia flash to use session or cache as the driver.
| when using the cache driver inertia flash will leverage your current
| cache driver and attempt to save the temporary shared keys there.
| A unique key is used to generate the unique key for each user
|
| Drivers: 'cache' or 'session' are supported.
| Prefix Key : inertia_container_
| Cache TTL : Time in seconds to store the keys in cache.
*/

'session-key' => 'inertia-container',
'prefix_key' => 'inertia_container_',
'driver' => 'session',

'session-driver' => \Igerslike\InertiaFlash\Drivers\SessionDriver::class,
'cache-driver' => \Igerslike\InertiaFlash\Drivers\CacheDriver::class,

'cache-ttl' => 60,

/*
|--------------------------------------------------------------------------
| Persistent Keys
|--------------------------------------------------------------------------
|
| Here you may configure the keys that should be persisted on the session,
| even if they are empty they will be mapped to their values configured here.
| even if they are empty they will be mapped to their primitives configured here.
|
*/

'persistent-keys' => [
// foo, bar, baz
],

/*
|--------------------------------------------------------------------------
| Middleware
Expand All @@ -63,6 +74,19 @@ return [
|
*/
'middleware' => 'web',

/*
|--------------------------------------------------------------------------
| Ignore URLs & Params
|--------------------------------------------------------------------------
|
| The URls to ignore by default, because inertia runs on web middleware
| Default For URLS: ['broadcasting/auth']
|
*/
'ignore_urls' => [
'broadcasting/auth',
],
];
```

Expand Down Expand Up @@ -114,6 +138,10 @@ inertia_flash()->shareUnless($foo === false, 'foo', 'bar');
// Appending
// You can also use append on regular share method as the third parameter
inertia_flash()->append('foo', 'bar');

// Sharing to a user
// Only available if driver is cache, otherwise session will always use the current logged user
inertia_flash()->forUser($user)->append('foo', 'bar');
```

## Testing
Expand Down
21 changes: 15 additions & 6 deletions config/inertia-flash.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
return [
/*
|--------------------------------------------------------------------------
| Session Configuration
| Driver Configuration
|--------------------------------------------------------------------------
|
| Key to be used on session, when we flash the items. This should be a
| a reserved and unique key.
| You can configure inertia flash to use session or cache as the driver.
| when using the cache driver inertia flash will leverage your current
| cache driver and attempt to save the temporary shared keys there.
| A unique key is used to generate the unique key for each user
|
| Drivers: 'cache' or 'session' are supported.
| Prefix Key : inertia_container_
| Cache TTL : Time in seconds to store the keys in cache.
*/

'session-key' => '_inertia_container',
'flush' => true,
'prefix_key' => 'inertia_container_',
'driver' => 'session',

'session-driver' => \Igerslike\InertiaFlash\Drivers\SessionDriver::class,
'cache-driver' => \Igerslike\InertiaFlash\Drivers\CacheDriver::class,

'cache-ttl' => 60,

/*
|--------------------------------------------------------------------------
Expand All @@ -22,7 +32,6 @@
| even if they are empty they will be mapped to their primitives configured here.
|
*/

'persistent-keys' => [
// foo, bar, baz
],
Expand Down
61 changes: 61 additions & 0 deletions src/Drivers/AbstractDriver.php
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
]
);
}
}
96 changes: 96 additions & 0 deletions src/Drivers/CacheDriver.php
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));
}
}
37 changes: 37 additions & 0 deletions src/Drivers/SessionDriver.php
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());
}
}
11 changes: 11 additions & 0 deletions src/Exceptions/DriverNotSupportedException.php
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.");
}
}
11 changes: 11 additions & 0 deletions src/Exceptions/InertiaFlashException.php
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");
}
}
11 changes: 11 additions & 0 deletions src/Exceptions/PrimaryKeyNotFoundException.php
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.");
}
}
Loading

0 comments on commit 754e638

Please sign in to comment.