|
3 | 3 | namespace DutchCodingCompany\CachedValuestore; |
4 | 4 |
|
5 | 5 | use Spatie\Valuestore\Valuestore; |
| 6 | +use Illuminate\Container\Container; |
| 7 | +use Illuminate\Contracts\Events\Dispatcher; |
6 | 8 |
|
7 | 9 | class CachedValuestore extends Valuestore |
8 | 10 | { |
9 | 11 | public static ?string $cachedFileName = null; |
10 | 12 |
|
| 13 | + protected ?Dispatcher $dispatcher = null; |
| 14 | + |
11 | 15 | protected ?array $cachedData = null; |
12 | 16 |
|
13 | 17 | /** |
@@ -58,4 +62,81 @@ public static function fromValuestore(Valuestore $store, array $values = null): |
58 | 62 | { |
59 | 63 | return static::make($store->fileName, $values); |
60 | 64 | } |
| 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 | + } |
61 | 142 | } |
0 commit comments