| 1 | <?php |
||
| 17 | class PHPEngine implements Engine |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @inheritdoc |
||
| 21 | */ |
||
| 22 | public function __invoke($template_pathname, $thisArg, array $variables, array $options = []) |
||
| 23 | { |
||
| 24 | $f = \Closure::bind(function($__TEMPLATE_PATHNAME__, $__VARIABLES__) { |
||
| 25 | |||
| 26 | unset($__VARIABLES__['this']); |
||
| 27 | |||
| 28 | extract($__VARIABLES__); |
||
| 29 | |||
| 30 | require $__TEMPLATE_PATHNAME__; |
||
| 31 | |||
| 32 | }, $this->ensure_is_object($thisArg)); |
||
| 33 | |||
| 34 | ob_start(); |
||
| 35 | |||
| 36 | try |
||
| 37 | { |
||
| 38 | $f($template_pathname, [ self::VAR_TEMPLATE_PATHNAME => $template_pathname ] + $variables); |
||
| 39 | |||
| 40 | return ob_get_clean(); |
||
| 41 | } |
||
| 42 | catch (\Exception $e) |
||
| 43 | { |
||
| 44 | ob_end_clean(); |
||
| 45 | |||
| 46 | throw $e; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Ensures that a value is an object. |
||
| 52 | * |
||
| 53 | * - `value` is an object, value is returned. |
||
| 54 | * - `value` is an array, an `ArrayObject` instance is returned. |
||
| 55 | * - Otherwise `value` is cast into a string and a {@link String} instance is returned. |
||
| 56 | * |
||
| 57 | * @param $value |
||
| 58 | * |
||
| 59 | * @return \ArrayObject|StringObject |
||
| 60 | */ |
||
| 61 | protected function ensure_is_object($value) |
||
| 75 | } |
||
| 76 |