TemplateEngine.php
6 months ago
TemplateEngineException.php
5 years ago
TemplateEngineInterface.php
1 year ago
TemplateEngine.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\TemplateEngine; |
| 4 | |
| 5 | use DateTime; |
| 6 | use WPStaging\Core\WPStaging; |
| 7 | use WPStaging\Framework\Adapter\DateTimeAdapter; |
| 8 | use WPStaging\Framework\Assets\Assets; |
| 9 | |
| 10 | class TemplateEngine implements TemplateEngineInterface |
| 11 | { |
| 12 | /** |
| 13 | * Hook that is used to inject pro templates in UI. So they can be used by our JS |
| 14 | * @var string |
| 15 | */ |
| 16 | const HOOK_RENDER_PRO_TEMPLATES = 'wpstg.template.render_pro_templates'; |
| 17 | |
| 18 | /** @var string */ |
| 19 | const ACTION_AFTER_EXISTING_CLONES = 'wpstg.views.single_overview.after_existing_clones_actions'; |
| 20 | |
| 21 | /** @var string */ |
| 22 | const ACTION_MULTI_SITE_CLONE_OPTION = 'wpstg.views.ajax_clone.multi_site_clone_option'; |
| 23 | |
| 24 | /** @var string */ |
| 25 | const ACTION_BACKUP_TAB = 'wpstg.views.backup.tab_backup'; |
| 26 | |
| 27 | /** @var string|null Absolute path to the views directory. */ |
| 28 | protected $views; |
| 29 | |
| 30 | /** @var Assets */ |
| 31 | private $assets; |
| 32 | |
| 33 | public function __construct() |
| 34 | { |
| 35 | $this->assets = WPStaging::make(Assets::class); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param string $path |
| 40 | * @param array $params |
| 41 | * |
| 42 | * @return string |
| 43 | */ |
| 44 | public function render(string $path, array $params = []): string |
| 45 | { |
| 46 | if (!isset($this->views)) { |
| 47 | $this->views = WPSTG_VIEWS_DIR; |
| 48 | } |
| 49 | |
| 50 | $fullPath = WPSTG_VIEWS_DIR . $path; |
| 51 | if (!file_exists($fullPath)) { |
| 52 | throw new TemplateEngineException('Template not found: ' . $fullPath); |
| 53 | } |
| 54 | |
| 55 | extract($params, EXTR_SKIP); |
| 56 | ob_start(); |
| 57 | |
| 58 | /** @noinspection PhpIncludeInspection */ |
| 59 | require $fullPath; |
| 60 | $result = ob_get_clean(); |
| 61 | |
| 62 | return (string)$result; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return Assets |
| 67 | */ |
| 68 | public function getAssets() |
| 69 | { |
| 70 | return $this->assets; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return string |
| 75 | * @noinspection PhpUnused |
| 76 | */ |
| 77 | protected function getDateTimeFormat(): string |
| 78 | { |
| 79 | return (new DateTimeAdapter())->getDateTimeFormat(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param DateTime|null $dateTime |
| 84 | * |
| 85 | * @return string |
| 86 | */ |
| 87 | protected function transformToWpFormat($dateTime = null): string |
| 88 | { |
| 89 | if (!$dateTime) { |
| 90 | return ''; |
| 91 | } |
| 92 | |
| 93 | return (new DateTimeAdapter())->transformToWpFormat($dateTime); |
| 94 | } |
| 95 | } |
| 96 |