-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6f386b
commit df06357
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Illuminate\Http\Middleware; | ||
|
||
use Closure; | ||
|
||
class SetCacheHeaders | ||
{ | ||
/** | ||
* Add cache related HTTP headers. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @param string|array $options | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function handle($request, Closure $next, $options = []) | ||
{ | ||
$response = $next($request); | ||
|
||
if (! $request->isMethodCacheable() || ! $response->getContent()) { | ||
return $response; | ||
} | ||
|
||
if (is_string($options)) { | ||
$options = $this->parseOptions($options); | ||
} | ||
|
||
if (isset($options['etag']) && $options['etag'] === true) { | ||
$options['etag'] = md5($response->getContent()); | ||
} | ||
|
||
$response->setCache($options); | ||
$response->isNotModified($request); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Parse the given header options. | ||
* | ||
* @param string $options | ||
* @return array | ||
*/ | ||
protected function parseOptions($options) | ||
{ | ||
return collect(explode(';', $options))->mapWithKeys(function ($option) { | ||
$data = explode('=', $option, 2); | ||
|
||
return [$data[0] => $data[1] ?? true]; | ||
})->all(); | ||
} | ||
} |