From 5530f0f3dbd48fbf29c2f21d4719ba8177ae5b46 Mon Sep 17 00:00:00 2001 From: EriBloo <19932449+EriBloo@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:29:59 +0000 Subject: [PATCH] session objects initial implementation --- src/CacheObjectsServiceProvider.php | 7 ++- src/Concerns/CacheObjectActions.php | 7 ++- src/Concerns/SessionObjectActions.php | 50 +++++++++++++++++ src/Contracts/{Driver.php => CacheDriver.php} | 12 ++--- src/Contracts/CacheObject.php | 11 ++-- src/Contracts/SessionDriver.php | 31 +++++++++++ src/Contracts/SessionObject.php | 18 +++++++ src/Drivers/CacheDriver.php | 11 ++-- src/Drivers/SessionDriver.php | 54 +++++++++++++++++++ tests/Fixtures/BasicSessionObject.php | 36 +++++++++++++ tests/SessionObjectTest.php | 33 ++++++++++++ 11 files changed, 249 insertions(+), 21 deletions(-) create mode 100644 src/Concerns/SessionObjectActions.php rename src/Contracts/{Driver.php => CacheDriver.php} (83%) create mode 100644 src/Contracts/SessionDriver.php create mode 100644 src/Contracts/SessionObject.php create mode 100644 src/Drivers/SessionDriver.php create mode 100644 tests/Fixtures/BasicSessionObject.php create mode 100644 tests/SessionObjectTest.php diff --git a/src/CacheObjectsServiceProvider.php b/src/CacheObjectsServiceProvider.php index 5866213..2d0d7c6 100644 --- a/src/CacheObjectsServiceProvider.php +++ b/src/CacheObjectsServiceProvider.php @@ -6,6 +6,7 @@ use EriBloo\CacheObjects\Commands\MakeCacheObject; use EriBloo\CacheObjects\Drivers\CacheDriver; +use EriBloo\CacheObjects\Drivers\SessionDriver; use Illuminate\Foundation\Application; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -24,8 +25,12 @@ public function configurePackage(Package $package): void ->hasCommand(MakeCacheObject::class) ->hasConfigFile(); - $this->app->scoped('cache-driver', function (Application $app) { + $this->app->scoped('cache-driver', function (Application $app): CacheDriver { return new CacheDriver($app->make('cache')->getStore()); }); + + $this->app->scoped('session-driver', function (Application $app): SessionDriver { + return new SessionDriver($app->make('session')->driver()); + }); } } diff --git a/src/Concerns/CacheObjectActions.php b/src/Concerns/CacheObjectActions.php index 91db83e..23e9664 100644 --- a/src/Concerns/CacheObjectActions.php +++ b/src/Concerns/CacheObjectActions.php @@ -4,7 +4,7 @@ namespace EriBloo\CacheObjects\Concerns; -use EriBloo\CacheObjects\Contracts\Driver; +use EriBloo\CacheObjects\Contracts\CacheDriver; /** * @template TValue @@ -40,7 +40,10 @@ public function delete(): bool ->delete($this); } - protected function resolveDriver(): Driver + /** + * @return CacheDriver + */ + protected function resolveDriver(): CacheDriver { return app()->make('cache-driver'); } diff --git a/src/Concerns/SessionObjectActions.php b/src/Concerns/SessionObjectActions.php new file mode 100644 index 0000000..5cb7a00 --- /dev/null +++ b/src/Concerns/SessionObjectActions.php @@ -0,0 +1,50 @@ +resolveDriver() + ->set($value, $this); + } + + /** + * @return TValue|null + */ + public function retrieve(): mixed + { + return $this->resolveDriver() + ->get($this); + } + + public function delete(): bool + { + return $this->resolveDriver() + ->delete($this); + } + + /** + * @return SessionDriver + */ + protected function resolveDriver(): SessionDriver + { + return app()->make('session-driver'); + } +} diff --git a/src/Contracts/Driver.php b/src/Contracts/CacheDriver.php similarity index 83% rename from src/Contracts/Driver.php rename to src/Contracts/CacheDriver.php index ad2aa3a..55cf064 100644 --- a/src/Contracts/Driver.php +++ b/src/Contracts/CacheDriver.php @@ -4,27 +4,25 @@ namespace EriBloo\CacheObjects\Contracts; -interface Driver +/** + * @template TValue + */ +interface CacheDriver { /** - * @template TValue - * * @param TValue $value * @param CacheObject $cacheObject */ public function set(mixed $value, CacheObject $cacheObject): string; /** - * @template TValue - * * @param CacheObject $cacheObject + * * @return TValue|null */ public function get(CacheObject $cacheObject): mixed; /** - * @template TValue - * * @param CacheObject $cacheObject */ public function delete(CacheObject $cacheObject): bool; diff --git a/src/Contracts/CacheObject.php b/src/Contracts/CacheObject.php index c1c380e..49632dc 100644 --- a/src/Contracts/CacheObject.php +++ b/src/Contracts/CacheObject.php @@ -8,15 +8,10 @@ /** * @template TValue + * + * @extends SessionObject */ -interface CacheObject +interface CacheObject extends SessionObject { - public function key(): Key; - public function ttl(): CarbonInterval; - - /** - * @return Transformer - */ - public function transformer(): Transformer; } diff --git a/src/Contracts/SessionDriver.php b/src/Contracts/SessionDriver.php new file mode 100644 index 0000000..352c44c --- /dev/null +++ b/src/Contracts/SessionDriver.php @@ -0,0 +1,31 @@ + + */ +interface SessionDriver extends CacheDriver +{ + /** + * @param TValue $value + * @param SessionObject $sessionObject + */ + public function set(mixed $value, SessionObject $sessionObject): string; + + /** + * @param SessionObject $sessionObject + * + * @return TValue|null + */ + public function get(SessionObject $sessionObject): mixed; + + /** + * @param SessionObject $sessionObject + */ + public function delete(SessionObject $sessionObject): bool; +} diff --git a/src/Contracts/SessionObject.php b/src/Contracts/SessionObject.php new file mode 100644 index 0000000..70db978 --- /dev/null +++ b/src/Contracts/SessionObject.php @@ -0,0 +1,18 @@ + + */ + public function transformer(): Transformer; +} diff --git a/src/Drivers/CacheDriver.php b/src/Drivers/CacheDriver.php index d39b465..3276fbc 100644 --- a/src/Drivers/CacheDriver.php +++ b/src/Drivers/CacheDriver.php @@ -5,14 +5,19 @@ namespace EriBloo\CacheObjects\Drivers; use EriBloo\CacheObjects\Contracts\CacheObject; -use EriBloo\CacheObjects\Contracts\Driver; use EriBloo\CacheObjects\Events\CacheObjectDeleted; use EriBloo\CacheObjects\Events\CacheObjectMissed; use EriBloo\CacheObjects\Events\CacheObjectRetrieved; use EriBloo\CacheObjects\Events\CacheObjectStored; use Illuminate\Contracts\Cache\Store; - -final class CacheDriver implements Driver +use EriBloo\CacheObjects\Contracts\CacheDriver as CacheDriverContract; + +/** + * @template TValue + * + * @implements CacheDriverContract + */ +final class CacheDriver implements CacheDriverContract { public function __construct( private readonly Store $store, diff --git a/src/Drivers/SessionDriver.php b/src/Drivers/SessionDriver.php new file mode 100644 index 0000000..f18dcda --- /dev/null +++ b/src/Drivers/SessionDriver.php @@ -0,0 +1,54 @@ + + */ +final readonly class SessionDriver implements SessionDriverContract +{ + public function __construct( + private Session $session, + ) {} + + public function set(mixed $value, SessionObject $sessionObject): string + { + $key = (string) $sessionObject->key(); + $transformed = $sessionObject->transformer() + ->onSave($value); + + $this->session->put($key, $transformed); + + return $key; + } + + public function get(SessionObject $sessionObject): mixed + { + $key = (string) $sessionObject->key(); + $value = $this->session->get($key); + + if ($value === null) { + return null; + } + + return $sessionObject->transformer() + ->onLoad($value); + } + + public function delete(SessionObject $sessionObject): bool + { + $key = (string) $sessionObject->key(); + + $this->session->forget($key); + + return true; + } +} diff --git a/tests/Fixtures/BasicSessionObject.php b/tests/Fixtures/BasicSessionObject.php new file mode 100644 index 0000000..302aec8 --- /dev/null +++ b/tests/Fixtures/BasicSessionObject.php @@ -0,0 +1,36 @@ + + */ +final class BasicSessionObject implements SessionObject +{ + /** @use SessionObjectActions */ + use SessionObjectActions; + + public function __construct( + private string $value, + ) {} + + public function key(): StringKey + { + return new StringKey("basic-session-object:{$this->value}"); + } + + /** + * @return SerializeTransformer + */ + public function transformer(): SerializeTransformer + { + return new SerializeTransformer(); + } +} diff --git a/tests/SessionObjectTest.php b/tests/SessionObjectTest.php new file mode 100644 index 0000000..7ba0223 --- /dev/null +++ b/tests/SessionObjectTest.php @@ -0,0 +1,33 @@ +store('test'); + + // assert + assertEquals('test', $obj->retrieve()); +}); + +it('deletes objects properly', function () { + // prepare + $obj = new BasicSessionObject('key'); + $obj->store('test'); + assertNotNull($obj->retrieve()); + + // execute + $obj->delete(); + + // assert + assertNull($obj->retrieve()); +});