Skip to content

Commit 6f1c188

Browse files
Merge pull request #36 from spatie/v2
V2
2 parents 8ec4bcc + b5e7be1 commit 6f1c188

31 files changed

+1017
-409
lines changed

CHANGELOG.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
All notable changes to `laravel-settings` will be documented in this file
44

5+
## 2.0.0 - 2020-03-03
6+
7+
- settings classes:
8+
- properties won't be loaded when constructed but when requested
9+
- receive a `SettingsMapper` when constructed
10+
- faking settings will now only request non-given properties from the repository
11+
- rewritten `SettingsMapper` from scratch
12+
- removed `SettingsPropertyData` and `ettingsPropertyDataCollection`
13+
- changed signatures of `SavingSettings` and `LoadingSettings` events
14+
- added support for caching settings
15+
- renamed `cache_path` in settings.php to `discovered_settings_cache_path`
16+
517
## 1.0.8 - 2020-03-03
618

719
- fix for properties without defined type
@@ -12,19 +24,19 @@ All notable changes to `laravel-settings` will be documented in this file
1224

1325
## 1.0.6 - 2020-02-05
1426

15-
- add support for restoring settings after a Laravel schema:dump
27+
- add support for restoring settings after a Laravel schema:dump
1628

1729
## 1.0.5 - 2020-01-29
1830

19-
- bump the `doctrine/dbal` dependency
31+
- bump the `doctrine/dbal` dependency
2032

2133
## 1.0.4 - 2020-01-08
2234

23-
- add support for getting the locked settings
35+
- add support for getting the locked settings
2436

2537
## 1.0.3 - 2020-11-26
2638

27-
- add PHP 8 support
39+
- add PHP 8 support
2840

2941
## 1.0.2 - 2020-11-26
3042

README.md

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ php artisan vendor:publish --provider="Spatie\LaravelSettings\LaravelSettingsSer
8989
This is the contents of the published config file:
9090

9191
```php
92+
9293
return [
9394

9495
/*
95-
* You can register all the settings classes here.
96+
* Each settings class used in your application must be registered, you can
97+
* put them (manually) here.
9698
*/
9799
'settings' => [
98100

@@ -102,22 +104,17 @@ return [
102104
* When you create a new settings migration via the `make:settings-migration`
103105
* command the package will store these migrations in this directory.
104106
*/
105-
106107
'migrations_path' => database_path('settings'),
107108

108109
/*
109-
* When no repository was set for a settings class this repository will be
110-
* used for loading and saving settings.
110+
* When no repository was set for a settings class the following repository
111+
* will be used for loading and saving settings.
111112
*/
112-
113113
'default_repository' => 'database',
114114

115115
/*
116-
* Settings will be stored and loaded from these repositories. There are
117-
* Two types of repositories: database and Redis. But its always
118-
* possible to create your specific types of repositories.
116+
* Settings will be stored and loaded from these repositories.
119117
*/
120-
121118
'repositories' => [
122119
'database' => [
123120
'type' => Spatie\LaravelSettings\SettingsRepositories\DatabaseSettingsRepository::class,
@@ -132,11 +129,20 @@ return [
132129
],
133130

134131
/*
135-
* When the package discovers a setting with a type other than the PHP built
136-
* in types, it should be cast. These casts will automatically cast types
137-
* when they occur in a settings class.
132+
* The contents of settings classes can be cached through your application,
133+
* settings will be stored within a provided Laravel store and can have an
134+
* additional prefix.
138135
*/
136+
'cache' => [
137+
'enabled' => env('SETTINGS_CACHE_ENABLED', false),
138+
'store' => null,
139+
'prefix' => null,
140+
],
139141

142+
/*
143+
* These global casts will be automatically used whenever a property within
144+
* your settings class isn't a default PHP type.
145+
*/
140146
'global_casts' => [
141147
DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class,
142148
DateTimeZone::class => Spatie\LaravelSettings\SettingsCasts\DateTimeZoneCast::class,
@@ -147,20 +153,16 @@ return [
147153
* The package will look for settings in these paths and automatically
148154
* register them.
149155
*/
150-
151156
'auto_discover_settings' => [
152157
app()->path(),
153158
],
154159

155160
/*
156-
* When in production, it is advised to cache the automatically discovered
157-
* and registered setting classes will be cached in this path.
161+
* Automatically discovered settings classes can be cached so they don't
162+
* need to be searched each time the application boors up.
158163
*/
159-
160-
'cache_path' => storage_path('app/laravel-settings'),
164+
'discovered_settings_cache_path' => storage_path('app/laravel-settings'),
161165
];
162-
163-
164166
```
165167

166168
## Usage
@@ -190,7 +192,8 @@ Now, you will have to add this settings class to the `settings.php` config file
190192

