|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Suitmedia\Cacheable; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Suitmedia\Cacheable\Contracts\CacheableModel; |
|
8
|
|
|
use Suitmedia\Cacheable\Events\CacheableEvent; |
|
9
|
|
|
use Suitmedia\Cacheable\Events\CacheableInvalidated; |
|
10
|
|
|
use Suitmedia\Cacheable\Events\CacheableInvalidating; |
|
11
|
|
|
|
|
12
|
|
|
class CacheableObserver |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Event dispatcher object. |
|
16
|
|
|
* |
|
17
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $events; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Cacheable observer constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Dispatcher $events) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->events = $events; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Fire cacheable events. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Suitmedia\Cacheable\Events\CacheableEvent $event |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
public function fireEvent(CacheableEvent $event) |
|
38
|
|
|
{ |
|
39
|
|
|
if (method_exists($this->events, 'fire')) { |
|
40
|
|
|
return $this->events->fire($event); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->events->dispatch($event); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Tell the cacheable service to flush all cache |
|
48
|
|
|
* that related to the given model. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \Suitmedia\Cacheable\Contracts\CacheableModel $model |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function flushCache(CacheableModel $model) |
|
55
|
|
|
{ |
|
56
|
|
|
$tags = $model->cacheTags(); |
|
57
|
|
|
|
|
58
|
|
|
$this->fireEvent(new CacheableInvalidating($model, $tags)); |
|
59
|
|
|
|
|
60
|
|
|
\Cacheable::flush($tags); |
|
61
|
|
|
|
|
62
|
|
|
$this->fireEvent(new CacheableInvalidated($model, $tags)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Saved event handler. |
|
67
|
|
|
* |
|
68
|
|
|
* @param \Suitmedia\Cacheable\Contracts\CacheableModel $model |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function saved(CacheableModel $model) |
|
73
|
|
|
{ |
|
74
|
|
|
$this->flushCache($model); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Deleted event handler. |
|
79
|
|
|
* |
|
80
|
|
|
* @param \Suitmedia\Cacheable\Contracts\CacheableModel $model |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function deleted(CacheableModel $model) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->flushCache($model); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Restored event handler. |
|
91
|
|
|
* |
|
92
|
|
|
* @param \Suitmedia\Cacheable\Contracts\CacheableModel $model |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function restored(CacheableModel $model) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->flushCache($model); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|