Skip to content

Commit

Permalink
Provide placeholder config
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Nov 23, 2021
1 parent dd9fc4e commit 2d653c7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,20 @@ as well.
you may use all the same classes and functions described in its documentation as well. To
access the user-specific context settings call the `preference()` function anywhere you would
normally use `setting()`:

```php
class Home extends Controller
{
public function index()
{
return view('welcome', [
'theme' => preference('theme'),
'icon' => preference('Users.avatar'),
];
}

public function update_theme()
public function update_avatar()
{
if ($theme = $this->request->getPost('theme')) {
prefernece('theme', $theme);
if ($icon = $this->request->getPost('icon')) {
prefernece('Users.avatar', $icon);
}

return redirect()->back();
Expand All @@ -70,6 +69,35 @@ class Home extends Controller
If no user is authenticated then it will fall back on the `Session` class with semi-persistent
settings for as long as the session lasts.

### Placeholder Config

In most cases each setting should have a corresponding Config file. Sometimes these settings
will not fit under an existing logical grouping, so this library provides a "placeholder"
Config (`Tatter\Preferences\Config\Preferences`). You may add your own version in **app/* to
supply default values:
```php
<?php

namespace Config;

class Preferences extends \Tatter\Preferences\Config\Preferences
{
/**
* Slug for the current user theme.
*/
public string $theme = 'midnight';
}
```

Any function calls with the class unspecified will reference the `Preferences` class:
```php
// Identical calls:
$theme = preference('Preferences.theme');
$theme = preference('theme');
```

> Hint: Don't forget that libraries and modules can provide Config properties via [Registrars](https://codeigniter.com/user_guide/general/configuration.html#registrars)
## Troubleshooting

`Preferences` is a very "thin" library conjoining `Settings` and your authentication library
Expand Down
17 changes: 17 additions & 0 deletions src/Config/Preferences.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tatter\Preferences\Config;

use CodeIgniter\Config\BaseConfig;

/**
* Preferences Config
*
* A placeholder config file to provide a home
* for properties that may not fit elsewhere.
* Intended for use with CodeIgniter\Settings
* and Tatter\Preferences.
*/
class Preferences extends BaseConfig
{
}
5 changes: 5 additions & 0 deletions src/Helpers/preferences_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
function preference(string $key, $value = null)
{
// Check for shorthand requests
if (count(explode('.', $key)) === 1) {
$key = 'Preferences.' . $key;
}

// Authenticated
if ($userId = user_id()) {
$settings = service('settings');
Expand Down
7 changes: 7 additions & 0 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function testGets()
$this->assertSame('Orange', preference('Food.fruit'));
}

public function testGetsShorthand()
{
config('Preferences')->food = 'Ghee';

$this->assertSame('Ghee', preference('food'));
}

public function testForgets()
{
preference('Food.fruit', 'Celery');
Expand Down

0 comments on commit 2d653c7

Please sign in to comment.