191193
```php
192194
/*
193-
* You can register all the settings classes here.
195+
* Each settings class used in your application must be registered, you can
196+
* put them (manually) here.
194197
*/
195198
'settings' => [
196199
GeneralSettings::class
@@ -316,7 +319,7 @@ class CreateGeneralSettings extends SettingsMigration
316319
}
317320
```
318321

319-
We haven't added a `down` method, but this can be added if required. In the `up` method, you can change the settings data in a repository when migrating. There are a few default operations supported:
322+
We haven't added a `down` method, but this can be added if disered. In the `up` method, you can change the settings data in a repository when migrating. There are a few default operations supported:
320323

321324
#### Adding a property
322325

@@ -703,6 +706,30 @@ DateSettings::fake([
703706

704707
Now, when the `DateSettings` settings class is injected somewhere in your application, the `birth_date` property will be `DateTime('16-05-1994')`.
705708

709+
### Caching settings
710+
711+
It takes a small amount of time to load a settings class from a repository. When you've got many settings classes, these added small amounts of time can grow quickly out of hand. The package has built-in support for caching stored settings using the Laravel cache.
712+
713+
You should first enable the cache within the `settings.php` config file:
714+
715+
```php
716+
'cache' => [
717+
'enabled' => env('SETTINGS_CACHE_ENABLED', false),
718+
'store' => null,
719+
'prefix' => null,
720+
],
721+
```
722+
723+
We suggest you enable caching in production by adding `SETTINGS_CACHE_ENABLED=true` to your `.env` file. It is also possible to define a store for the cache, which should be one of the stores you defined in the `cache.php` config file. If no store were defined, the default cache store would be taken. To avoid conflicts within the cache, you can also define a prefix that will be added to each cache entry.
724+
725+
That's it. The package is now smart enough to cache the settings the first time they're loaded. Whenever the settings are edited, the package will refresh the settings.
726+
727+
You can always clear the cached settings with the following command:
728+
729+
```bash
730+
php artisan settings:clear-cache
731+
```
732+
706733
### Auto discovering settings classes
707734

708735
Each settings class you create should be added to the `settings` array within the `settings.php` config file. When you've got a lot of settings, this can be quickly forgotten.
@@ -967,6 +994,15 @@ It is required to return raw values again in the `getPropertiesInGroup` and `get
967994

968995
Each repository's constructor will receive a `$config` array that the user-defined for the repository within the application `settings.php` config file. It is possible to add other dependencies to the constructor. They will be injected when the repository is created.
969996

997+
### Events
998+
999+
The package will emit a series of events when loading/saving settings classes:
1000+
1001+
- `LoadingSettings` whenever settings are loaded from the repository but not yet inserted in the settings class
1002+
- `LoadedSettings` after settings are loaded into the settings class
1003+
- `SavingSettings` whenever settings are saved to the repository but are not yet cast or encrypted
1004+
- `SavedSettings` after settings are stored within the repository
1005+
9701006
## Testing
9711007

9721008
``` bash

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
"require": {
1919
"php": "^7.4|^8.0",
2020
"ext-json": "*",
21-
"illuminate/database": "^7.0|^8.0",
21+
"illuminate/database": "^7.0|^8.24.0",
2222
"doctrine/dbal" : "^2.6|^3",
2323
"phpdocumentor/type-resolver": "^1.4",
2424
"spatie/data-transfer-object": "^2.0",
2525
"spatie/temporary-directory": "^1.2"
2626
},
2727
"require-dev": {
28+
"ext-redis": "*",
29+
"mockery/mockery": "^1.4",
2830
"orchestra/testbench": "^5.0|^6.0",
29-
"mockery/mockery" : "^1.4",
3031
"phpunit/phpunit": "^9.1",
3132
"psalm/plugin-laravel": "^1.4",
3233
"spatie/phpunit-snapshot-assertions": "^4.2",
33-
"vimeo/psalm": "^4.2",
34-
"ext-redis" : "*"
34+
"vimeo/psalm": "^4.2"
3535
},
3636
"autoload": {
3737
"psr-4": {
@@ -44,7 +44,7 @@
4444
}
4545
},
4646
"scripts": {
47-
"psalm": "vendor/bin/psalm",
47+
"psalm": "vendor/bin/psalm --no-diff",
4848
"test": "vendor/bin/phpunit",
4949
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
5050
},

config/settings.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
return [
44

55
/*
6-
* You can register all the settings classes here.
6+
* Each settings class used in your application must be registered, you can
7+
* put them (manually) here.
78
*/
89
'settings' => [
910

@@ -16,15 +17,13 @@
1617
'migrations_path' => database_path('settings'),
1718

1819
/*
19-
* When no repository was set for a settings class this repository will be
20-
* used for loading and saving settings.
20+
* When no repository was set for a settings class the following repository
21+
* will be used for loading and saving settings.
2122
*/
2223
'default_repository' => 'database',
2324

2425
/*
25-
* Settings will be stored and loaded from these repositories. There are
26-
* two types of repositories: database and Redis. But its always
27-
* possible to create your specific types of repositories.
26+
* Settings will be stored and loaded from these repositories.
2827
*/
2928
'repositories' => [
3029
'database' => [
@@ -40,9 +39,19 @@
4039
],
4140

4241
/*
43-
* When the package discovers a setting with a type other than the PHP built
44-
* in types, it should be cast. These casts will automatically cast types
45-
* when they occur in a settings class.
42+
* The contents of settings classes can be cached through your application,
43+
* settings will be stored within a provided Laravel store and can have an
44+
* additional prefix.
45+
*/
46+
'cache' => [
47+
'enabled' => env('SETTINGS_CACHE_ENABLED', false),
48+
'store' => null,
49+
'prefix' => null,
50+
],
51+
52+
/*
53+
* These global casts will be automatically used whenever a property within
54+
* your settings class isn't a default PHP type.
4655
*/
4756
'global_casts' => [
4857
DateTimeInterface::class => Spatie\LaravelSettings\SettingsCasts\DateTimeInterfaceCast::class,
@@ -59,8 +68,8 @@
5968
],
6069

6170
/*
62-
* When in production, it is advised to cache the automatically discovered
63-
* and registered setting classes will be cached in this path.
71+
* Automatically discovered settings classes can be cached so they don't
72+
* need to be searched each time the application boors up.
6473
*/
65-
'cache_path' => storage_path('app/laravel-settings'),
74+
'discovered_settings_cache_path' => storage_path('app/laravel-settings'),
6675
];

psalm.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
</projectFiles>
1414

1515
<issueHandlers>
16+
<UnusedClosureParam>
17+
<errorLevel type="suppress">
18+
<file name="src/SettingsMapper.php"/>
19+
</errorLevel>
20+
</UnusedClosureParam>
21+
<InvalidReturnType>
22+
<errorLevel type="suppress">
23+
<file name="src/SettingsConfig.php"/>
24+
</errorLevel>
25+
</InvalidReturnType>
26+
<InvalidReturnStatement>
27+
<errorLevel type="suppress">
28+
<file name="src/SettingsConfig.php"/>
29+
</errorLevel>
30+
</InvalidReturnStatement>
1631
</issueHandlers>
1732

1833
<plugins>

src/Console/CacheDiscoveredSettingsCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ public function handle(SettingsContainer $container, Filesystem $files): void
1717
{
1818
$this->info('Caching registered settings...');
1919

20-
$container->getSettingClasses()
20+
$container
21+
->clearCache()
22+
->getSettingClasses()
2123
->pipe(function (Collection $settingClasses) use ($files) {
22-
$cachePath = config('settings.cache_path');
24+
$cachePath = config('settings.discovered_settings_cache_path');
2325

2426
$files->makeDirectory($cachePath, 0755, true, true);
2527

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Spatie\LaravelSettings\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Spatie\LaravelSettings\SettingsCache;
7+
8+
class ClearCachedSettingsCommand extends Command
9+
{
10+
protected $signature = 'settings:clear-cache';
11+
12+
protected $description = 'Clear cached settings';
13+
14+
public function handle(SettingsCache $settingsCache): void
15+
{
16+
$settingsCache->clear();
17+
18+
$this->info('Cached settings cleared!');
19+
}
20+
}

src/Console/ClearDiscoveredSettingsCacheCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ClearDiscoveredSettingsCacheCommand extends Command
1313

1414
public function handle(Filesystem $files): void
1515
{
16-
$files->delete(config('settings.cache_path') . '/settings.php');
16+
$files->delete(config('settings.discovered_settings_cache_path') . '/settings.php');
1717

1818
$this->info('Cached discovered settings cleared!');
1919
}

src/Events/LoadingSettings.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Spatie\LaravelSettings\Events;
44

5-
use Spatie\LaravelSettings\Support\SettingsPropertyDataCollection;
5+
use Illuminate\Support\Collection;
66

77
class LoadingSettings
88
{
99
public string $settingsClass;
1010

11-
public SettingsPropertyDataCollection $properties;
11+
public Collection $properties;
1212

13-
public function __construct(string $settingsClass, SettingsPropertyDataCollection $properties)
13+
public function __construct(string $settingsClass, Collection $properties)
1414
{
1515
$this->settingsClass = $settingsClass;
16-
1716
$this->properties = $properties;
1817
}
1918
}

0 commit comments

Comments
 (0)