]> git.mxchange.org Git - core.git/blob - framework/main/classes/template/mail/class_MailTemplateEngine.php
Continued:
[core.git] / framework / main / classes / template / mail / class_MailTemplateEngine.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Template\Engine;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
8 use Org\Mxchange\CoreFramework\Mailer\DeliverableMail;
9 use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Response\Responseable;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
13 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
14
15 // Import SPL stuff
16 use \UnexpectedValueException;
17
18 /**
19  * The own template engine for loading caching and sending out images
20  *
21  * @author              Roland Haeder <webmaster@shipsimu.org>
22  * @version             0.0.0
23  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
24  * @license             GNU GPL 3.0 or any newer version
25  * @link                http://www.shipsimu.org
26  * @todo                This template engine does not make use of setTemplateType()
27  *
28  * This program is free software: you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation, either version 3 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program. If not, see <http://www.gnu.org/licenses/>.
40  */
41 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
42         /**
43          * Main nodes in the XML tree
44          */
45         private $mainNodes = array(
46                 'mail-data'
47         );
48
49         /**
50          * Sub nodes in the XML tree
51          */
52         private $subNodes = array(
53                 'subject-line',
54                 'sender-address',
55                 'recipient-address',
56                 'message'
57         );
58
59         /**
60          * Mailer instance
61          */
62         private $mailerInstance = NULL;
63
64         /**
65          * Current main node
66          */
67         private $currMainNode = '';
68
69         /**
70          * Protected constructor
71          *
72          * @return      void
73          */
74         protected function __construct () {
75                 // Call parent constructor
76                 parent::__construct(__CLASS__);
77         }
78
79         /**
80          * Creates an instance of the class TemplateEngine and prepares it for usage
81          *
82          * @return      $templateInstance               An instance of TemplateEngine
83          * @throws      UnexpectedValueException                If the provided $templateBasePath is empty or no string
84          * @throws      InvalidDirectoryException       If $templateBasePath is no
85          *                                                                                      directory or not found
86          * @throws      BasePathReadProtectedException  If $templateBasePath is
87          *                                                                                      read-protected
88          */
89         public static final function createMailTemplateEngine () {
90                 // Get a new instance
91                 $templateInstance = new MailTemplateEngine();
92
93                 // Get the application instance from registry
94                 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
95
96                 // Determine base path
97                 $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
98
99                 // Is the base path valid?
100                 if (empty($templateBasePath)) {
101                         // Base path is empty
102                         throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
103                 } elseif (!is_string($templateBasePath)) {
104                         // Is not a string
105                         throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
106                 } elseif (!is_dir($templateBasePath)) {
107                         // Is not a path
108                         throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
109                 } elseif (!is_readable($templateBasePath)) {
110                         // Is not readable
111                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
112                 }
113
114                 // Set the base path
115                 $templateInstance->setTemplateBasePath($templateBasePath);
116
117                 // Set template extensions
118                 $templateInstance->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
119                 $templateInstance->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('code_template_extension'));
120
121                 // Absolute output path for compiled templates
122                 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
123                         $templateBasePath,
124                         FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
125                 ));
126
127                 // Return the prepared instance
128                 return $templateInstance;
129         }
130
131         /**
132          * Getter for current main node
133          *
134          * @return      $currMainNode   Current main node
135          */
136         public final function getCurrMainNode () {
137                 return $this->currMainNode;
138         }
139
140         /**
141          * Getter for main node array
142          *
143          * @return      $mainNodes      Array with valid main node names
144          */
145         public final function getMainNodes () {
146                 return $this->mainNodes;
147         }
148
149         /**
150          * Getter for sub node array
151          *
152          * @return      $subNodes       Array with valid sub node names
153          */
154         public final function getSubNodes () {
155                 return $this->subNodes;
156         }
157
158         /**
159          * Handles the start element of an XML resource
160          *
161          * @param       $resource               XML parser resource (currently ignored)
162          * @param       $element                The element we shall handle
163          * @param       $attributes             All attributes
164          * @return      void
165          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
166          */
167         public function startElement ($resource, $element, array $attributes) {
168                 // Initial method name which will never be called...
169                 $methodName = 'initEmail';
170
171                 // Make the element name lower-case
172                 $element = strtolower($element);
173
174                 // Is the element a main node?
175                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
176                 if (in_array($element, $this->getMainNodes())) {
177                         // Okay, main node found!
178                         $methodName = 'setEmail' . self::convertToClassName($element);
179                 } elseif (in_array($element, $this->getSubNodes())) {
180                         // Sub node found
181                         $methodName = 'setEmailProperty' . self::convertToClassName($element);
182                 } elseif ($element != 'text-mail') {
183                         // Invalid node name found
184                         throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
185                 }
186
187                 // Call method
188                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
189                 call_user_func_array(array($this, $methodName), $attributes);
190         }
191
192         /**
193          * Ends the main or sub node by sending out the gathered data
194          *
195          * @param       $resource       An XML resource pointer (currently ignored)
196          * @param       $nodeName       Name of the node we want to finish
197          * @return      void
198          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
199          */
200         public function finishElement ($resource, $nodeName) {
201                 // Make all lower-case
202                 $nodeName = strtolower($nodeName);
203
204                 // Does this match with current main node?
205                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
206                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
207                         // Did not match!
208                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
209                 } elseif (in_array($nodeName, $this->getSubNodes())) {
210                         // Silently ignore sub nodes
211                         return;
212                 }
213
214                 // Construct method name
215                 $methodName = 'finish' . self::convertToClassName($nodeName);
216
217                 // Call the corresponding method
218                 call_user_func_array(array($this, $methodName), array());
219         }
220
221         /**
222          * Adds the message text to the template engine
223          *
224          * @param       $resource               XML parser resource (currently ignored)
225          * @param       $characters             Characters to handle
226          * @return      void
227          */
228         public function characterHandler ($resource, $characters) {
229                 // Trim all spaces away
230                 $characters = trim($characters);
231
232                 // Is this string empty?
233                 if (empty($characters)) {
234                         // Then skip it silently
235                         return;
236                 } // END - if
237
238                 // Add the message now
239                 $this->assignVariable('message', $characters);
240         }
241
242         /**
243          * Intializes the mail
244          *
245          * @return      void
246          * @todo        Add cache creation here
247          */
248         private function initEmail () {
249                 // Unfinished work!
250         }
251
252         /**
253          * Setter for mail data node
254          *
255          * @return      void
256          * @todo        Should we call back the mailer class here?
257          */
258         private function setEmailMailData () {
259                 // Set current main node
260                 $this->currMainNode = 'mail-data';
261         }
262
263         /**
264          * Setter for sender address property
265          *
266          * @param       $senderAddress  Sender address to set in email
267          * @return      void
268          */
269         private function setEmailPropertySenderAddress ($senderAddress) {
270                 // Set the template variable
271                 $this->assignVariable('sender', $senderAddress);
272         }
273
274         /**
275          * Setter for recipient address property
276          *
277          * @param       $recipientAddress       Recipient address to set in email
278          * @return      void
279          */
280         private function setEmailPropertyRecipientAddress ($recipientAddress) {
281                 // Set the template variable
282                 $this->assignVariable('recipient', $recipientAddress);
283         }
284
285         /**
286          * Setter for subject line property
287          *
288          * @return      void
289          */
290         private function setEmailPropertySubjectLine () {
291                 // Empty for now
292         }
293
294         /**
295          * Method stub to avoid output
296          *
297          * @return      void
298          */
299         private function setEmailPropertyMessage () {
300                 // Empty for now
301         }
302
303         /**
304          * Gets the template variable "message", stores it back as raw template data
305          * and compiles all variables so the mail message got prepared for output
306          *
307          * @return      void
308          */
309         private function finishMailData () {
310                 // Get the message and set it as new raw template data back
311                 $message = $this->readVariable('message');
312                 $this->setRawTemplateData($message);
313
314                 // Get some variables to compile
315                 //$sender = $this->compileRawCode($this->readVariable('sender'));
316                 //$this->assignVariable('sender', $sender);
317
318                 // Then compile all variables
319                 $this->compileVariables();
320         }
321
322         /**
323          * Invokes the final mail process
324          *
325          * @return      void
326          */
327         private function finishTextMail () {
328                 $this->getMailerInstance()->invokeMailDelivery();
329         }
330
331         /**
332          * Setter for mailer instance
333          *
334          * @param       $mailerInstance         A mailer instance
335          * @return      void
336          */
337         public final function setMailerInstance (DeliverableMail $mailerInstance) {
338                 $this->mailerInstance = $mailerInstance;
339         }
340
341         /**
342          * Getter for mailer instance
343          *
344          * @return      $mailerInstance         A mailer instance
345          */
346         protected final function getMailerInstance () {
347                 return $this->mailerInstance;
348         }
349
350         /**
351          * Outputs the mail to the world. This should only the mailer debug class do!
352          *
353          * @param       $responseInstance       An instance of a Responseable class
354          * @return      void
355          */
356         public function transferToResponse (Responseable $responseInstance) {
357                 $responseInstance->writeToBody($this->getCompiledData());
358         }
359
360 }