| 1 |
<?php |
| 2 |
/** |
| 3 |
* Loads a partial nginx.conf with replaced dynamic variables. |
| 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\Page_Cache\Cache_Path; |
| 14 |
use SolidWP\Performance\Page_Cache\Expiration; |
| 15 |
use SolidWP\Performance\View\Contracts\View; |
| 16 |
use SolidWP\Performance\View\Exceptions\FileNotFoundException; |
| 17 |
|
| 18 |
/** |
| 19 |
* Loads a partial nginx.conf with replaced dynamic variables. |
| 20 |
* |
| 21 |
* @package SolidWP\Performance |
| 22 |
*/ |
| 23 |
final class Template_Loader { |
| 24 |
|
| 25 |
public const VIEW = 'cache/nginx'; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var View |
| 29 |
*/ |
| 30 |
private View $view; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var Cache_Path |
| 34 |
*/ |
| 35 |
private Cache_Path $cache_path; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var Expiration |
| 39 |
*/ |
| 40 |
private Expiration $expiration; |
| 41 |
|
| 42 |
/** |
| 43 |
* @param View $view The view renderer. |
| 44 |
* @param Cache_Path $cache_path The cache path object. |
| 45 |
* @param Expiration $expiration The expiration object. |
| 46 |
*/ |
| 47 |
public function __construct( |
| 48 |
View $view, |
| 49 |
Cache_Path $cache_path, |
| 50 |
Expiration $expiration |
| 51 |
) { |
| 52 |
$this->view = $view; |
| 53 |
$this->cache_path = $cache_path; |
| 54 |
$this->expiration = $expiration; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get our nginx config template, with the dynamic values replaced. |
| 59 |
* |
| 60 |
* @throws FileNotFoundException If we can't find the view file. |
| 61 |
* @throws RuntimeException If we can't find the document root. |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
public function get(): string { |
| 66 |
$page_cache_dir = $this->cache_path->get_page_cache_dir(); |
| 67 |
|
| 68 |
$root = swpsp_get_document_root(); |
| 69 |
|
| 70 |
// e.g. "wp-content/cache/solid-performance/page". |
| 71 |
$cache_path = str_replace( $root, '', $page_cache_dir ); |
| 72 |
|
| 73 |
$args = [ |
| 74 |
'cache_path' => $cache_path, |
| 75 |
'expiration' => $this->expiration->get_expiration_length(), |
| 76 |
]; |
| 77 |
|
| 78 |
return $this->view->render_to_string( self::VIEW, $args ); |
| 79 |
} |
| 80 |
} |
| 81 |
|