| 1 |
<?php |
| 2 |
/** |
| 3 |
* The swpsp-nginx.conf reader. |
| 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\Readable; |
| 14 |
use SolidWP\Performance\Cache_Delivery\Exceptions\CacheDeliveryReadException; |
| 15 |
use SolidWP\Performance\Filesystem\Filesystem; |
| 16 |
|
| 17 |
/** |
| 18 |
* The swpsp-nginx.conf reader. |
| 19 |
* |
| 20 |
* @package SolidWP\Performance |
| 21 |
*/ |
| 22 |
final class Reader implements Readable { |
| 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 |
* @param Nginx_Conf_File $nginx_conf The nginx.conf file object. |
| 36 |
* @param Filesystem $filesystem The filesystem component. |
| 37 |
*/ |
| 38 |
public function __construct( |
| 39 |
Nginx_Conf_File $nginx_conf, |
| 40 |
Filesystem $filesystem |
| 41 |
) { |
| 42 |
$this->nginx_conf = $nginx_conf; |
| 43 |
$this->filesystem = $filesystem; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Acquire a read lock and read the contents of the swpsp-nginx.conf file. |
| 48 |
* |
| 49 |
* @throws CacheDeliveryReadException If we are unable to open or lock the swpsp-nginx.conf file for reading. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public function read(): string { |
| 54 |
try { |
| 55 |
if ( ! $this->nginx_conf->exists() ) { |
| 56 |
return ''; |
| 57 |
} |
| 58 |
|
| 59 |
return $this->filesystem->read( $this->nginx_conf->get_file_path() ); |
| 60 |
} catch ( RuntimeException $e ) { |
| 61 |
throw new CacheDeliveryReadException( $e->getMessage(), $e->getCode(), $e ); |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|