| 1 |
<?php |
| 2 |
namespace Depicter\Middleware; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Psr\Http\Message\ResponseInterface; |
| 7 |
use WPEmerge\Requests\RequestInterface; |
| 8 |
use WPEmerge\Responses\ResponseService; |
| 9 |
|
| 10 |
class CacheMiddleware |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* Response service. |
| 15 |
* |
| 16 |
* @var ResponseService |
| 17 |
*/ |
| 18 |
protected $responseService = null; |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor. |
| 22 |
* |
| 23 |
* @codeCoverageIgnore |
| 24 |
* @param ResponseService $responseService |
| 25 |
*/ |
| 26 |
public function __construct( ResponseService $responseService ) { |
| 27 |
$this->responseService = $responseService; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @param RequestInterface $request |
| 32 |
* @param $next |
| 33 |
* @param int $expiration Expiration time in seconds |
| 34 |
* @param string $module |
| 35 |
* @param string $allowedMethod pass 'ANY or ALL or *' for all methods |
| 36 |
* |
| 37 |
* @return ResponseInterface |
| 38 |
* @throws \Psr\SimpleCache\InvalidArgumentException |
| 39 |
*/ |
| 40 |
public function handle( RequestInterface $request, $next, $expiration = 60, $module = 'api', $allowedMethod = '*' ) { |
| 41 |
|
| 42 |
// only continue for allowed methods |
| 43 |
if( ! in_array( $allowedMethod, [ $request->getMethod(), 'ANY', 'ALL', '*' ] ) ){ |
| 44 |
return $next( $request ); |
| 45 |
} |
| 46 |
|
| 47 |
// Append Cache_control and Access-Control to response ( $next( $request ) ) |
| 48 |
if( $module === 'browser' ){ |
| 49 |
// To flush cache we dont set cache-control and let WordPress send nocache headers |
| 50 |
if( $this->hasForceFlush( $request ) ){ |
| 51 |
return $next( $request ); |
| 52 |
} |
| 53 |
|
| 54 |
// disable WordPress nocache header for this request |
| 55 |
add_filter( 'nocache_headers', 'depicter_disable_nocache_headers' ); |
| 56 |
|
| 57 |
return $next( $request )->withHeader( 'Cache-Control', 'max-age=' . $expiration ) |
| 58 |
->withHeader( 'Access-Control-Allow-Origin' , '*' ); |
| 59 |
} |
| 60 |
|
| 61 |
$cache = \Depicter::cache( $module ); |
| 62 |
$key = $cache->hashRequest( $request ); |
| 63 |
|
| 64 |
// flush cache |
| 65 |
if( $this->hasForceFlush( $request ) ){ |
| 66 |
$cache->delete( $key ); |
| 67 |
|
| 68 |
// read and return cache if exists |
| 69 |
} elseif ( $cache->has( $key ) ) { |
| 70 |
|
| 71 |
// disable WordPress nocache header for this request |
| 72 |
add_filter( 'nocache_headers', 'depicter_disable_nocache_headers' ); |
| 73 |
|
| 74 |
$result = $cache->get( $key ); |
| 75 |
$response = ( is_array( $result ) || JSON::isJson( $result ) ) ? $this->responseService->json( $result ) : $this->responseService->output( $result ); |
| 76 |
|
| 77 |
return $response->withHeader( 'Cache-Control', 'max-age=' . $expiration ) |
| 78 |
->withHeader( 'Access-Control-Allow-Origin' , '*' ); |
| 79 |
} |
| 80 |
|
| 81 |
return $next( $request ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Whether to flush cache or not |
| 86 |
* |
| 87 |
* @param RequestInterface $request |
| 88 |
* |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
protected function hasForceFlush( RequestInterface $request ){ |
| 92 |
return $request->query('flush') == 'true'; |
| 93 |
} |
| 94 |
} |
| 95 |
|