Skip to content
7 changes: 7 additions & 0 deletions docs/basic-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Comment thread
devajmeireles marked this conversation as resolved.
Outdated
```

### Contextual Settings

In addition to the default behavior describe above, `Settings` can be used to define "contextual settings".
Expand Down
9 changes: 9 additions & 0 deletions src/Handlers/ArrayHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->get($class, $property, $context);

$this->forget($class, $property, $context);

return $value;
}

public function flush()
{
$this->general = [];
Expand Down
15 changes: 15 additions & 0 deletions src/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->get($class, $property, $context);

$this->forget($class, $property, $context);

return $value;
}

/**
* Deletes all records from persistent storage, if found,
* and from the local cache.
Expand Down
14 changes: 14 additions & 0 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@
}
}

/**
* 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);

Check failure on line 114 in src/Settings.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Method CodeIgniter\Settings\Settings::pull() with return type void returns mixed but should not return anything.

Check failure on line 114 in src/Settings.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Method CodeIgniter\Settings\Settings::pull() with return type void returns mixed but should not return anything.

Check failure on line 114 in src/Settings.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Method CodeIgniter\Settings\Settings::pull() with return type void returns mixed but should not return anything.

Check failure on line 114 in src/Settings.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.4 Static Analysis

Method CodeIgniter\Settings\Settings::pull() with return type void returns mixed but should not return anything.
}
}

/**
* Removes all settings from the persistent storage,
* Useful during testing. Use with caution.
Expand Down
20 changes: 20 additions & 0 deletions tests/DatabaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,26 @@
]);
}

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');

Check failure on line 224 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / psalm / Psalm Analysis

AssignmentToVoid

tests/DatabaseHandlerTest.php:224:9: AssignmentToVoid: Cannot assign $value to type void (see https://psalm.dev/121)

Check failure on line 224 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 224 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 224 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 224 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.4 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

$this->dontSeeInDatabase($this->table, [
'class' => 'Tests\Support\Config\Example',
'key' => 'siteName',
]);

$this->assertSame('foo', $value);

Check failure on line 231 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'foo' and null will always evaluate to false.

Check failure on line 231 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'foo' and null will always evaluate to false.

Check failure on line 231 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'foo' and null will always evaluate to false.

Check failure on line 231 in tests/DatabaseHandlerTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.4 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'foo' and null will always evaluate to false.
}

public function testForgetWithNoStoredRecord()
{
$this->settings->forget('Example.siteName');
Expand Down
20 changes: 20 additions & 0 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,24 @@

$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');

Check failure on line 78 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / psalm / Psalm Analysis

AssignmentToVoid

tests/SettingsTest.php:78:9: AssignmentToVoid: Cannot assign $value to type void (see https://psalm.dev/121)

Check failure on line 78 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 78 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 78 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 78 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.4 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

$this->assertSame('Amnesia', $value);

Check failure on line 80 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'Amnesia' and null will always evaluate to false.

Check failure on line 80 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'Amnesia' and null will always evaluate to false.

Check failure on line 80 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'Amnesia' and null will always evaluate to false.

Check failure on line 80 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.4 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'Amnesia' and null will always evaluate to false.
$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');

Check failure on line 88 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / psalm / Psalm Analysis

AssignmentToVoid

tests/SettingsTest.php:88:9: AssignmentToVoid: Cannot assign $value to type void (see https://psalm.dev/121)

Check failure on line 88 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 88 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 88 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

Check failure on line 88 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.4 Static Analysis

Result of method CodeIgniter\Settings\Settings::pull() (void) is used.

$this->assertSame('NoContext', $value);

Check failure on line 90 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.2 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'NoContext' and null will always evaluate to false.

Check failure on line 90 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.3 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'NoContext' and null will always evaluate to false.

Check failure on line 90 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.1 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'NoContext' and null will always evaluate to false.

Check failure on line 90 in tests/SettingsTest.php

View workflow job for this annotation

GitHub Actions / phpstan / PHP 8.4 Static Analysis

Call to method PHPUnit\Framework\Assert::assertSame() with 'NoContext' and null will always evaluate to false.
$this->assertSame('Settings Test', $this->settings->get('Example.siteName'));
}
}
Loading