1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RichanFongdasen\Varnishable\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
use Symfony\Component\HttpFoundation\HeaderBag; |
7
|
|
|
|
8
|
|
|
trait ManipulateHttpResponse |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* HTTP Request object. |
12
|
|
|
* |
13
|
|
|
* @var \Illuminate\Http\Request |
14
|
|
|
*/ |
15
|
|
|
protected $requestHeaders; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Acknowledge the ESI support and send a specific |
19
|
|
|
* HTTP header as a reply. |
20
|
|
|
* |
21
|
|
|
* @param \Illuminate\Http\Response $response |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
protected function acknowledgeEsiSupport(Response $response) |
25
|
|
|
{ |
26
|
|
|
$esiHeader = $this->getConfig('esi_capability_header'); |
27
|
|
|
|
28
|
|
|
if ($esiHeader = $this->requestHeaders->get($esiHeader)) { |
29
|
|
|
$response->header($this->getConfig('esi_reply_header'), $esiHeader); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add cacheable header so varnish can recognize |
35
|
|
|
* the response as a cacheable content. |
36
|
|
|
* |
37
|
|
|
* @param \Illuminate\Http\Response $response |
38
|
|
|
* @param int $cacheDuration |
39
|
|
|
*/ |
40
|
|
|
protected function addCacheableHeader(Response $response, $cacheDuration) |
41
|
|
|
{ |
42
|
|
|
$duration = $this->getCacheDuration($cacheDuration); |
43
|
|
|
|
44
|
|
|
return $response->header($this->getConfig('cacheable_header'), '1') |
45
|
|
|
->header('Cache-Control', 'public, max-age=' . $duration); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add an ETag header to the current response. |
50
|
|
|
* |
51
|
|
|
* @param \Illuminate\Http\Response $response |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
protected function addEtagHeader(Response $response) |
55
|
|
|
{ |
56
|
|
|
if ($this->getConfig('use_etag')) { |
57
|
|
|
$response->setEtag(md5($response->getContent())); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Normalize the given cache duration and convert |
63
|
|
|
* it to seconds. |
64
|
|
|
* |
65
|
|
|
* @param int $duration |
66
|
|
|
* @return int |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
protected function getCacheDuration($duration) |
69
|
|
|
{ |
70
|
|
|
$cacheInMinutes = ((int) $duration > 0) ? $duration : $this->getConfig('cache_duration'); |
71
|
|
|
|
72
|
|
|
return $cacheInMinutes * 60; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Manipulate the current Http response. |
77
|
|
|
* |
78
|
|
|
* @param \Illuminate\Http\Response $response |
79
|
|
|
* @param int $cacheDuration |
80
|
|
|
* @return \Illuminate\Http\Response |
81
|
|
|
*/ |
82
|
|
|
public function manipulate(Response $response, $cacheDuration) |
83
|
|
|
{ |
84
|
|
|
$this->acknowledgeEsiSupport($response); |
85
|
|
|
|
86
|
|
|
if ($this->shouldNotCache($response)) { |
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->addCacheableHeader($response, (int) $cacheDuration); |
91
|
|
|
$this->addEtagHeader($response); |
92
|
|
|
|
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the current Http request headers. |
98
|
|
|
* |
99
|
|
|
* @param \Symfony\Component\HttpFoundation\HeaderBag $headers |
100
|
|
|
*/ |
101
|
|
|
public function setRequestHeaders(HeaderBag $headers) |
102
|
|
|
{ |
103
|
|
|
$this->requestHeaders = $headers; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if the current response shouldn't be cached. |
108
|
|
|
* |
109
|
|
|
* @param \Illuminate\Http\Response $response [description] |
110
|
|
|
* @return boolean |
|
|
|
|
111
|
|
|
*/ |
112
|
|
|
protected function shouldNotCache(Response $response) |
113
|
|
|
{ |
114
|
|
|
return $response->headers->get($this->getConfig('uncacheable_header')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get configuration value for a specific key. |
119
|
|
|
* |
120
|
|
|
* @param string $key |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
|
|
abstract public function getConfig($key); |
124
|
|
|
} |
125
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.