3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Registry\Generic\Registry;
7 use CoreFramework\Template\CompileableTemplate;
10 * The own template engine for loading caching and sending out images
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
17 * @todo This template engine does not make use of setTemplateType()
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
34 * Main nodes in the XML tree
36 private $mainNodes = array(
41 * Sub nodes in the XML tree
43 private $subNodes = array(
53 private $mailerInstance = NULL;
58 private $currMainNode = '';
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
71 * Creates an instance of the class TemplateEngine and prepares it for usage
73 * @return $templateInstance An instance of TemplateEngine
74 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
75 * @throws InvalidBasePathStringException If $templateBasePath is no string
76 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
77 * directory or not found
78 * @throws BasePathReadProtectedException If $templateBasePath is
81 public static final function createMailTemplateEngine () {
83 $templateInstance = new MailTemplateEngine();
85 // Get the application instance from registry
86 $applicationInstance = Registry::getRegistry()->getInstance('app');
88 // Determine base path
89 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
91 // Is the base path valid?
92 if (empty($templateBasePath)) {
94 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
95 } elseif (!is_string($templateBasePath)) {
97 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
98 } elseif (!is_dir($templateBasePath)) {
100 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
101 } elseif (!is_readable($templateBasePath)) {
103 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
107 $templateInstance->setTemplateBasePath($templateBasePath);
109 // Set template extensions
110 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
111 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
113 // Absolute output path for compiled templates
114 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
116 // Return the prepared instance
117 return $templateInstance;
121 * Getter for current main node
123 * @return $currMainNode Current main node
125 public final function getCurrMainNode () {
126 return $this->currMainNode;
130 * Getter for main node array
132 * @return $mainNodes Array with valid main node names
134 public final function getMainNodes () {
135 return $this->mainNodes;
139 * Getter for sub node array
141 * @return $subNodes Array with valid sub node names
143 public final function getSubNodes () {
144 return $this->subNodes;
148 * Handles the start element of an XML resource
150 * @param $resource XML parser resource (currently ignored)
151 * @param $element The element we shall handle
152 * @param $attributes All attributes
154 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
156 public function startElement ($resource, $element, array $attributes) {
157 // Initial method name which will never be called...
158 $methodName = 'initEmail';
160 // Make the element name lower-case
161 $element = strtolower($element);
163 // Is the element a main node?
164 //* DEBUG: */ echo "START: >".$element."<<br />\n";
165 if (in_array($element, $this->getMainNodes())) {
166 // Okay, main node found!
167 $methodName = 'setEmail' . self::convertToClassName($element);
168 } elseif (in_array($element, $this->getSubNodes())) {
170 $methodName = 'setEmailProperty' . self::convertToClassName($element);
171 } elseif ($element != 'text-mail') {
172 // Invalid node name found
173 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
177 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
178 call_user_func_array(array($this, $methodName), $attributes);
182 * Ends the main or sub node by sending out the gathered data
184 * @param $resource An XML resource pointer (currently ignored)
185 * @param $nodeName Name of the node we want to finish
187 * @throws XmlNodeMismatchException If current main node mismatches the closing one
189 public function finishElement ($resource, $nodeName) {
190 // Make all lower-case
191 $nodeName = strtolower($nodeName);
193 // Does this match with current main node?
194 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
195 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
197 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
198 } elseif (in_array($nodeName, $this->getSubNodes())) {
199 // Silently ignore sub nodes
203 // Construct method name
204 $methodName = 'finish' . self::convertToClassName($nodeName);
206 // Call the corresponding method
207 call_user_func_array(array($this, $methodName), array());
211 * Adds the message text to the template engine
213 * @param $resource XML parser resource (currently ignored)
214 * @param $characters Characters to handle
217 public function characterHandler ($resource, $characters) {
218 // Trim all spaces away
219 $characters = trim($characters);
221 // Is this string empty?
222 if (empty($characters)) {
223 // Then skip it silently
227 // Add the message now
228 $this->assignVariable('message', $characters);
232 * Intializes the mail
235 * @todo Add cache creation here
237 private function initEmail () {
242 * Setter for mail data node
245 * @todo Should we call back the mailer class here?
247 private function setEmailMailData () {
248 // Set current main node
249 $this->currMainNode = 'mail-data';
253 * Setter for sender address property
255 * @param $senderAddress Sender address to set in email
258 private function setEmailPropertySenderAddress ($senderAddress) {
259 // Set the template variable
260 $this->assignVariable('sender', $senderAddress);
264 * Setter for recipient address property
266 * @param $recipientAddress Recipient address to set in email
269 private function setEmailPropertyRecipientAddress ($recipientAddress) {
270 // Set the template variable
271 $this->assignVariable('recipient', $recipientAddress);
275 * Setter for subject line property
279 private function setEmailPropertySubjectLine () {
284 * Method stub to avoid output
288 private function setEmailPropertyMessage () {
293 * Gets the template variable "message", stores it back as raw template data
294 * and compiles all variables so the mail message got prepared for output
298 private function finishMailData () {
299 // Get the message and set it as new raw template data back
300 $message = $this->readVariable('message');
301 $this->setRawTemplateData($message);
303 // Get some variables to compile
304 //$sender = $this->compileRawCode($this->readVariable('sender'));
305 //$this->assignVariable('sender', $sender);
307 // Then compile all variables
308 $this->compileVariables();
312 * Invokes the final mail process
316 private function finishTextMail () {
317 $this->getMailerInstance()->invokeMailDelivery();
321 * Getter for image cache file (FQFN)
323 * @return $fqfn Full-qualified file name of the image cache
326 public function getMailCacheFqfn () {
329 $this->debugBackTrace('Unfinished area!');
336 * Setter for mailer instance
338 * @param $mailerInstance A mailer instance
341 public final function setMailerInstance (DeliverableMail $mailerInstance) {
342 $this->mailerInstance = $mailerInstance;
346 * Getter for mailer instance
348 * @return $mailerInstance A mailer instance
350 protected final function getMailerInstance () {
351 return $this->mailerInstance;
355 * Outputs the mail to the world. This should only the mailer debug class do!
357 * @param $responseInstance An instance of a Responseable class
360 public function transferToResponse (Responseable $responseInstance) {
361 $responseInstance->writeToBody($this->getCompiledData());