Просмотр файла vendor/laravel/framework/src/Illuminate/Http/Middleware/SetCacheHeaders.php

Размер файла: 1.66Kb
  1. <?php
  2.  
  3. namespace Illuminate\Http\Middleware;
  4.  
  5. use Closure;
  6. use Illuminate\Support\Carbon;
  7.  
  8. class SetCacheHeaders
  9. {
  10. /**
  11. * Add cache related HTTP headers.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @param \Closure $next
  15. * @param string|array $options
  16. * @return \Symfony\Component\HttpFoundation\Response
  17. *
  18. * @throws \InvalidArgumentException
  19. */
  20. public function handle($request, Closure $next, $options = [])
  21. {
  22. $response = $next($request);
  23.  
  24. if (! $request->isMethodCacheable() || ! $response->getContent()) {
  25. return $response;
  26. }
  27.  
  28. if (is_string($options)) {
  29. $options = $this->parseOptions($options);
  30. }
  31.  
  32. if (isset($options['etag']) && $options['etag'] === true) {
  33. $options['etag'] = md5($response->getContent());
  34. }
  35.  
  36. if (isset($options['last_modified'])) {
  37. if (is_numeric($options['last_modified'])) {
  38. $options['last_modified'] = Carbon::createFromTimestamp($options['last_modified']);
  39. } else {
  40. $options['last_modified'] = Carbon::parse($options['last_modified']);
  41. }
  42. }
  43.  
  44. $response->setCache($options);
  45. $response->isNotModified($request);
  46.  
  47. return $response;
  48. }
  49.  
  50. /**
  51. * Parse the given header options.
  52. *
  53. * @param string $options
  54. * @return array
  55. */
  56. protected function parseOptions($options)
  57. {
  58. return collect(explode(';', $options))->mapWithKeys(function ($option) {
  59. $data = explode('=', $option, 2);
  60.  
  61. return [$data[0] => $data[1] ?? true];
  62. })->all();
  63. }
  64. }