| 1 |
<?php |
| 2 |
/** |
| 3 |
* The WordPress View Renderer. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\View\Renderers; |
| 11 |
|
| 12 |
use SolidWP\Performance\View\Contracts\View; |
| 13 |
use SolidWP\Performance\View\Exceptions\FileNotFoundException; |
| 14 |
use Throwable; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* The WordPress View Renderer. |
| 22 |
* |
| 23 |
* @package SolidWP\Performance |
| 24 |
*/ |
| 25 |
final class WordPress implements View { |
| 26 |
|
| 27 |
/** |
| 28 |
* The server path to the views folder. |
| 29 |
* |
| 30 |
* @var string |
| 31 |
* |
| 32 |
* @example /app/views/ |
| 33 |
*/ |
| 34 |
private string $directory; |
| 35 |
|
| 36 |
/** |
| 37 |
* The file extension of view files. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
* |
| 41 |
* @example .php |
| 42 |
*/ |
| 43 |
private string $extension; |
| 44 |
|
| 45 |
/** |
| 46 |
* @param string $directory The server path to the views folder. |
| 47 |
* @param string $extension The file extension of view files. |
| 48 |
*/ |
| 49 |
public function __construct( string $directory, string $extension = '.php' ) { |
| 50 |
$this->directory = rtrim( realpath( $directory ), '/\\' ) . '/'; |
| 51 |
$this->extension = $extension; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Renders a view. |
| 56 |
* |
| 57 |
* @example If the server path is /app/views, and you wish to load /app/views/admin/notice.php, |
| 58 |
* pass `admin/notice` as the view name. |
| 59 |
* |
| 60 |
* @param string $name The relative path/name of the view file without extension. |
| 61 |
* |
| 62 |
* @param mixed[] $args Arguments to be extracted and passed to the view. |
| 63 |
* |
| 64 |
* @throws FileNotFoundException|Throwable If the view file cannot be found. |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public function render( string $name, array $args = [] ): void { |
| 69 |
echo $this->render_to_string( $name, $args ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Renders a view and returns it as a string to be echoed. |
| 74 |
* |
| 75 |
* @example If the server path is /app/views, and you wish to load /app/views/admin/notice.php, |
| 76 |
* pass `admin/notice` as the view name. |
| 77 |
* |
| 78 |
* @param string $name The relative path/name of the view file without extension. |
| 79 |
* |
| 80 |
* @param mixed[] $args Arguments to be extracted and passed to the view. |
| 81 |
* |
| 82 |
* @throws FileNotFoundException|Throwable If the view file cannot be found. |
| 83 |
* |
| 84 |
* @return string |
| 85 |
*/ |
| 86 |
public function render_to_string( string $name, array $args = [] ): string { |
| 87 |
$file = $this->get_path( $name ); |
| 88 |
|
| 89 |
try { |
| 90 |
$level = ob_get_level(); |
| 91 |
ob_start(); |
| 92 |
|
| 93 |
extract( $args ); |
| 94 |
include $file; |
| 95 |
|
| 96 |
return (string) ob_get_clean(); |
| 97 |
} catch ( Throwable $e ) { |
| 98 |
while ( ob_get_level() > $level ) { |
| 99 |
ob_end_clean(); |
| 100 |
} |
| 101 |
|
| 102 |
throw $e; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get the absolute server path to a view file. |
| 108 |
* |
| 109 |
* @param string $name The relative view path/name, e.g. `admin/notice`. |
| 110 |
* |
| 111 |
* @throws FileNotFoundException If the view file cannot be found. |
| 112 |
* |
| 113 |
* @return string The absolute path to the view file. |
| 114 |
*/ |
| 115 |
private function get_path( string $name ): string { |
| 116 |
$file = $this->directory . $name . $this->extension; |
| 117 |
$path = realpath( $file ); |
| 118 |
|
| 119 |
if ( $path === false ) { |
| 120 |
throw new FileNotFoundException( |
| 121 |
sprintf( 'View file "%s" not found or not readable.', $file ) |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
return $path; |
| 126 |
} |
| 127 |
} |
| 128 |
|