1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RichanFongdasen\Varnishable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use RichanFongdasen\Varnishable\Events\ModelHasUpdated; |
8
|
|
|
|
9
|
|
|
class VarnishableObserver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Listening to any saved events. |
13
|
|
|
* |
14
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
public function deleted(Model $model) |
19
|
|
|
{ |
20
|
|
|
$this->handleModelUpdates($model); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Handle any retrieved and wakeup events on |
25
|
|
|
* the observed models. |
26
|
|
|
* |
27
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
protected function handleModelInitialization(Model $model) |
32
|
|
|
{ |
33
|
|
|
if ($updatedAt = $model->getAttribute('updated_at')) { |
34
|
|
|
\Varnishable::setLastModifiedHeader($updatedAt); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handle any update events on the observed models. |
40
|
|
|
* |
41
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
protected function handleModelUpdates(Model $model) |
46
|
|
|
{ |
47
|
|
|
event(new ModelHasUpdated($model)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Listening to any saved events. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function restored(Model $model) |
58
|
|
|
{ |
59
|
|
|
$this->handleModelUpdates($model); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Listening to any retrieved events. |
64
|
|
|
* |
65
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function retrieved(Model $model) |
70
|
|
|
{ |
71
|
|
|
$this->handleModelInitialization($model); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Listening to any saved events. |
76
|
|
|
* |
77
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function saved(Model $model) |
82
|
|
|
{ |
83
|
|
|
$this->handleModelUpdates($model); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Listening to any wakeup events. |
88
|
|
|
* |
89
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function wakeup(Model $model) |
94
|
|
|
{ |
95
|
|
|
$this->handleModelInitialization($model); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|