Skip to content

Commit b7a46ba

Browse files
committed
Added forget method. Fixes #7
1 parent cb3f5e9 commit b7a46ba

File tree

9 files changed

+768
-674
lines changed

9 files changed

+768
-674
lines changed

README.md

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@ In order to store the settings in the database, you can run the provided migrati
3939

4040
This will also migrate all other packages. If you don't want to do that you can copy the file
4141
from `vendor/codeigniter4/settings/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php`
42-
into `app/Database/Migrations`, and migrate without the `--all` flag.
42+
into `app/Database/Migrations`, and migrate without the `--all` flag.
43+
44+
## dot Notation
45+
46+
This library uses what we call "dot notation" to specify the class name and the property name to use.
47+
These are joined by a dot, hence the name.
48+
49+
If you have a class named `App`, and the property you are wanting to use is `siteName`, then the key
50+
would be `App.siteName`.
4351

4452
## Usage
4553

4654
To retrieve a config value use the `settings` service.
4755

4856
```php
4957
// The same as config('App')->siteName;
50-
$siteName = service('settings')->get('App', 'siteName');
58+
$siteName = service('settings')->get('App.siteName');
5159
```
5260

5361
In this case we used the short class name, `App`, which the `config()` method automatically locates within the
@@ -62,7 +70,14 @@ will be converted back into a boolean when retrieved. Arrays and objects are ser
6270
when retrieved.
6371

6472
```php
65-
service('setting')->set('App', 'siteName', 'My Great Site');
73+
service('setting')->set('App.siteName', 'My Great Site');
74+
```
75+
76+
You can delete a value from the persistent storage with the `forget()` method. Since it is removed from the storage,
77+
it effectively resets itself back to the default value in config file, if any.
78+
79+
```php
80+
service('setting')->forget('App.siteName')
6681
```
6782

6883
### Using the Helper
@@ -73,12 +88,33 @@ or telling your BaseController to always load it.
7388
```php
7489
helper('setting');
7590

76-
// Using 'dot' syntax to separate class and field name
7791
$name = setting('App.siteName');
7892
// Store a value
79-
setting('App.sitename', 'My Great Site');
93+
setting('App.siteName', 'My Great Site');
8094

8195
// Using the service through the helper
82-
$name = setting()->get('App', 'siteName');
83-
$setting()->set('App', 'siteName', 'My Great Site');
96+
$name = setting()->get('App.siteName');
97+
setting()->set('App.siteName', 'My Great Site');
98+
99+
// Forgetting a value
100+
setting()->forget('App.siteName');
84101
```
102+
103+
## Known Limitations
104+
105+
The following are known limitations of the library:
106+
107+
1. Using the `setting()` helper method does not support setting a `null` value on a property. For most cases, you are
108+
better off forgetting the value or setting it to an empty string. If you need to, you can set it by grabbing
109+
the service and using the `set()` method:
110+
111+
```php
112+
service('settings')->set('App.siteName', null);
113+
setting()->set('App.siteName', null);
114+
```
115+
116+
2. You can currently only store a single setting at a time. While the `DatabaseHandler` uses a local cache to
117+
keep performance as high as possible for reads, writes must be done one at a time.
118+
3. You can only access the first level within a property directly. In most config classes this is a non-issue,
119+
since the properties are simple values. Some config files, like the `database` file, contain properties that
120+
are arrays.

src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,41 @@
77

88
class CreateSettingsTable extends Migration
99
{
10-
public function up()
11-
{
12-
$this->forge->addField('id');
13-
$this->forge->addField([
14-
'class' => [
15-
'type' => 'varchar',
16-
'constraint' => 255,
17-
],
18-
'key' => [
19-
'type' => 'varchar',
20-
'constraint' => 255,
21-
],
22-
'value' => [
23-
'type' => 'text',
24-
'null' => true,
25-
],
26-
'type' => [
27-
'type' => 'varchar',
28-
'constraint' => 31,
29-
'default' => 'string',
30-
],
31-
'created_at' => [
32-
'type' => 'datetime',
33-
'null' => false,
34-
],
35-
'updated_at' => [
36-
'type' => 'datetime',
37-
'null' => false,
38-
],
39-
]);
40-
$this->forge->createTable(config('Settings')->database['table'], true);
41-
}
10+
public function up()
11+
{
12+
$this->forge->addField('id');
13+
$this->forge->addField([
14+
'class' => [
15+
'type' => 'varchar',
16+
'constraint' => 255,
17+
],
18+
'key' => [
19+
'type' => 'varchar',
20+
'constraint' => 255,
21+
],
22+
'value' => [
23+
'type' => 'text',
24+
'null' => true,
25+
],
26+
'type' => [
27+
'type' => 'varchar',
28+
'constraint' => 31,
29+
'default' => 'string',
30+
],
31+
'created_at' => [
32+
'type' => 'datetime',
33+
'null' => false,
34+
],
35+
'updated_at' => [
36+
'type' => 'datetime',
37+
'null' => false,
38+
],
39+
]);
40+
$this->forge->createTable(config('Settings')->database['table'], true);
41+
}
4242

43-
public function down()
44-
{
45-
$this->forge->dropTable(config('Settings')->database['table']);
46-
}
43+
public function down()
44+
{
45+
$this->forge->dropTable(config('Settings')->database['table']);
46+
}
4747
}

0 commit comments

Comments
 (0)