| 1 |
<?php |
| 2 |
/** |
| 3 |
* The swpsp-nginx.conf writer. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Cache_Delivery\Nginx; |
| 11 |
|
| 12 |
use RuntimeException; |
| 13 |
use SolidWP\Performance\Cache_Delivery\Contracts\Writable; |
| 14 |
use SolidWP\Performance\Filesystem\Filesystem; |
| 15 |
use SolidWP\Performance\Psr\Log\LoggerInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* The swpsp-nginx.conf writer. |
| 19 |
* |
| 20 |
* @package SolidWP\Performance |
| 21 |
*/ |
| 22 |
final class Writer implements Writable { |
| 23 |
|
| 24 |
/** |
| 25 |
* @var Nginx_Conf_File |
| 26 |
*/ |
| 27 |
private Nginx_Conf_File $nginx_conf; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var Filesystem |
| 31 |
*/ |
| 32 |
private Filesystem $filesystem; |
| 33 |
|
| 34 |
/** |
| 35 |
* @var LoggerInterface |
| 36 |
*/ |
| 37 |
private LoggerInterface $logger; |
| 38 |
|
| 39 |
/** |
| 40 |
* @param Nginx_Conf_File $nginx_conf The nginx conf object. |
| 41 |
* @param Filesystem $filesystem The filesystem component. |
| 42 |
* @param LoggerInterface $logger The logger. |
| 43 |
*/ |
| 44 |
public function __construct( |
| 45 |
Nginx_Conf_File $nginx_conf, |
| 46 |
Filesystem $filesystem, |
| 47 |
LoggerInterface $logger |
| 48 |
) { |
| 49 |
$this->nginx_conf = $nginx_conf; |
| 50 |
$this->filesystem = $filesystem; |
| 51 |
$this->logger = $logger; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Atomically write content to the swpsp-nginx.conf file. |
| 56 |
* |
| 57 |
* @param string $content The content to save to the swpsp-nginx.conf file. |
| 58 |
* |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
public function write( string $content ): bool { |
| 62 |
try { |
| 63 |
$filepath = $this->nginx_conf->get_file_path(); |
| 64 |
$this->filesystem->write( $filepath, $content ); |
| 65 |
} catch ( RuntimeException $e ) { |
| 66 |
$this->logger->error( |
| 67 |
'Unable to write content to swpsp-nginx.conf file', |
| 68 |
[ |
| 69 |
'exception' => $e, |
| 70 |
] |
| 71 |
); |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
return true; |
| 77 |
} |
| 78 |
} |
| 79 |
|