| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base view class. |
| 4 |
* |
| 5 |
* @package Calotes\Base |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Calotes\Base; |
| 9 |
|
| 10 |
/** |
| 11 |
* Base class for all views. |
| 12 |
*/ |
| 13 |
class View extends Component { |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Holds the blocks used in the view. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
public $blocks = array(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds parameters that can be passed to the view. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
public $params = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* The template file in which this view should be rendered. |
| 32 |
* |
| 33 |
* @var null |
| 34 |
*/ |
| 35 |
public $layout = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* The file contains content of this view, relative path. |
| 39 |
* |
| 40 |
* @var null |
| 41 |
*/ |
| 42 |
public $view_file = null; |
| 43 |
|
| 44 |
/** |
| 45 |
* The folder contains view files, absolute path. |
| 46 |
* |
| 47 |
* @var null |
| 48 |
*/ |
| 49 |
private $base_path = null; |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructor to set the base path of the view. |
| 53 |
* |
| 54 |
* @param mixed $base_path The base path of the view. |
| 55 |
*/ |
| 56 |
public function __construct( $base_path ) { |
| 57 |
$this->base_path = $base_path; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Render a view file. This will be used to render a whole page. |
| 62 |
* If a layout is defined, then we will render layout + view. |
| 63 |
* |
| 64 |
* @param string $view The name of the view file to render. |
| 65 |
* @param array $params An optional array of parameters to pass to the view file. |
| 66 |
* |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
public function render( $view, $params = array() ) { |
| 70 |
$view_file = $this->base_path . DIRECTORY_SEPARATOR . $view . '.php'; |
| 71 |
$real_base = realpath( $this->base_path ); |
| 72 |
$real_file = realpath( $view_file ); |
| 73 |
|
| 74 |
if ( false !== $real_base && false !== $real_file && str_starts_with( |
| 75 |
$real_file, |
| 76 |
rtrim( $real_base, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR |
| 77 |
) && is_file( $real_file ) |
| 78 |
) { |
| 79 |
return $this->render_php_file( $real_file, $params ); |
| 80 |
} |
| 81 |
|
| 82 |
return ''; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Renders a PHP file and returns its output. |
| 87 |
* |
| 88 |
* @param string $_view_file_path_ The path to the PHP file to render. |
| 89 |
* @param array $params An optional array of parameters to pass to the PHP file. |
| 90 |
* |
| 91 |
* @return string The output of the rendered PHP file. |
| 92 |
*/ |
| 93 |
private function render_php_file( $_view_file_path_, $params = array() ) { |
| 94 |
ob_start(); |
| 95 |
ob_implicit_flush( false ); |
| 96 |
$reserved_names = array( |
| 97 |
'_view_file_path_', |
| 98 |
'params', |
| 99 |
'param_name', |
| 100 |
'param_value', |
| 101 |
'reserved_names', |
| 102 |
'GLOBALS', |
| 103 |
'this', |
| 104 |
); |
| 105 |
foreach ( $params as $param_name => $param_value ) { |
| 106 |
if ( |
| 107 |
is_string( $param_name ) |
| 108 |
&& preg_match( '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $param_name ) |
| 109 |
&& ! in_array( $param_name, $reserved_names, true ) |
| 110 |
) { |
| 111 |
${$param_name} = $param_value; |
| 112 |
} |
| 113 |
} |
| 114 |
unset( $param_name, $param_value, $reserved_names ); |
| 115 |
require $_view_file_path_; |
| 116 |
|
| 117 |
return ob_get_clean(); |
| 118 |
} |
| 119 |
} |
| 120 |
|