PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.8.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.8.0
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_Path.php

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

183 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A single place to get cache path directories.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Page_Cache;
11
12 use FilesystemIterator;
13 use RecursiveDirectoryIterator;
14 use RecursiveIteratorIterator;
15 use RuntimeException;
16 use SolidWP\Performance\Page_Cache\Compression\Strategies\Brotli;
17 use SolidWP\Performance\Page_Cache\Compression\Strategies\Gzip;
18 use SolidWP\Performance\Page_Cache\Compression\Strategies\Html;
19 use SolidWP\Performance\Page_Cache\Compression\Strategies\Zstd;
20 use UnexpectedValueException;
21
22 /**
23 * A single place to get cache path directories.
24 *
25 * @package SolidWP\Performance
26 */
27 class Cache_Path {
28
29 /**
30 * @var Request_Scheme_Resolver
31 */
32 private Request_Scheme_Resolver $scheme_resolver;
33
34 /**
35 * The cache directory, e.g. /app/wp-content/cache/solid-performance.
36 *
37 * @var string
38 */
39 private string $cache_dir;
40
41 /**
42 * @param Request_Scheme_Resolver $scheme_resolver The http scheme resolver.
43 * @param string $cache_dir The cache directory, e.g. /app/wp-content/cache/solid-performance.
44 */
45 public function __construct( Request_Scheme_Resolver $scheme_resolver, string $cache_dir ) {
46 $this->scheme_resolver = $scheme_resolver;
47 $this->cache_dir = $cache_dir;
48 }
49
50 /**
51 * Returns the server path to the cache directory.
52 *
53 * @example /app/wp-content/cache/solid-performance
54 *
55 * @return string
56 */
57 public function get_cache_dir(): string {
58 return $this->cache_dir;
59 }
60
61 /**
62 * Get the path to where pages are cached.
63 *
64 * @example /app/wp-content/cache/solid-performance/page
65 *
66 * @return string
67 */
68 public function get_page_cache_dir(): string {
69 return $this->get_cache_dir() . '/page';
70 }
71
72 /**
73 * Get the host site cache directory.
74 *
75 * @example /app/wp-content/cache/solid-performance/page/www.wordpress.test
76 *
77 * @return string
78 */
79 public function get_site_cache_dir(): string {
80 $path = $this->get_page_cache_dir();
81 $site_host = wp_parse_url( get_site_url(), PHP_URL_HOST );
82
83
84 return $path . DIRECTORY_SEPARATOR . $site_host;
85 }
86
87 /**
88 * Get the HTTP scheme, e.g. https or http.
89 *
90 * @return string
91 */
92 public function get_scheme(): string {
93 return $this->scheme_resolver->get_scheme();
94 }
95
96 /**
97 * Converts a URL into a directory structure.
98 *
99 * All cached requests will be saved to a directory structure that matches
100 * the relative URL. This provides a way to consistently get the cached
101 * resource from the URL.
102 *
103 * @example /app/wp-content/cache/solid-performance/page/local.test.com/test-post/index-https
104 *
105 * @throws RuntimeException When URL does not have valid host.
106 *
107 * @param string $url The full URL of the request (e.g. https://solidwp.com/about).
108 *
109 * @return string
110 */
111 public function get_path_from_url( string $url ): string {
112 $url_parts = parse_url( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
113
114 if ( ! isset( $url_parts['host'] ) || $url_parts['host'] === '' ) {
115 throw new RuntimeException( 'URL needs a valid host.' );
116 }
117
118 $host = trim( $url_parts['host'], '/' );
119 $path = trim( $url_parts['path'] ?? '', '/' );
120 $scheme = $this->get_scheme();
121
122 $cache_path = "{$this->get_page_cache_dir()}/$host";
123
124 if ( $path !== '' ) {
125 $cache_path .= "/$path";
126 }
127
128 return "$cache_path/index-$scheme";
129 }
130
131 /**
132 * Recursively count how many cache files we have.
133 *
134 * We count 1 supported extension per directory as a single file.
135 *
136 * @param string $path The optional path.
137 *
138 * @return int
139 */
140 public function count( string $path = '' ): int {
141 if ( ! $path ) {
142 $path = $this->get_site_cache_dir();
143 }
144
145 if ( ! file_exists( $path ) ) {
146 return 0;
147 }
148
149 try {
150 $iterator = new RecursiveIteratorIterator(
151 new RecursiveDirectoryIterator( $path, FilesystemIterator::SKIP_DOTS )
152 );
153 } catch ( UnexpectedValueException $e ) {
154 return 0;
155 }
156
157 $extensions = [
158 Html::EXT,
159 Gzip::EXT,
160 Brotli::EXT,
161 Zstd::EXT,
162 ];
163
164 $seen = [];
165
166 /** @var FilesystemIterator $file */
167 foreach ( $iterator as $file ) {
168 $dir = $file->getPath();
169
170 // Only count 1 cache extension per directory once.
171 if ( isset( $seen[ $dir ] ) ) {
172 continue;
173 }
174
175 if ( in_array( $file->getExtension(), $extensions, true ) ) {
176 $seen[ $dir ] = true;
177 }
178 }
179
180 return count( $seen );
181 }
182 }
183