Skip to content

Commit d0c1ab8

Browse files
committed
initial commit
0 parents  commit d0c1ab8

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
## 0.1.0 - 2020-11-16
4+
5+
### Added
6+
- Initial release

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Dutch Coding Company BV <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Cached Valuestore
2+
3+
Adds a cached version of `spatie/valuestore` and registers it to the service container.
4+
5+
## License
6+
7+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "dutchcodingcompany/cached-valuestore",
3+
"description": "Cached version of spatie/valuestore",
4+
"keywords": [
5+
"dcc",
6+
"laravel"
7+
],
8+
"require": {
9+
"php" : ">=7.2",
10+
"spatie/valuestore": "^1.0",
11+
"illuminate/support": "^6.0|^7.0|^8.0"
12+
},
13+
"autoload": {
14+
"psr-4": {
15+
"DutchCodingCompany\\CachedValuestore\\": "src/"
16+
}
17+
},
18+
"extra": {
19+
"laravel": {
20+
"providers": [
21+
"DutchCodingCompany\\CachedValuestore\\ServiceProvider"
22+
]
23+
}
24+
},
25+
"config": {
26+
"sort-packages": true
27+
},
28+
"homepage": "https://github.com/dutchcodingcompany",
29+
"license": "MIT",
30+
"minimum-stability": "dev",
31+
"prefer-stable": true
32+
}

src/CachedValuestore.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\CachedValuestore;
4+
5+
use Spatie\Valuestore\Valuestore;
6+
7+
class CachedValuestore extends Valuestore
8+
{
9+
public static ?string $cachedFileName = null;
10+
11+
protected ?array $cachedData = null;
12+
13+
/**
14+
* Get all values from the store.
15+
*
16+
* @param bool $reloadCache wether or not to reload data from the store
17+
* @return array
18+
*/
19+
public function all(bool $reloadCache = false): array
20+
{
21+
if (! is_null($this->cachedData) && $reloadCache === false) {
22+
return $this->cachedData;
23+
}
24+
25+
return $this->cachedData = parent::all();
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
protected function setContent(array $values)
32+
{
33+
$this->clearCache();
34+
35+
return parent::setContent($values);
36+
}
37+
38+
/**
39+
* Clears the cached data.
40+
*
41+
* @return $this
42+
*/
43+
public function clearCache(): self
44+
{
45+
$this->cachedData = null;
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* Upgrade from regular valuestore.
52+
*
53+
* @param \Spatie\Valuestore\Valuestore $store
54+
* @param array $values
55+
* @return $this
56+
*/
57+
public static function fromValuestore(Valuestore $store, array $values = null): self
58+
{
59+
return static::make($store->fileName, $values);
60+
}
61+
}

src/ServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\CachedValuestore;
4+
5+
use Illuminate\Contracts\Foundation\Application;
6+
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
7+
8+
class ServiceProvider extends BaseServiceProvider
9+
{
10+
public function boot()
11+
{
12+
$this->app->singleton(Valuestore::class, static function (Application $app) {
13+
return CachedValuestore::make(CachedValuestore::$cachedFileName ?? $app->storagePath().DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'settings.json');
14+
});
15+
}
16+
}

0 commit comments

Comments
 (0)