TemplateEngine.php
1 year ago
TemplateEngineException.php
5 years ago
TemplateEngineInterface.php
2 years ago
TemplateEngine.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\TemplateEngine; |
| 4 | |
| 5 | use DateTime; |
| 6 | use WPStaging\Framework\Adapter\DateTimeAdapter; |
| 7 | |
| 8 | class TemplateEngine implements TemplateEngineInterface |
| 9 | { |
| 10 | /** |
| 11 | * Hook that is used to inject pro templates in UI. So they can be used by our JS |
| 12 | * @var string |
| 13 | */ |
| 14 | const HOOK_RENDER_PRO_TEMPLATES = 'wpstg.template.render_pro_templates'; |
| 15 | |
| 16 | /** @var string Absolute path to the views directory. */ |
| 17 | protected $views; |
| 18 | |
| 19 | /** |
| 20 | * @param string $path |
| 21 | * @param array $params |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | public function render(string $path, array $params = []): string |
| 26 | { |
| 27 | if (!isset($this->views)) { |
| 28 | $this->views = WPSTG_VIEWS_DIR; |
| 29 | } |
| 30 | |
| 31 | $fullPath = WPSTG_VIEWS_DIR . $path; |
| 32 | if (!file_exists($fullPath)) { |
| 33 | throw new TemplateEngineException('Template not found: ' . $fullPath); |
| 34 | } |
| 35 | |
| 36 | extract($params, EXTR_SKIP); |
| 37 | ob_start(); |
| 38 | |
| 39 | /** @noinspection PhpIncludeInspection */ |
| 40 | require $fullPath; |
| 41 | $result = ob_get_clean(); |
| 42 | |
| 43 | return (string)$result; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return string |
| 48 | * @noinspection PhpUnused |
| 49 | */ |
| 50 | protected function getDateTimeFormat(): string |
| 51 | { |
| 52 | return (new DateTimeAdapter())->getDateTimeFormat(); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param DateTime|null $dateTime |
| 57 | * |
| 58 | * @return string |
| 59 | */ |
| 60 | protected function transformToWpFormat($dateTime = null): string |
| 61 | { |
| 62 | if (!$dateTime) { |
| 63 | return ''; |
| 64 | } |
| 65 | |
| 66 | return (new DateTimeAdapter())->transformToWpFormat($dateTime); |
| 67 | } |
| 68 | } |
| 69 |