Skip to content

Commit df06357

Browse files
committed
add files
1 parent f6f386b commit df06357

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Illuminate\Http\Middleware;
4+
5+
use Closure;
6+
7+
class SetCacheHeaders
8+
{
9+
/**
10+
* Add cache related HTTP headers.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
* @param string|array $options
15+
* @return \Symfony\Component\HttpFoundation\Response
16+
* @throws \InvalidArgumentException
17+
*/
18+
public function handle($request, Closure $next, $options = [])
19+
{
20+
$response = $next($request);
21+
22+
if (! $request->isMethodCacheable() || ! $response->getContent()) {
23+
return $response;
24+
}
25+
26+
if (is_string($options)) {
27+
$options = $this->parseOptions($options);
28+
}
29+
30+
if (isset($options['etag']) && $options['etag'] === true) {
31+
$options['etag'] = md5($response->getContent());
32+
}
33+
34+
$response->setCache($options);
35+
$response->isNotModified($request);
36+
37+
return $response;
38+
}
39+
40+
/**
41+
* Parse the given header options.
42+
*
43+
* @param string $options
44+
* @return array
45+
*/
46+
protected function parseOptions($options)
47+
{
48+
return collect(explode(';', $options))->mapWithKeys(function ($option) {
49+
$data = explode('=', $option, 2);
50+
51+
return [$data[0] => $data[1] ?? true];
52+
})->all();
53+
}
54+
}

0 commit comments

Comments
 (0)