1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace h4kuna\MailManager\Template; |
4
|
|
|
|
5
|
|
|
use Nette\Application\UI\ITemplate; |
6
|
|
|
|
7
|
|
|
class LayoutFactory |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** @var ITemplateFactory */ |
11
|
|
|
private $templateFactory; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
private $mailDir; |
15
|
|
|
|
16
|
|
|
/** @var Layout */ |
17
|
|
|
private $lastLayout; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $plainMacro; |
21
|
|
|
|
22
|
|
|
/** @var Layout[] */ |
23
|
|
|
private $netteLayouts; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
public function __construct(ITemplateFactory $templateFactory) |
27
|
|
|
{ |
28
|
|
|
$this->templateFactory = $templateFactory; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
public function setTemplateDir(string $path): void |
33
|
|
|
{ |
34
|
|
|
$this->mailDir = $path; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function setPlainMacro(string $plainMacro): void |
39
|
|
|
{ |
40
|
|
|
$this->plainMacro = $plainMacro; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
public function getLastLayout(): Layout |
45
|
|
|
{ |
46
|
|
|
return $this->lastLayout; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function createHtml($body): Layout |
51
|
|
|
{ |
52
|
|
|
return $this->createBody($body, true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public function createPlainText($body): Layout |
57
|
|
|
{ |
58
|
|
|
return $this->createBody($body, false); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
private function createBody($body, bool $html): Layout |
63
|
|
|
{ |
64
|
|
|
$file = is_string($body) ? $this->checkFile($body) : null; |
65
|
|
|
|
66
|
|
|
if ($file === null) { |
67
|
|
|
$layout = $this->createLayoutClass(); |
68
|
|
|
if ($html) { |
69
|
|
|
$layout->setHtml($body); |
70
|
|
|
} else { |
71
|
|
|
$layout->setPlain($body); |
72
|
|
|
} |
73
|
|
|
} else { |
74
|
|
|
$layout = $this->createLayout($file, $body); |
75
|
|
|
} |
76
|
|
|
return $this->lastLayout = $layout; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
private function createLayout(string $file, string $fileName): Layout |
81
|
|
|
{ |
82
|
|
|
if (isset($this->netteLayouts[$file])) { |
83
|
|
|
return $this->netteLayouts[$file]; |
84
|
|
|
} |
85
|
|
|
$layout = $this->createLayoutClass(); |
86
|
|
|
$layout->setHtml($this->createNetteTemplate($file)); |
87
|
|
|
|
88
|
|
|
$plain = str_replace('{file}', $fileName, $this->plainMacro); |
89
|
|
|
$plainFile = $this->checkFile($plain); |
90
|
|
|
if ($plainFile) { |
91
|
|
|
$layout->setPlain($this->createNetteTemplate($plainFile)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $layout; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
private function createLayoutClass(): Layout |
99
|
|
|
{ |
100
|
|
|
return new Layout; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
private function createNetteTemplate(string $file): ITemplate |
105
|
|
|
{ |
106
|
|
|
$template = $this->templateFactory->create(); |
107
|
|
|
$template->setFile($file); |
108
|
|
|
return $template; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
private function checkFile(string $filePath): ?string |
113
|
|
|
{ |
114
|
|
|
$file = $this->mailDir . DIRECTORY_SEPARATOR . $filePath . '.latte'; |
115
|
|
|
if ($this->mailDir && is_file($file)) { |
116
|
|
|
return $file; |
117
|
|
|
} elseif (is_file($filePath)) { |
118
|
|
|
return $filePath; |
119
|
|
|
} |
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|