TemplateEngine.php
5 years ago
TemplateEngineException.php
5 years ago
TemplateEngineInterface.php
5 years ago
TemplateEngine.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | // TODO PHP7.x; declare(strict_types=1); |
| 4 | // TODO PHP7.x; type-hints & return types; |
| 5 | |
| 6 | namespace WPStaging\Framework\TemplateEngine; |
| 7 | |
| 8 | use DateTime; |
| 9 | use WPStaging\Framework\Adapter\DateTimeAdapter; |
| 10 | |
| 11 | class TemplateEngine implements TemplateEngineInterface |
| 12 | { |
| 13 | /** @var string Absolute path to the views directory. */ |
| 14 | protected $views; |
| 15 | |
| 16 | /** |
| 17 | * @param string $path |
| 18 | * @param array $params |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | public function render($path, array $params = []) |
| 23 | { |
| 24 | if (!isset($this->views)) { |
| 25 | $this->views = WPSTG_PLUGIN_DIR . 'Backend/views/'; |
| 26 | } |
| 27 | |
| 28 | $fullPath = WPSTG_PLUGIN_DIR . $path; |
| 29 | if (!file_exists($fullPath)) { |
| 30 | throw new TemplateEngineException('Template not found: ' . $fullPath); |
| 31 | } |
| 32 | |
| 33 | extract($params, EXTR_SKIP); |
| 34 | ob_start(); |
| 35 | |
| 36 | /** @noinspection PhpIncludeInspection */ |
| 37 | require $fullPath; |
| 38 | $result = ob_get_clean(); |
| 39 | |
| 40 | return (string)$result; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return string |
| 45 | * @noinspection PhpUnused |
| 46 | */ |
| 47 | protected function getDateTimeFormat() |
| 48 | { |
| 49 | return (new DateTimeAdapter())->getDateTimeFormat(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param DateTime|null $dateTime |
| 54 | * |
| 55 | * @return string |
| 56 | */ |
| 57 | protected function transformToWpFormat(DateTime $dateTime = null) |
| 58 | { |
| 59 | if (!$dateTime) { |
| 60 | return ''; |
| 61 | } |
| 62 | |
| 63 | return (new DateTimeAdapter())->transformToWpFormat($dateTime); |
| 64 | } |
| 65 | } |
| 66 |