11<?php
2+ /**
3+ * Slim Framework (https://www.slimframework.com)
4+ *
5+ * @license https://github.com/slimphp/Slim-HttpCache/blob/master/LICENSE.md (MIT License)
6+ */
7+
8+ declare (strict_types=1 );
9+
210namespace Slim \HttpCache ;
311
4- use Psr \Http \Message \RequestInterface ;
512use Psr \Http \Message \ResponseInterface ;
6-
7- class Cache
13+ use Psr \Http \Message \ServerRequestInterface ;
14+ use Psr \Http \Server \MiddlewareInterface ;
15+ use Psr \Http \Server \RequestHandlerInterface ;
16+
17+ use function in_array ;
18+ use function is_array ;
19+ use function is_numeric ;
20+ use function preg_split ;
21+ use function reset ;
22+ use function sprintf ;
23+ use function strtotime ;
24+
25+ class Cache implements MiddlewareInterface
826{
927 /**
1028 * Cache-Control type (public or private)
@@ -34,44 +52,45 @@ class Cache
3452 * @param int $maxAge The maximum age of client-side cache
3553 * @param bool $mustRevalidate must-revalidate
3654 */
37- public function __construct ($ type = 'private ' , $ maxAge = 86400 , $ mustRevalidate = false )
55+ public function __construct (string $ type = 'private ' , int $ maxAge = 86400 , bool $ mustRevalidate = false )
3856 {
3957 $ this ->type = $ type ;
4058 $ this ->maxAge = $ maxAge ;
4159 $ this ->mustRevalidate = $ mustRevalidate ;
4260 }
4361
4462 /**
45- * Invoke cache middleware
46- *
47- * @param RequestInterface $request A PSR7 request object
48- * @param ResponseInterface $response A PSR7 response object
49- * @param callable $next The next middleware callable
50- *
51- * @return ResponseInterface A PSR7 response object
63+ * {@inheritDoc}
5264 */
53- public function __invoke ( RequestInterface $ request , ResponseInterface $ response , callable $ next )
65+ public function process ( ServerRequestInterface $ request , RequestHandlerInterface $ handler ): ResponseInterface
5466 {
55- $ response = $ next ($ request, $ response );
67+ $ response = $ handler -> handle ($ request );
5668
5769 // Cache-Control header
5870 if (!$ response ->hasHeader ('Cache-Control ' )) {
5971 if ($ this ->maxAge === 0 ) {
60- $ response = $ response ->withHeader ('Cache-Control ' , sprintf (
61- '%s, no-cache%s ' ,
62- $ this ->type ,
63- $ this ->mustRevalidate ? ', must-revalidate ' : ''
64- ));
72+ $ response = $ response ->withHeader (
73+ 'Cache-Control ' ,
74+ sprintf (
75+ '%s, no-cache%s ' ,
76+ $ this ->type ,
77+ $ this ->mustRevalidate ? ', must-revalidate ' : ''
78+ )
79+ );
6580 } else {
66- $ response = $ response ->withHeader ('Cache-Control ' , sprintf (
67- '%s, max-age=%s%s ' ,
68- $ this ->type ,
69- $ this ->maxAge ,
70- $ this ->mustRevalidate ? ', must-revalidate ' : ''
71- ));
81+ $ response = $ response ->withHeader (
82+ 'Cache-Control ' ,
83+ sprintf (
84+ '%s, max-age=%s%s ' ,
85+ $ this ->type ,
86+ $ this ->maxAge ,
87+ $ this ->mustRevalidate ? ', must-revalidate ' : ''
88+ )
89+ );
7290 }
7391 }
7492
93+
7594 // ETag header and conditional GET check
7695 $ etag = $ response ->getHeader ('ETag ' );
7796 $ etag = reset ($ etag );
@@ -81,7 +100,7 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
81100
82101 if ($ ifNoneMatch ) {
83102 $ etagList = preg_split ('@\s*,\s*@ ' , $ ifNoneMatch );
84- if (in_array ($ etag , $ etagList ) || in_array ('* ' , $ etagList )) {
103+ if (is_array ( $ etagList ) && ( in_array ($ etag , $ etagList ) || in_array ('* ' , $ etagList) )) {
85104 return $ response ->withStatus (304 );
86105 }
87106 }
@@ -92,7 +111,7 @@ public function __invoke(RequestInterface $request, ResponseInterface $response,
92111 $ lastModified = $ response ->getHeaderLine ('Last-Modified ' );
93112
94113 if ($ lastModified ) {
95- if (!is_integer ($ lastModified )) {
114+ if (!is_numeric ($ lastModified )) {
96115 $ lastModified = strtotime ($ lastModified );
97116 }
98117
0 commit comments