Skip to content

Commit 56f4267

Browse files
committed
add events functionality
1 parent 0f98c14 commit 56f4267

File tree

7 files changed

+173
-0
lines changed

7 files changed

+173
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 0.3.0 - 2021-02-15
4+
- Add events on changes of the valuestore
5+
36
## 0.2.0 - 2020-11-19
47

58
### Changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ You can install the package via composer:
1313
composer require dutchcodingcompany/cached-valuestore
1414
```
1515

16+
## Events
17+
18+
Three events are triggered:
19+
- `DutchCodingCompany\CachedValuestore\Events\PutIntoValuestore`, triggered by `put`, `prepend`, `push`, `offsetSet`, `increment` and `decrement`
20+
- `DutchCodingCompany\CachedValuestore\Events\ForgetFromValuestore`, triggered by `forget`
21+
- `DutchCodingCompany\CachedValuestore\Events\FlushValuestore`, triggered by `flush` and `flushStartingWith`
22+
1623
## License
1724

1825
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"require": {
99
"php" : ">=7.2",
1010
"spatie/valuestore": "^1.0",
11+
"illuminate/container": "^6.0|^7.0|^8.0",
12+
"illuminate/events": "^6.0|^7.0|^8.0",
13+
"illuminate/queue": "^6.0|^7.0|^8.0",
1114
"illuminate/support": "^6.0|^7.0|^8.0"
1215
},
1316
"autoload": {

src/CachedValuestore.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
namespace DutchCodingCompany\CachedValuestore;
44

55
use Spatie\Valuestore\Valuestore;
6+
use Illuminate\Container\Container;
7+
use Illuminate\Contracts\Events\Dispatcher;
68

79
class CachedValuestore extends Valuestore
810
{
911
public static ?string $cachedFileName = null;
1012

13+
protected ?Dispatcher $dispatcher = null;
14+
1115
protected ?array $cachedData = null;
1216

1317
/**
@@ -58,4 +62,81 @@ public static function fromValuestore(Valuestore $store, array $values = null):
5862
{
5963
return static::make($store->fileName, $values);
6064
}
65+
66+
/**
67+
* Gets the dispatcher from the container
68+
*
69+
* @return \Illuminate\Contracts\Events\Dispatcher
70+
*/
71+
protected function dispatcher(): Dispatcher
72+
{
73+
if (is_null($this->dispatcher)) {
74+
$this->dispatcher = Container::getInstance()->make(Dispatcher::class);
75+
}
76+
77+
return $this->dispatcher;
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function put($name, $value = null)
84+
{
85+
// Get arguments to event
86+
$oldValue = parent::get($name);
87+
$newValue = $value;
88+
89+
// Execute method
90+
$result = parent::put($name, $value);
91+
92+
// Trigger event
93+
$this->dispatcher()->dispatch(new Events\PutIntoValuestore($name, $oldValue, $newValue));
94+
95+
// Return stuff
96+
return $result;
97+
}
98+
99+
/**
100+
* {@inheritdoc}
101+
*/
102+
public function forget(string $key)
103+
{
104+
// Get arguments to event
105+
$oldValue = parent::get($key);
106+
107+
// Execute method
108+
$result = parent::forget($key);
109+
110+
// Trigger event
111+
$this->dispatcher()->dispatch(new Events\ForgetFromValuestore($key, $oldValue));
112+
113+
// Return stuff
114+
return $result;
115+
}
116+
117+
/**
118+
* {@inheritdoc}
119+
*/
120+
public function flush()
121+
{
122+
$result = parent::flush();
123+
124+
// Trigger event
125+
$this->dispatcher()->dispatch(new Events\FlushValuestore());
126+
127+
return $result;
128+
}
129+
130+
/**
131+
* {@inheritdoc}
132+
*/
133+
public function flushStartingWith(string $startingWith = '')
134+
{
135+
$result = parent::flushStartingWith($startingWith);
136+
137+
// Trigger event
138+
$this->dispatcher()->dispatch(new Events\FlushValuestore($startingWith));
139+
140+
return $result;
141+
}
61142
}

src/Events/FlushValuestore.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\CachedValuestore\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
class FlushValuestore
8+
{
9+
use SerializesModels;
10+
11+
public ?string $startingWith;
12+
13+
/**
14+
* Create a new event instance.
15+
*
16+
* @return void
17+
*/
18+
public function __construct(string $startingWith = null)
19+
{
20+
$this->startingWith = $startingWith;
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\CachedValuestore\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
class ForgetFromValuestore
8+
{
9+
use SerializesModels;
10+
11+
public string $key;
12+
13+
/** @var string|int|null */
14+
public $oldValue;
15+
16+
/**
17+
* Create a new event instance.
18+
*
19+
* @return void
20+
*/
21+
public function __construct(string $key, $oldValue)
22+
{
23+
$this->key = $key;
24+
$this->oldValue = $oldValue;
25+
}
26+
}

src/Events/PutIntoValuestore.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace DutchCodingCompany\CachedValuestore\Events;
4+
5+
use Illuminate\Queue\SerializesModels;
6+
7+
class PutIntoValuestore
8+
{
9+
use SerializesModels;
10+
11+
/** @var string|array */
12+
public $name;
13+
14+
/** @var string|int|null */
15+
public $oldValue;
16+
17+
/** @var string|int|null */
18+
public $newValue;
19+
20+
/**
21+
* Create a new event instance.
22+
*
23+
* @return void
24+
*/
25+
public function __construct($name, $oldValue, $newValue)
26+
{
27+
$this->name = $name;
28+
$this->oldValue = $oldValue;
29+
$this->newValue = $newValue;
30+
}
31+
}

0 commit comments

Comments
 (0)