Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PhpResponse | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Miniframe\Response; |
| 4 | |
| 5 | use Miniframe\Core\Response; |
| 6 | |
| 7 | class PhpResponse extends Response |
| 8 | { |
| 9 | /** |
| 10 | * Path to the PHP file |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | private $phpFile; |
| 15 | |
| 16 | /** |
| 17 | * List of variables available in the scope of the PHP file |
| 18 | * |
| 19 | * @var mixed[] |
| 20 | */ |
| 21 | private $variables; |
| 22 | |
| 23 | /** |
| 24 | * Uses a PHP file as template. |
| 25 | * |
| 26 | * @param string $phpFile Path to the PHP file. |
| 27 | * @param mixed[] $variables List of variables available in the scope of the PHP file. |
| 28 | */ |
| 29 | public function __construct(string $phpFile, array $variables = array()) |
| 30 | { |
| 31 | $this->phpFile = $phpFile; |
| 32 | $this->variables = $variables; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Renders the PHP file and returns the output. |
| 37 | * |
| 38 | * @return string |
| 39 | */ |
| 40 | public function render(): string |
| 41 | { |
| 42 | extract($this->variables); |
| 43 | ob_start(); |
| 44 | require $this->phpFile; |
| 45 | return ob_get_clean() ?: ''; |
| 46 | } |
| 47 | } |