PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.3.2
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.3.2
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Page_Cache / Cache_Handler.php

Cache_Handler.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.3.2, at src/Performance/Page_Cache/Cache_Handler.php

164 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Append HTML generated by comment if debugging is DISABLED.
90 $output .= $this->debug->get_generated_by_comment();
91
92 // Don't save any fails that failed to compress their content.
93 try {
94 $compressed = $this->compressor->compress( $output );
95 } catch ( CompressionFailedException $e ) {
96 swpsp_log( sprintf( 'Failed to compress content using "%s" compression: %s', $this->compressor->encoding(), $e->getMessage() ) );
97
98 return;
99 }
100
101 $result = swpsp_direct_filesystem()->put_contents( $file, $compressed, swpsp_get_file_mode() );
102
103 if ( $result === false ) {
104 throw new RuntimeException( sprintf( 'File could not be saved to cache: %s', $file ) );
105 }
106 }
107
108 /**
109 * Converts a URL into a directory structure.
110 *
111 * All cached requests will be saved to a directory structure that matches
112 * the relative URL. This provides a way to consistently get the cached
113 * resource from the URL.
114 *
115 * @throws RuntimeException When URL does not have valid host.
116 *
117 * @return string
118 */
119 public function get_file_path(): string {
120 $path = $this->cache_path->get_path_from_url( $this->request->uri );
121 $ext = $this->compressor->extension();
122
123 return "$path.$ext";
124 }
125
126 /**
127 * Whether the current cache file exists and is not expired.
128 *
129 * @return bool
130 */
131 public function is_valid(): bool {
132 return file_exists( $this->get_file_path() ) && ! $this->expiration->file_expired( $this->get_file_path() );
133 }
134
135 /**
136 * Get the current compression strategy.
137 *
138 * @return Compressible
139 */
140 public function compressor(): Compressible {
141 return $this->compressor;
142 }
143
144 /**
145 * Make sure the complete directory we'll be saving our file to, actually exists.
146 *
147 * @throws RuntimeException If the cache directory can't be created.
148 *
149 * @return void
150 */
151 private function ensure_cache_dir_exists(): void {
152 $dir = dirname( $this->get_file_path() );
153
154 // If the file's directory structure does not exist yet, create it.
155 if ( ! is_dir( $dir ) ) {
156 $result = wp_mkdir_p( $dir );
157
158 if ( ! $result ) {
159 throw new RuntimeException( 'Unable to make directory: ' . $dir );
160 }
161 }
162 }
163 }
164