Skip to content

Commit 6a26ae8

Browse files
authored
Merge pull request #16 from codeigniter4/context
Contexts
2 parents c7d6c8f + 3bc96e8 commit 6a26ae8

File tree

8 files changed

+294
-75
lines changed

8 files changed

+294
-75
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,39 @@ it effectively resets itself back to the default value in config file, if any.
8080
service('setting')->forget('App.siteName')
8181
```
8282

83+
### Contextual Settings
84+
85+
In addition to the default behavior describe above, `Settings` can can be used to define "contextual settings".
86+
A context may be anything you want, but common examples are a runtime environment or an authenticated user.
87+
In order to use a context you pass it as an additional parameter to the `get()`/`set()`/`forget()` methods; if
88+
a context setting is requested and does not exist then the general value will be used.
89+
90+
Contexts may be any unique string you choose, but a recommended format for supplying some consistency is to
91+
give them a category and identifier, like `environment:production` or `group:42`.
92+
93+
An example... Say your App config includes the name of a theme to use to enhance your display. By default
94+
your config file specifies `App.theme = 'default'`. When a user changes their theme, you do not want this to
95+
change the theme for all visitors to the site, so you need to provide the user as the *context* for the change:
96+
97+
```php
98+
$context = 'user:' . user_id();
99+
service('setting')->set('App.theme', 'dark', $context);
100+
```
101+
102+
Now when your filter is determining which theme to apply it can check for the current user as the context:
103+
104+
```php
105+
$context = 'user:' . user_id();
106+
$theme = service('setting')->get('App.theme', $context);
107+
108+
// or using the helper
109+
setting()->get('App.theme', $context);
110+
```
111+
112+
Contexts are a cascading check, so if a context does not match a value it will fall back on general,
113+
i.e. `service('setting')->get('App.theme')`. Return value priority is as follows:
114+
"Setting with a context > Setting without context > Config value > null".
115+
83116
### Using the Helper
84117

85118
The helper provides a shortcut to the using the service. It must first be loaded using the `helper()` method
@@ -100,6 +133,8 @@ setting()->set('App.siteName', 'My Great Site');
100133
setting()->forget('App.siteName');
101134
```
102135

136+
> Note: Due to the shorthand nature of the helper function it cannot access contextual settings.
137+
103138
## Known Limitations
104139

105140
The following are known limitations of the library:

UPGRADING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Upgrade Guide
2+
3+
## Version 1 to 2
4+
***
5+
6+
* Due to the addition of contexts the `BaseHandler` abstract class was changed. Update any handlers that extend this class to include the new and changed methods.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Sparks\Settings\Database\Migrations;
4+
5+
use CodeIgniter\Database\Migration;
6+
7+
class AddContextColumn extends Migration
8+
{
9+
public function up()
10+
{
11+
$this->forge->addColumn(config('Settings')->database['table'], [
12+
'context' => [
13+
'type' => 'varchar',
14+
'constraint' => 255,
15+
'null' => true,
16+
'after' => 'type',
17+
],
18+
]);
19+
}
20+
21+
public function down()
22+
{
23+
$this->forge->dropColumn(config('Settings')->database['table'], 'context');
24+
}
25+
}

src/Handlers/BaseHandler.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
namespace Sparks\Settings\Handlers;
44

5+
use RuntimeException;
6+
57
abstract class BaseHandler
68
{
9+
/**
10+
* Checks whether this handler has a value set.
11+
*/
12+
abstract public function has(string $class, string $property, ?string $context = null): bool;
13+
714
/**
815
* Returns a single value from the handler, if stored.
916
*
1017
* @return mixed
1118
*/
12-
abstract public function get(string $class, string $property);
19+
abstract public function get(string $class, string $property, ?string $context = null);
1320

1421
/**
1522
* If the Handler supports saving values, it
@@ -18,11 +25,27 @@ abstract public function get(string $class, string $property);
1825
*
1926
* @param mixed $value
2027
*
28+
* @throws RuntimeException
29+
*
30+
* @return mixed
31+
*/
32+
public function set(string $class, string $property, $value = null, ?string $context = null)
33+
{
34+
throw new RuntimeException('Set method not implemented for current Settings handler.');
35+
}
36+
37+
/**
38+
* If the Handler supports forgetting values, it
39+
* MUST override this method to provide that functionality.
40+
* Not all Handlers will support writing values.
41+
*
42+
* @throws RuntimeException
43+
*
2144
* @return mixed
2245
*/
23-
public function set(string $class, string $property, $value = null)
46+
public function forget(string $class, string $property, ?string $context = null)
2447
{
25-
throw new \RuntimeException('Set method not implemented for current Settings handler.');
48+
throw new RuntimeException('Forget method not implemented for current Settings handler.');
2649
}
2750

2851
/**

0 commit comments

Comments
 (0)