| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles managing cache files. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Page_Cache; |
| 11 |
|
| 12 |
use RuntimeException; |
| 13 |
use SolidWP\Performance\Http\Request; |
| 14 |
use SolidWP\Performance\Page_Cache\Compression\Collection; |
| 15 |
use SolidWP\Performance\Page_Cache\Compression\Contracts\Compressible; |
| 16 |
use SolidWP\Performance\Page_Cache\Compression\Exceptions\CompressionFailedException; |
| 17 |
|
| 18 |
/** |
| 19 |
* Handles managing cache files. |
| 20 |
* |
| 21 |
* @package SolidWP\Performance |
| 22 |
*/ |
| 23 |
final class Cache_Handler { |
| 24 |
|
| 25 |
/** |
| 26 |
* @var Request |
| 27 |
*/ |
| 28 |
private Request $request; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Compressible |
| 32 |
*/ |
| 33 |
private Compressible $compressor; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Debug |
| 37 |
*/ |
| 38 |
private Debug $debug; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var Expiration |
| 42 |
*/ |
| 43 |
private Expiration $expiration; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var Cache_Path |
| 47 |
*/ |
| 48 |
private Cache_Path $cache_path; |
| 49 |
|
| 50 |
/** |
| 51 |
* @param Collection $compressors The collection of compression algorithms. |
| 52 |
* @param Request $request The current request. |
| 53 |
* @param Debug $debug The debugger. |
| 54 |
* @param Expiration $expiration The expiration manager. |
| 55 |
* @param Cache_Path $cache_path The server path to the cache directory. |
| 56 |
*/ |
| 57 |
public function __construct( |
| 58 |
Collection $compressors, |
| 59 |
Request $request, |
| 60 |
Debug $debug, |
| 61 |
Expiration $expiration, |
| 62 |
Cache_Path $cache_path |
| 63 |
) { |
| 64 |
$this->request = $request; |
| 65 |
$this->compressor = $compressors->get_by_header( (string) $request->header->get( 'accept-encoding' ) ); |
| 66 |
$this->debug = $debug; |
| 67 |
$this->expiration = $expiration; |
| 68 |
$this->cache_path = $cache_path; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Save a file to the cache based on the type of compression the browser is asking for |
| 73 |
* and what the server supports. |
| 74 |
* |
| 75 |
* @param string $output Output to save in the cached file. |
| 76 |
* |
| 77 |
* @throws RuntimeException If the cache directory or cache file can't be created. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function save( string $output ): void { |
| 82 |
$this->ensure_cache_dir_exists(); |
| 83 |
|
| 84 |
$file = $this->get_file_path(); |
| 85 |
|
| 86 |
// Append HTML debug comment, if enabled. |
| 87 |
$output .= $this->debug->get_debug_comment( $this->request, $this->compressor ); |
| 88 |
|
| 89 |
// Don't save any fails that failed to compress their content. |
| 90 |
try { |
| 91 |
$compressed = $this->compressor->compress( $output ); |
| 92 |
} catch ( CompressionFailedException $e ) { |
| 93 |
swpsp_log( sprintf( 'Failed to compress content using "%s" compression: %s', $this->compressor->encoding(), $e->getMessage() ) ); |
| 94 |
|
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$result = swpsp_direct_filesystem()->put_contents( $file, $compressed, swpsp_get_file_mode() ); |
| 99 |
|
| 100 |
if ( $result === false ) { |
| 101 |
throw new RuntimeException( sprintf( 'File could not be saved to cache: %s', $file ) ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Converts a URL into a directory structure. |
| 107 |
* |
| 108 |
* All cached requests will be saved to a directory structure that matches |
| 109 |
* the relative URL. This provides a way to consistently get the cached |
| 110 |
* resource from the URL. |
| 111 |
* |
| 112 |
* @throws RuntimeException When URL does not have valid host. |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
public function get_file_path(): string { |
| 117 |
$path = $this->cache_path->get_path_from_url( $this->request->uri ); |
| 118 |
$ext = $this->compressor->extension(); |
| 119 |
|
| 120 |
return "$path.$ext"; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Whether the current cache file exists and is not expired. |
| 125 |
* |
| 126 |
* @return bool |
| 127 |
*/ |
| 128 |
public function is_valid(): bool { |
| 129 |
return file_exists( $this->get_file_path() ) && ! $this->expiration->file_expired( $this->get_file_path() ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get the current compression strategy. |
| 134 |
* |
| 135 |
* @return Compressible |
| 136 |
*/ |
| 137 |
public function compressor(): Compressible { |
| 138 |
return $this->compressor; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Make sure the complete directory we'll be saving our file to, actually exists. |
| 143 |
* |
| 144 |
* @throws RuntimeException If the cache directory can't be created. |
| 145 |
* |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
private function ensure_cache_dir_exists(): void { |
| 149 |
$dir = dirname( $this->get_file_path() ); |
| 150 |
|
| 151 |
// If the file's directory structure does not exist yet, create it. |
| 152 |
if ( ! is_dir( $dir ) ) { |
| 153 |
$result = wp_mkdir_p( $dir ); |
| 154 |
|
| 155 |
if ( ! $result ) { |
| 156 |
throw new RuntimeException( 'Unable to make directory: ' . $dir ); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|