| 1 |
<?php |
| 2 |
/** |
| 3 |
* The swpsp-nginx.conf manager. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Cache_Delivery\Nginx; |
| 11 |
|
| 12 |
use SolidWP\Performance\Cache_Delivery\Contracts\Readable; |
| 13 |
use SolidWP\Performance\Cache_Delivery\Contracts\Writable; |
| 14 |
use SolidWP\Performance\Cache_Delivery\Exceptions\CacheDeliveryReadException; |
| 15 |
use SolidWP\Performance\Symfony\Component\Filesystem\Exception\IOException; |
| 16 |
use SolidWP\Performance\Symfony\Component\Filesystem\Filesystem; |
| 17 |
use SolidWP\Performance\View\Exceptions\FileNotFoundException; |
| 18 |
|
| 19 |
/** |
| 20 |
* The swpsp-nginx.conf manager. |
| 21 |
* |
| 22 |
* @package SolidWP\Performance |
| 23 |
*/ |
| 24 |
final class Manager { |
| 25 |
|
| 26 |
/** |
| 27 |
* @var Readable |
| 28 |
*/ |
| 29 |
private Readable $reader; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var Writable |
| 33 |
*/ |
| 34 |
private Writable $writer; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var Nginx_Conf_File |
| 38 |
*/ |
| 39 |
private Nginx_Conf_File $nginx_conf; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var Filesystem |
| 43 |
*/ |
| 44 |
private Filesystem $filesystem; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var Template_Loader |
| 48 |
*/ |
| 49 |
private Template_Loader $template_loader; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var Bypass_Template_Loader |
| 53 |
*/ |
| 54 |
private Bypass_Template_Loader $bypass_template_loader; |
| 55 |
|
| 56 |
/** |
| 57 |
* @param Readable $reader The config file reader. |
| 58 |
* @param Writable $writer The config file writer. |
| 59 |
* @param Nginx_Conf_File $nginx_conf The nginx.conf object. |
| 60 |
* @param Filesystem $filesystem The filesystem component. |
| 61 |
* @param Template_Loader $template_loader The template loader. |
| 62 |
* @param Bypass_Template_Loader $bypass_template_loader The cache bypass template loader. |
| 63 |
*/ |
| 64 |
public function __construct( |
| 65 |
Readable $reader, |
| 66 |
Writable $writer, |
| 67 |
Nginx_Conf_File $nginx_conf, |
| 68 |
Filesystem $filesystem, |
| 69 |
Template_Loader $template_loader, |
| 70 |
Bypass_Template_Loader $bypass_template_loader |
| 71 |
) { |
| 72 |
$this->reader = $reader; |
| 73 |
$this->writer = $writer; |
| 74 |
$this->nginx_conf = $nginx_conf; |
| 75 |
$this->filesystem = $filesystem; |
| 76 |
$this->template_loader = $template_loader; |
| 77 |
$this->bypass_template_loader = $bypass_template_loader; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* If this feature is supported by the current server environment. |
| 82 |
* |
| 83 |
* @return bool |
| 84 |
*/ |
| 85 |
public function supported(): bool { |
| 86 |
// Allow Solid Security define override. |
| 87 |
if ( defined( 'ITSEC_SERVER_OVERRIDE' ) ) { |
| 88 |
return ITSEC_SERVER_OVERRIDE === 'nginx'; |
| 89 |
} |
| 90 |
|
| 91 |
global $is_nginx; |
| 92 |
|
| 93 |
return (bool) $is_nginx; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Check if the swpsp-nginx.conf exists. |
| 98 |
* |
| 99 |
* @throws \RuntimeException If we can't find the document root. |
| 100 |
* |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public function exists(): bool { |
| 104 |
return $this->nginx_conf->exists(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Get the path to the swpsp-nginx.conf file. |
| 109 |
* |
| 110 |
* @throws \RuntimeException If we can't find the document root. |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
public function path(): string { |
| 115 |
return $this->nginx_conf->get_file_path(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Create or update the swpsp-nginx.conf. |
| 120 |
* |
| 121 |
* @throws \RuntimeException If we can't find the document root. |
| 122 |
* @throws FileNotFoundException If the template loader can't find the view file. |
| 123 |
* |
| 124 |
* @return bool |
| 125 |
*/ |
| 126 |
public function add(): bool { |
| 127 |
return $this->writer->write( $this->template_loader->get() ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get the current rules. |
| 132 |
* |
| 133 |
* @throws CacheDeliveryReadException If we are unable to open or lock the swpsp-nginx.conf file for |
| 134 |
* reading. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
public function get(): string { |
| 139 |
return $this->reader->read(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Write swpsp-nginx.conf rules to bypass caching. |
| 144 |
* |
| 145 |
* @throws FileNotFoundException If the template loader can't find the view file. |
| 146 |
* |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
public function bypass(): bool { |
| 150 |
return $this->writer->write( $this->bypass_template_loader->get() ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Whether our rules have populated swpsp-nginx.conf. |
| 155 |
* |
| 156 |
* @return bool |
| 157 |
*/ |
| 158 |
public function has_rules(): bool { |
| 159 |
try { |
| 160 |
$rules = $this->get(); |
| 161 |
|
| 162 |
return str_contains( $rules, 'BEGIN KadencePerformanceNginxCacheRules' ) |
| 163 |
|| str_contains( $rules, 'BEGIN SolidPerformanceNginxCacheRules' ); |
| 164 |
} catch ( CacheDeliveryReadException $e ) { |
| 165 |
return false; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Delete the swpsp-nginx.conf file. |
| 171 |
* |
| 172 |
* @throws IOException If we fail to remove the swpsp-nginx.conf. |
| 173 |
* @throws \RuntimeException If we can't find the document root. |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
public function remove(): void { |
| 178 |
$path = $this->nginx_conf->get_file_path(); |
| 179 |
|
| 180 |
$this->filesystem->remove( $path ); |
| 181 |
} |
| 182 |
} |
| 183 |
|