PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.5.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.5.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 / Compression / Strategies / Deflate.php

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

111 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Deflate compressor (zlib).
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Page_Cache\Compression\Strategies;
11
12 use InvalidArgumentException;
13 use SolidWP\Performance\Config\Config;
14 use SolidWP\Performance\Page_Cache\Compression\Contracts\Compressible;
15 use SolidWP\Performance\Page_Cache\Compression\Exceptions\CompressionFailedException;
16 use SolidWP\Performance\Page_Cache\Compression\Traits\With_Headers;
17
18 /**
19 * The Deflate compressor (zlib).
20 *
21 * @package SolidWP\Performance
22 */
23 final class Deflate implements Compressible {
24
25 use With_Headers;
26
27 public const EXT = 'df';
28
29 /**
30 * The deflate compression level.
31 *
32 * @var int
33 */
34 private int $level;
35
36 /**
37 * @var Config
38 */
39 private Config $config;
40
41 /**
42 * @param Config $config The config object.
43 * @param int $level The deflate compression level.
44 *
45 * @throws InvalidArgumentException Thrown if an invalid compression level is passed.
46 */
47 public function __construct( Config $config, int $level = 6 ) {
48 if ( $level < - 1 || $level > 9 ) {
49 throw new InvalidArgumentException( 'Deflate compression level must be between -1 and 9' );
50 }
51
52 $this->config = $config;
53 $this->level = $level;
54 }
55
56 /**
57 * Whether deflate is supported.
58 *
59 * @return bool
60 */
61 public function supported(): bool {
62 return function_exists( 'gzcompress' );
63 }
64
65 /**
66 * Whether this compressor is enabled.
67 *
68 * @return bool
69 */
70 public function enabled(): bool {
71 return $this->supported() && $this->config->get( 'page_cache.compression.enabled' );
72 }
73
74 /**
75 * The file extension.
76 *
77 * @return string
78 */
79 public function extension(): string {
80 return self::EXT;
81 }
82
83 /**
84 * The content encoding header value.
85 *
86 * @return string
87 */
88 public function encoding(): string {
89 return 'deflate';
90 }
91
92 /**
93 * Compress the content.
94 *
95 * @param string $content The content to compress.
96 *
97 * @throws CompressionFailedException When the strategy fails to compress the content.
98 *
99 * @return string
100 */
101 public function compress( string $content ): string {
102 $result = gzcompress( $content, $this->level );
103
104 if ( $result === false ) {
105 throw new CompressionFailedException( 'Failed to compress content with gzcompress' );
106 }
107
108 return $result;
109 }
110 }
111