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