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