|
| 1 | +<?php |
| 2 | +namespace Slim\HttpCache; |
| 3 | + |
| 4 | +use Pimple\Container; |
| 5 | +use Pimple\ServiceProviderInterface; |
| 6 | +use Psr\Http\Message\ResponseInterface; |
| 7 | + |
| 8 | +class CacheProvider implements ServiceProviderInterface |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Register this cache provider with a Pimple container |
| 12 | + * |
| 13 | + * @param Container $container |
| 14 | + */ |
| 15 | + public function register(Container $container) |
| 16 | + { |
| 17 | + $container['cache'] = $this; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Add `Expires` header to PSR7 response object |
| 22 | + * |
| 23 | + * @param ResponseInterface $response A PSR7 response object |
| 24 | + * @param int|string $time A UNIX timestamp or a valid `strtotime()` string |
| 25 | + * |
| 26 | + * @return ResponseInterface A new PSR7 response object with `Expires` header |
| 27 | + */ |
| 28 | + public function withExpires(ResponseInterface $response, $time) |
| 29 | + { |
| 30 | + if (!is_integer($time)) { |
| 31 | + $time = strtotime($time); |
| 32 | + if ($time === false) { |
| 33 | + throw new \InvalidArgumentException('Expiration value could not be parsed with `strtotime()`.'); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return $response->withHeader('Expires', gmdate('D, d M Y H:i:s T', $time)); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Add `ETag` header to PSR7 response object |
| 42 | + * |
| 43 | + * @param ResponseInterface $response A PSR7 response object |
| 44 | + * @param string $value The ETag value |
| 45 | + * @param string $type ETag type: "strong" or "weak" |
| 46 | + * |
| 47 | + * @return ResponseInterface A new PSR7 response object with `ETag` header |
| 48 | + */ |
| 49 | + public function withEtag(ResponseInterface $response, $value, $type = 'strong') |
| 50 | + { |
| 51 | + if (!in_array($type, ['strong', 'weak'])) { |
| 52 | + throw new \InvalidArgumentException('Invalid etag type. Must be "strong" or "weak".'); |
| 53 | + } |
| 54 | + $value = '"' . $value . '"'; |
| 55 | + if ($type === 'weak') { |
| 56 | + $value = 'W/' . $value; |
| 57 | + } |
| 58 | + |
| 59 | + return $response->withHeader('ETag', $value); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Add `Last-Modified` header to PSR7 response object |
| 64 | + * |
| 65 | + * @param ResponseInterface $response A PSR7 response object |
| 66 | + * @param int|string $time A UNIX timestamp or a valid `strtotime()` string |
| 67 | + * |
| 68 | + * @return ResponseInterface A new PSR7 response object with `Last-Modified` header |
| 69 | + */ |
| 70 | + public function withLastModified(ResponseInterface $response, $time) |
| 71 | + { |
| 72 | + if (!is_integer($time)) { |
| 73 | + $time = strtotime($time); |
| 74 | + if ($time === false) { |
| 75 | + throw new \InvalidArgumentException('Last Modified value could not be parsed with `strtotime()`.'); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return $response->withHeader('Last-Modified', gmdate('D, d M Y H:i:s T', $time)); |
| 80 | + } |
| 81 | +} |
0 commit comments