reader = $reader; $this->writer = $writer; $this->nginx_conf = $nginx_conf; $this->filesystem = $filesystem; $this->template_loader = $template_loader; $this->bypass_template_loader = $bypass_template_loader; } /** * If this feature is supported by the current server environment. * * @return bool */ public function supported(): bool { // Allow Solid Security define override. if ( defined( 'ITSEC_SERVER_OVERRIDE' ) ) { return ITSEC_SERVER_OVERRIDE === 'nginx'; } global $is_nginx; return (bool) $is_nginx; } /** * Check if the swpsp-nginx.conf exists. * * @throws \RuntimeException If we can't find the document root. * * @return bool */ public function exists(): bool { return $this->nginx_conf->exists(); } /** * Get the path to the swpsp-nginx.conf file. * * @throws \RuntimeException If we can't find the document root. * * @return string */ public function path(): string { return $this->nginx_conf->get_file_path(); } /** * Create or update the swpsp-nginx.conf. * * @throws \RuntimeException If we can't find the document root. * @throws FileNotFoundException If the template loader can't find the view file. * * @return bool */ public function add(): bool { return $this->writer->write( $this->template_loader->get() ); } /** * Get the current rules. * * @throws CacheDeliveryReadException If we are unable to open or lock the swpsp-nginx.conf file for * reading. * * @return string */ public function get(): string { return $this->reader->read(); } /** * Write swpsp-nginx.conf rules to bypass caching. * * @throws FileNotFoundException If the template loader can't find the view file. * * @return bool */ public function bypass(): bool { return $this->writer->write( $this->bypass_template_loader->get() ); } /** * Whether our rules have populated swpsp-nginx.conf. * * @return bool */ public function has_rules(): bool { try { $rules = $this->get(); return str_contains( $rules, 'BEGIN SolidPerformanceNginxCacheRules' ); } catch ( CacheDeliveryReadException $e ) { return false; } } /** * Delete the swpsp-nginx.conf file. * * @throws IOException If we fail to remove the swpsp-nginx.conf. * @throws \RuntimeException If we can't find the document root. * * @return void */ public function remove(): void { $path = $this->nginx_conf->get_file_path(); $this->filesystem->remove( $path ); } }