1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WebServCo\Framework; |
6
|
|
|
|
7
|
|
|
use WebServCo\Framework\Libraries\HtmlOutput; |
8
|
|
|
use WebServCo\Framework\Libraries\JsonOutput; |
9
|
|
|
|
10
|
|
|
abstract class AbstractOutputLoader implements \WebServCo\Framework\Interfaces\OutputLoaderInterface |
11
|
|
|
{ |
12
|
|
|
protected string $projectPath; |
13
|
|
|
protected HtmlOutput $htmlOutput; |
14
|
|
|
protected JsonOutput $jsonOutput; |
15
|
|
|
|
16
|
|
|
public function __construct(string $projectPath, HtmlOutput $htmlOutput, JsonOutput $jsonOutput) |
17
|
|
|
{ |
18
|
|
|
$this->projectPath = $projectPath; |
19
|
|
|
$this->htmlOutput = $htmlOutput; |
20
|
|
|
$this->jsonOutput = $jsonOutput; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function cli(string $string, bool $eol = true): bool |
24
|
|
|
{ |
25
|
|
|
echo $string; |
26
|
|
|
if ($eol) { |
27
|
|
|
echo \PHP_EOL; |
28
|
|
|
} |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array<int|string,mixed> $data |
34
|
|
|
*/ |
35
|
|
|
public function html(array $data, string $template): string |
36
|
|
|
{ |
37
|
|
|
$this->setHtmlTemplateData($data); |
38
|
|
|
return $this->getRenderedHtml($template); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function htmlOutput(): HtmlOutput |
42
|
|
|
{ |
43
|
|
|
return $this->htmlOutput; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array<int|string,mixed> $data |
48
|
|
|
*/ |
49
|
|
|
public function htmlPage(array $data, string $pageTemplate, ?string $mainTemplate = null): string |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Set template data. |
53
|
|
|
*/ |
54
|
|
|
$this->setHtmlTemplateData($data); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Partials |
58
|
|
|
*/ |
59
|
|
|
foreach ($this->htmlOutput()->setting('partials', []) as $partialTemplate) { |
60
|
|
|
$this->htmlOutput()->setData( |
61
|
|
|
"tpl_{$partialTemplate}", |
62
|
|
|
$this->getRenderedHtml($partialTemplate), |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* Page content |
67
|
|
|
*/ |
68
|
|
|
if (!\WebServCo\Framework\Helpers\StringHelper::isEmpty($pageTemplate)) { |
69
|
|
|
$this->htmlOutput()->setData( |
70
|
|
|
'tpl_content', |
71
|
|
|
$this->getRenderedHtml($pageTemplate), |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Main template |
77
|
|
|
*/ |
78
|
|
|
$mainTemplate ??= $this->htmlOutput()->setting('main_template', 'layout'); |
79
|
|
|
return $this->getRenderedHtml($mainTemplate); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array<string,mixed> $data |
84
|
|
|
*/ |
85
|
|
|
public function json(array $data): string |
86
|
|
|
{ |
87
|
|
|
if (\is_array($data)) { |
|
|
|
|
88
|
|
|
foreach ($data as $key => $value) { |
89
|
|
|
$this->jsonOutput()->setData($key, $value); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return $this->jsonOutput()->render(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function jsonOutput(): JsonOutput |
96
|
|
|
{ |
97
|
|
|
return $this->jsonOutput; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array<int|string,mixed> $data |
102
|
|
|
*/ |
103
|
|
|
protected function setHtmlTemplateData(array $data): bool |
104
|
|
|
{ |
105
|
|
|
if (!\is_array($data)) { |
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
foreach ($data as $key => $value) { |
109
|
|
|
$this->htmlOutput()->setData($key, $value); |
110
|
|
|
} |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function getRenderedHtml(string $template): string |
115
|
|
|
{ |
116
|
|
|
/** |
117
|
|
|
* Set template path. |
118
|
|
|
*/ |
119
|
|
|
$this->htmlOutput()->setPath("{$this->projectPath}resources/views/"); |
120
|
|
|
/** |
121
|
|
|
* Set page template |
122
|
|
|
*/ |
123
|
|
|
$this->htmlOutput()->setTemplate($template); |
124
|
|
|
return $this->htmlOutput()->render(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|