From 08b92b15d4606a8f496ff6cecaca6aa20ad790a8 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Sun, 1 Jun 2025 22:28:11 -0300 Subject: [PATCH 1/7] Added 'pull' method to ArrayHandler, BaseHandler, and DatabaseHandler classes --- src/Handlers/ArrayHandler.php | 9 +++++++++ src/Handlers/BaseHandler.php | 15 +++++++++++++++ src/Handlers/DatabaseHandler.php | 15 +++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/Handlers/ArrayHandler.php b/src/Handlers/ArrayHandler.php index 29467ee..9ffe8bf 100644 --- a/src/Handlers/ArrayHandler.php +++ b/src/Handlers/ArrayHandler.php @@ -47,6 +47,15 @@ public function forget(string $class, string $property, ?string $context = null) $this->forgetStored($class, $property, $context); } + public function pull(string $class, string $property, ?string $context = null): mixed + { + $value = $this->getStored($class, $property, $context); + + $this->forget($class, $property, $context); + + return $value; + } + public function flush() { $this->general = []; diff --git a/src/Handlers/BaseHandler.php b/src/Handlers/BaseHandler.php index d8b01a5..0a6b02a 100644 --- a/src/Handlers/BaseHandler.php +++ b/src/Handlers/BaseHandler.php @@ -50,6 +50,21 @@ public function forget(string $class, string $property, ?string $context = null) throw new RuntimeException('Forget method not implemented for current Settings handler.'); } + /** + * If the Handler supports pulling values, it + * MUST override this method to provide that functionality. + * Not all Handlers will support writing values. + * Must throw RuntimeException for any failures. + * + * @return mixed + * + * @throws RuntimeException + */ + public function pull(string $class, string $property, ?string $context = null) + { + throw new RuntimeException('Pull method not implemented for current Settings handler.'); + } + /** * All handlers MUST support flushing all values. * diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index d93b34c..ae09ee2 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -140,6 +140,21 @@ public function forget(string $class, string $property, ?string $context = null) $this->forgetStored($class, $property, $context); } + /** + * Retrieves a value from persistent storage + * and deletes it from the local cache. + * + * @return mixed|null + */ + public function pull(string $class, string $property, ?string $context = null): mixed + { + $value = $this->getStored($class, $property, $context); + + $this->forget($class, $property, $context); + + return $value; + } + /** * Deletes all records from persistent storage, if found, * and from the local cache. From 6e5e68d25edbdbf104f1ed6c791c5b83dd4e0f4b Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Sun, 1 Jun 2025 22:57:29 -0300 Subject: [PATCH 2/7] Refactored pull methods and added tests for ArrayHandler, DatabaseHandler, and Settings classes --- src/Handlers/ArrayHandler.php | 2 +- src/Handlers/DatabaseHandler.php | 2 +- src/Settings.php | 14 ++++++++++++++ tests/DatabaseHandlerTest.php | 20 ++++++++++++++++++++ tests/SettingsTest.php | 20 ++++++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Handlers/ArrayHandler.php b/src/Handlers/ArrayHandler.php index 9ffe8bf..f5374ce 100644 --- a/src/Handlers/ArrayHandler.php +++ b/src/Handlers/ArrayHandler.php @@ -49,7 +49,7 @@ public function forget(string $class, string $property, ?string $context = null) public function pull(string $class, string $property, ?string $context = null): mixed { - $value = $this->getStored($class, $property, $context); + $value = $this->get($class, $property, $context); $this->forget($class, $property, $context); diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index ae09ee2..7192326 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -148,7 +148,7 @@ public function forget(string $class, string $property, ?string $context = null) */ public function pull(string $class, string $property, ?string $context = null): mixed { - $value = $this->getStored($class, $property, $context); + $value = $this->get($class, $property, $context); $this->forget($class, $property, $context); diff --git a/src/Settings.php b/src/Settings.php index cd4e577..48f4802 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -101,6 +101,20 @@ public function forget(string $key, ?string $context = null) } } + /** + * Retrieves a value from the persistent storage and removes it. + * + * @return void + */ + public function pull(string $key, ?string $context = null) + { + [$class, $property] = $this->prepareClassAndProperty($key); + + foreach ($this->getWriteHandlers() as $handler) { + return $handler->pull($class, $property, $context); + } + } + /** * Removes all settings from the persistent storage, * Useful during testing. Use with caution. diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index 7e77682..4c73a4f 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -211,6 +211,26 @@ public function testForgetSuccess() ]); } + public function testPullSuccess() + { + $this->hasInDatabase($this->table, [ + 'class' => 'Tests\Support\Config\Example', + 'key' => 'siteName', + 'value' => 'foo', + 'created_at' => Time::now()->toDateTimeString(), + 'updated_at' => Time::now()->toDateTimeString(), + ]); + + $value = $this->settings->pull('Example.siteName'); + + $this->dontSeeInDatabase($this->table, [ + 'class' => 'Tests\Support\Config\Example', + 'key' => 'siteName', + ]); + + $this->assertSame('foo', $value); + } + public function testForgetWithNoStoredRecord() { $this->settings->forget('Example.siteName'); diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index a519142..f49cb8f 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -70,4 +70,24 @@ public function testForgetWithContext() $this->assertSame('Bar', $this->settings->get('Example.siteName', 'category:disease')); } + + public function testPullWithContext() + { + $this->settings->set('Example.siteName', 'Amnesia', 'category:disease'); + + $value = $this->settings->pull('Example.siteName', 'category:disease'); + + $this->assertSame('Amnesia', $value); + $this->assertSame('Settings Test', $this->settings->get('Example.siteName', 'category:disease')); + } + + public function testPullWithoutContext() + { + $this->settings->set('Example.siteName', 'NoContext'); + + $value = $this->settings->pull('Example.siteName'); + + $this->assertSame('NoContext', $value); + $this->assertSame('Settings Test', $this->settings->get('Example.siteName')); + } } From 2a66f649e80b29d8d91f4586a51d50b00b2cc8b1 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Sun, 1 Jun 2025 22:59:18 -0300 Subject: [PATCH 3/7] Added pull method explanation and example to basic-usage.md documentation --- docs/basic-usage.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/basic-usage.md b/docs/basic-usage.md index 0030fa2..1bcc530 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -43,6 +43,13 @@ If you ever need to completely remove all settings from their persistent storage service('settings')->flush(); ``` +Also, you can use the `pull()` method to retrieve a setting and then remove it from the persistent storage in one go. This is useful when you want to retrieve a value and ensure it is no longer available for future use. + +```php +// The same as config('App')->siteName; +$siteName = service('settings')->pull('App.siteName'); +``` + ### Contextual Settings In addition to the default behavior describe above, `Settings` can be used to define "contextual settings". From b539ce6259e2c25544f794da9d9f0562657677ca Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Mon, 2 Jun 2025 11:27:48 -0300 Subject: [PATCH 4/7] Removed redundant comment from basic-usage.md documentation --- docs/basic-usage.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/basic-usage.md b/docs/basic-usage.md index 1bcc530..b482748 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -46,7 +46,6 @@ service('settings')->flush(); Also, you can use the `pull()` method to retrieve a setting and then remove it from the persistent storage in one go. This is useful when you want to retrieve a value and ensure it is no longer available for future use. ```php -// The same as config('App')->siteName; $siteName = service('settings')->pull('App.siteName'); ``` From f78037daea9b561abc9f2b6e74aff3efb3eed850 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Mon, 2 Jun 2025 11:30:21 -0300 Subject: [PATCH 5/7] Updated return type for pull method in Settings.php --- src/Settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Settings.php b/src/Settings.php index 48f4802..fad8bce 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -104,7 +104,7 @@ public function forget(string $key, ?string $context = null) /** * Retrieves a value from the persistent storage and removes it. * - * @return void + * @return mixed */ public function pull(string $key, ?string $context = null) { From 8b2d904a0fdb8a5f374ac997a84b2ba2681ae8b6 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Mon, 2 Jun 2025 11:31:28 -0300 Subject: [PATCH 6/7] Refactored method name from 'pull' to 'take' across multiple files --- src/Handlers/ArrayHandler.php | 2 +- src/Handlers/BaseHandler.php | 2 +- src/Handlers/DatabaseHandler.php | 2 +- src/Settings.php | 4 ++-- tests/DatabaseHandlerTest.php | 2 +- tests/SettingsTest.php | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Handlers/ArrayHandler.php b/src/Handlers/ArrayHandler.php index f5374ce..0fff33c 100644 --- a/src/Handlers/ArrayHandler.php +++ b/src/Handlers/ArrayHandler.php @@ -47,7 +47,7 @@ public function forget(string $class, string $property, ?string $context = null) $this->forgetStored($class, $property, $context); } - public function pull(string $class, string $property, ?string $context = null): mixed + public function take(string $class, string $property, ?string $context = null): mixed { $value = $this->get($class, $property, $context); diff --git a/src/Handlers/BaseHandler.php b/src/Handlers/BaseHandler.php index 0a6b02a..3778740 100644 --- a/src/Handlers/BaseHandler.php +++ b/src/Handlers/BaseHandler.php @@ -60,7 +60,7 @@ public function forget(string $class, string $property, ?string $context = null) * * @throws RuntimeException */ - public function pull(string $class, string $property, ?string $context = null) + public function take(string $class, string $property, ?string $context = null) { throw new RuntimeException('Pull method not implemented for current Settings handler.'); } diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index 7192326..05ad017 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -146,7 +146,7 @@ public function forget(string $class, string $property, ?string $context = null) * * @return mixed|null */ - public function pull(string $class, string $property, ?string $context = null): mixed + public function take(string $class, string $property, ?string $context = null): mixed { $value = $this->get($class, $property, $context); diff --git a/src/Settings.php b/src/Settings.php index fad8bce..2806190 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -106,12 +106,12 @@ public function forget(string $key, ?string $context = null) * * @return mixed */ - public function pull(string $key, ?string $context = null) + public function take(string $key, ?string $context = null) { [$class, $property] = $this->prepareClassAndProperty($key); foreach ($this->getWriteHandlers() as $handler) { - return $handler->pull($class, $property, $context); + return $handler->take($class, $property, $context); } } diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index 4c73a4f..06ee348 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -221,7 +221,7 @@ public function testPullSuccess() 'updated_at' => Time::now()->toDateTimeString(), ]); - $value = $this->settings->pull('Example.siteName'); + $value = $this->settings->take('Example.siteName'); $this->dontSeeInDatabase($this->table, [ 'class' => 'Tests\Support\Config\Example', diff --git a/tests/SettingsTest.php b/tests/SettingsTest.php index f49cb8f..0236528 100644 --- a/tests/SettingsTest.php +++ b/tests/SettingsTest.php @@ -75,7 +75,7 @@ public function testPullWithContext() { $this->settings->set('Example.siteName', 'Amnesia', 'category:disease'); - $value = $this->settings->pull('Example.siteName', 'category:disease'); + $value = $this->settings->take('Example.siteName', 'category:disease'); $this->assertSame('Amnesia', $value); $this->assertSame('Settings Test', $this->settings->get('Example.siteName', 'category:disease')); @@ -85,7 +85,7 @@ public function testPullWithoutContext() { $this->settings->set('Example.siteName', 'NoContext'); - $value = $this->settings->pull('Example.siteName'); + $value = $this->settings->take('Example.siteName'); $this->assertSame('NoContext', $value); $this->assertSame('Settings Test', $this->settings->get('Example.siteName')); From b93f85fc896c2db5dfc7611a9316a03dcacdeda4 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Mon, 2 Jun 2025 11:33:27 -0300 Subject: [PATCH 7/7] Updated method name from 'pull' to 'take' in basic-usage.md documentation --- docs/basic-usage.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/basic-usage.md b/docs/basic-usage.md index b482748..fe07135 100644 --- a/docs/basic-usage.md +++ b/docs/basic-usage.md @@ -43,10 +43,10 @@ If you ever need to completely remove all settings from their persistent storage service('settings')->flush(); ``` -Also, you can use the `pull()` method to retrieve a setting and then remove it from the persistent storage in one go. This is useful when you want to retrieve a value and ensure it is no longer available for future use. +Also, you can use the `take()` method to retrieve a setting and then remove it from the persistent storage in one go. This is useful when you want to retrieve a value and ensure it is no longer available for future use. ```php -$siteName = service('settings')->pull('App.siteName'); +$siteName = service('settings')->take('App.siteName'); ``` ### Contextual Settings