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 / Compression / Collection.php

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

153 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The collection of different compression strategies.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Page_Cache\Compression;
11
12 use SolidWP\Performance\Page_Cache\Compression\Contracts\Compressible;
13 use SolidWP\Performance\Page_Cache\Compression\Strategies\Html;
14
15 /**
16 * The collection of different compression strategies.
17 *
18 * @package SolidWP\Performance
19 */
20 final class Collection {
21
22 /**
23 * @var Compressible[]
24 */
25 private array $compressors;
26
27 /**
28 * Memoization cache for enabled compressors.
29 *
30 * @var Compressible[]|null
31 */
32 private ?array $enabled;
33
34 /**
35 * @param Compressible[] $compressors The cache compression strategies.
36 */
37 public function __construct( array $compressors ) {
38 $this->compressors = $compressors;
39 }
40
41 /**
42 * Get all compressors.
43 *
44 * @return Compressible[]
45 */
46 public function all(): array {
47 return $this->compressors;
48 }
49
50 /**
51 * Get an enabled compressor based on the order the browser requested it, using the accept encoding header.
52 *
53 * @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
54 *
55 * @param string $header The accept-encoding header e.g. `gzip, compress, br, zstd`, `br;q=1.0, gzip;q=0.8, *;q=0.1`.
56 *
57 * @return Compressible We will always fall back to the raw HTML compressor if none are found.
58 */
59 public function get_by_header( string $header ): Compressible {
60 $algorithms = explode( ',', $header );
61 $found = [];
62
63 foreach ( $algorithms as $algorithm ) {
64 $algorithm = trim( $algorithm );
65 $weight = 1.0;
66
67 // This header contains weighted algorithms.
68 if ( str_contains( $algorithm, ';q=' ) ) {
69 $weight = (float) str_replace( ';q=', '', strstr( $algorithm, ';q=' ) );
70 $algorithm = strtok( $algorithm, ';q=' );
71 }
72
73 $compressor = $this->get_by_encoding( $algorithm );
74
75 if ( ! $compressor ) {
76 continue;
77 }
78
79 $found[] = [
80 'compressor' => $compressor,
81 'weight' => $weight,
82 ];
83 }
84
85 // If no compression strategies are found, just return the raw HTML strategy.
86 if ( ! $found ) {
87 return $this->get_by_extension( Html::EXT );
88 }
89
90 // Sort algorithms by their weight.
91 usort( $found, static fn( array $a, array $b ) => $b['weight'] <=> $a['weight'] );
92
93 return $found[0]['compressor'];
94 }
95
96 /**
97 * Get an enabled compressor by encoding type.
98 *
99 * @param string $encoding The content encoding, e.g. gzip, br, zstd etc...
100 *
101 * @return Compressible|null
102 */
103 public function get_by_encoding( string $encoding ): ?Compressible {
104 if ( ! $encoding ) {
105 return null;
106 }
107
108 foreach ( $this->enabled() as $compressor ) {
109 if ( $compressor->encoding() === $encoding ) {
110 return $compressor;
111 }
112 }
113
114 return null;
115 }
116
117 /**
118 * Get an enabled compressor by file extension.
119 *
120 * @param string $extension The file extension, e.g. gz, br, html etc...
121 *
122 * @return Compressible|null
123 */
124 public function get_by_extension( string $extension ): ?Compressible {
125 if ( ! $extension ) {
126 return null;
127 }
128
129 foreach ( $this->enabled() as $compressor ) {
130 if ( $compressor->extension() === $extension ) {
131 return $compressor;
132 }
133 }
134
135 return null;
136 }
137
138 /**
139 * Get all enabled compressors.
140 *
141 * @return Compressible[]
142 */
143 public function enabled(): array {
144 if ( isset( $this->enabled ) ) {
145 return $this->enabled;
146 }
147
148 $this->enabled = array_filter( $this->compressors, static fn( Compressible $c ): bool => $c->enabled() );
149
150 return $this->enabled;
151 }
152 }
153