Skip to content

Commit 92aeb98

Browse files
authored
Merge pull request #8 from tattersoftware/config
Placeholder Config
2 parents dd9fc4e + 2d653c7 commit 92aeb98

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,20 @@ as well.
4242
you may use all the same classes and functions described in its documentation as well. To
4343
access the user-specific context settings call the `preference()` function anywhere you would
4444
normally use `setting()`:
45-
4645
```php
4746
class Home extends Controller
4847
{
4948
public function index()
5049
{
5150
return view('welcome', [
52-
'theme' => preference('theme'),
51+
'icon' => preference('Users.avatar'),
5352
];
5453
}
5554

56-
public function update_theme()
55+
public function update_avatar()
5756
{
58-
if ($theme = $this->request->getPost('theme')) {
59-
prefernece('theme', $theme);
57+
if ($icon = $this->request->getPost('icon')) {
58+
prefernece('Users.avatar', $icon);
6059
}
6160

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

72+
### Placeholder Config
73+
74+
In most cases each setting should have a corresponding Config file. Sometimes these settings
75+
will not fit under an existing logical grouping, so this library provides a "placeholder"
76+
Config (`Tatter\Preferences\Config\Preferences`). You may add your own version in **app/* to
77+
supply default values:
78+
```php
79+
<?php
80+
81+
namespace Config;
82+
83+
class Preferences extends \Tatter\Preferences\Config\Preferences
84+
{
85+
/**
86+
* Slug for the current user theme.
87+
*/
88+
public string $theme = 'midnight';
89+
}
90+
```
91+
92+
Any function calls with the class unspecified will reference the `Preferences` class:
93+
```php
94+
// Identical calls:
95+
$theme = preference('Preferences.theme');
96+
$theme = preference('theme');
97+
```
98+
99+
> Hint: Don't forget that libraries and modules can provide Config properties via [Registrars](https://codeigniter.com/user_guide/general/configuration.html#registrars)
100+
73101
## Troubleshooting
74102

75103
`Preferences` is a very "thin" library conjoining `Settings` and your authentication library

src/Config/Preferences.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Tatter\Preferences\Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
7+
/**
8+
* Preferences Config
9+
*
10+
* A placeholder config file to provide a home
11+
* for properties that may not fit elsewhere.
12+
* Intended for use with CodeIgniter\Settings
13+
* and Tatter\Preferences.
14+
*/
15+
class Preferences extends BaseConfig
16+
{
17+
}

src/Helpers/preferences_helper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
function preference(string $key, $value = null)
1414
{
15+
// Check for shorthand requests
16+
if (count(explode('.', $key)) === 1) {
17+
$key = 'Preferences.' . $key;
18+
}
19+
1520
// Authenticated
1621
if ($userId = user_id()) {
1722
$settings = service('settings');

tests/SettingsTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public function testGets()
3131
$this->assertSame('Orange', preference('Food.fruit'));
3232
}
3333

34+
public function testGetsShorthand()
35+
{
36+
config('Preferences')->food = 'Ghee';
37+
38+
$this->assertSame('Ghee', preference('food'));
39+
}
40+
3441
public function testForgets()
3542
{
3643
preference('Food.fruit', 'Celery');

0 commit comments

Comments
 (0)