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 / Zstd.php

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

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