1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Core |
7
|
|
|
* @see Mailcode |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
use AppUtils\ConvertHelper; |
15
|
|
|
use AppUtils\FileHelper\FolderInfo; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Main hub for the "Mailcode" syntax handling, which is used |
20
|
|
|
* to abstract the actual command syntax used by the selected |
21
|
|
|
* mailing format. |
22
|
|
|
* |
23
|
|
|
* Users only work with the mailcode commands to ensure that |
24
|
|
|
* the mail editor interface stays independent of the actual |
25
|
|
|
* format implementation used by the backend systems. |
26
|
|
|
* |
27
|
|
|
* @package Mailcode |
28
|
|
|
* @subpackage Core |
29
|
|
|
* @author Sebastian Mordziol <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Mailcode |
32
|
|
|
{ |
33
|
|
|
public const PACKAGE_NAME = 'Mailcode'; |
34
|
|
|
|
35
|
|
|
protected ?Mailcode_Parser $parser = null; |
36
|
|
|
protected ?Mailcode_Commands $commands = null; |
37
|
|
|
protected ?Mailcode_Variables $variables = null; |
38
|
|
|
|
39
|
|
|
private static ?FolderInfo $cacheFolder = null; |
40
|
|
|
|
41
|
|
|
public static function setCacheFolder(FolderInfo $cacheFolder) : void |
42
|
|
|
{ |
43
|
|
|
self::$cacheFolder = $cacheFolder; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function getCacheFolder() : FolderInfo |
47
|
|
|
{ |
48
|
|
|
if(isset(self::$cacheFolder)) { |
49
|
|
|
return self::$cacheFolder; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
throw new Mailcode_Exception( |
53
|
|
|
'The cache folder has not been set.', |
54
|
|
|
sprintf( |
55
|
|
|
'The cache folder must be set with [%s] before using the library.', |
56
|
|
|
ConvertHelper::callback2string(array(__CLASS__, 'setCacheFolder')) |
57
|
|
|
), |
58
|
|
|
Mailcode_Exception::ERROR_CACHE_FOLDER_NOT_SET |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Creates a new mailcode instance. |
64
|
|
|
* @return Mailcode |
65
|
|
|
*/ |
66
|
|
|
public static function create() : Mailcode |
67
|
|
|
{ |
68
|
|
|
return new Mailcode(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function getName() : string |
72
|
|
|
{ |
73
|
|
|
return self::PACKAGE_NAME; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Parses the string to detect all commands contained within. |
78
|
|
|
* |
79
|
|
|
* @param string $string |
80
|
|
|
* @return Mailcode_Collection |
81
|
|
|
*/ |
82
|
|
|
public function parseString(string $string) : Mailcode_Collection |
83
|
|
|
{ |
84
|
|
|
return $this->getParser() |
85
|
|
|
->parseString($string) |
86
|
|
|
->getCollection(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieves the string parser instance used to detect commands. |
91
|
|
|
* |
92
|
|
|
* @return Mailcode_Parser |
93
|
|
|
*/ |
94
|
|
|
public function getParser() : Mailcode_Parser |
95
|
|
|
{ |
96
|
|
|
if(!isset($this->parser)) |
97
|
|
|
{ |
98
|
|
|
$this->parser = new Mailcode_Parser($this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->parser; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Retrieves the commands collection, which is used to |
106
|
|
|
* access information on the available commands. |
107
|
|
|
* |
108
|
|
|
* @return Mailcode_Commands |
109
|
|
|
*/ |
110
|
|
|
public function getCommands() : Mailcode_Commands |
111
|
|
|
{ |
112
|
|
|
if(!isset($this->commands)) |
113
|
|
|
{ |
114
|
|
|
$this->commands = new Mailcode_Commands(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->commands; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function createSafeguard(string $subject) : Mailcode_Parser_Safeguard |
121
|
|
|
{ |
122
|
|
|
return $this->getParser()->createSafeguard($subject); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function createString(string $subject) : Mailcode_StringContainer |
126
|
|
|
{ |
127
|
|
|
return new Mailcode_StringContainer($subject); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Attempts to find all variables in the target string. |
132
|
|
|
* |
133
|
|
|
* @param string $subject |
134
|
|
|
* @param Mailcode_Commands_Command|null $sourceCommand |
135
|
|
|
* @return Mailcode_Variables_Collection_Regular |
136
|
|
|
*/ |
137
|
|
|
public function findVariables(string $subject, ?Mailcode_Commands_Command $sourceCommand=null) : Mailcode_Variables_Collection_Regular |
138
|
|
|
{ |
139
|
|
|
return $this->createVariables()->parseString($subject, $sourceCommand); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function createVariables() : Mailcode_Variables |
143
|
|
|
{ |
144
|
|
|
if(!isset($this->variables)) |
145
|
|
|
{ |
146
|
|
|
$this->variables = new Mailcode_Variables(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $this->variables; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Creates the translator, which can be used to convert commands |
154
|
|
|
* to another supported syntax. |
155
|
|
|
* |
156
|
|
|
* @return Mailcode_Translator |
157
|
|
|
*/ |
158
|
|
|
public function createTranslator() : Mailcode_Translator |
159
|
|
|
{ |
160
|
|
|
return Mailcode_Translator::create(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Creates the styler, which can be used to retrieve the |
165
|
|
|
* CSS required to style the highlighted commands in HTML. |
166
|
|
|
* |
167
|
|
|
* @return Mailcode_Styler |
168
|
|
|
*/ |
169
|
|
|
public function createStyler() : Mailcode_Styler |
170
|
|
|
{ |
171
|
|
|
return new Mailcode_Styler(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Creates a new pre-processor instance for the specified content |
176
|
|
|
* string, to replace all pre-process enabled commands with their |
177
|
|
|
* corresponding contents. |
178
|
|
|
* |
179
|
|
|
* @param string $subject |
180
|
|
|
* @return Mailcode_PreProcessor |
181
|
|
|
*/ |
182
|
|
|
public function createPreProcessor(string $subject) : Mailcode_PreProcessor |
183
|
|
|
{ |
184
|
|
|
return new Mailcode_PreProcessor($subject); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @var LoggerInterface|NULL |
189
|
|
|
*/ |
190
|
|
|
private static ?LoggerInterface $logger = null; |
191
|
|
|
private static bool $debug = false; |
192
|
|
|
|
193
|
|
|
public static function setLogger(LoggerInterface $logger) : void |
194
|
|
|
{ |
195
|
|
|
self::$logger = $logger; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public static function getLogger() : ?LoggerInterface |
199
|
|
|
{ |
200
|
|
|
return self::$logger; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public static function setDebugging(bool $enabled=true) : void |
204
|
|
|
{ |
205
|
|
|
self::$debug = $enabled; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public static function isDebugEnabled() : bool |
209
|
|
|
{ |
210
|
|
|
return self::$debug; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $message |
215
|
|
|
* @param array<string|int,mixed> $context |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
public static function debug(string $message, array $context=array()) : void |
219
|
|
|
{ |
220
|
|
|
if(self::$debug && isset(self::$logger)) |
221
|
|
|
{ |
222
|
|
|
self::$logger->debug($message, $context); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|