3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Parser\Xml\XmlParser;
7 use CoreFramework\Registry\Registry;
8 use CoreFramework\Response\Responseable;
9 use CoreFramework\Template\CompileableTemplate;
12 * The own template engine for loading caching and sending out images
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
19 * @todo This template engine does not make use of setTemplateType()
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
36 * Main nodes in the XML tree
38 private $mainNodes = array(
43 * Sub nodes in the XML tree
45 private $subNodes = array(
55 private $mailerInstance = NULL;
60 private $currMainNode = '';
63 * Protected constructor
67 protected function __construct () {
68 // Call parent constructor
69 parent::__construct(__CLASS__);
73 * Creates an instance of the class TemplateEngine and prepares it for usage
75 * @return $templateInstance An instance of TemplateEngine
76 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
77 * @throws InvalidBasePathStringException If $templateBasePath is no string
78 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
79 * directory or not found
80 * @throws BasePathReadProtectedException If $templateBasePath is
83 public static final function createMailTemplateEngine () {
85 $templateInstance = new MailTemplateEngine();
87 // Get the application instance from registry
88 $applicationInstance = Registry::getRegistry()->getInstance('app');
90 // Determine base path
91 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
93 // Is the base path valid?
94 if (empty($templateBasePath)) {
96 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
97 } elseif (!is_string($templateBasePath)) {
99 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
100 } elseif (!is_dir($templateBasePath)) {
102 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
103 } elseif (!is_readable($templateBasePath)) {
105 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
109 $templateInstance->setTemplateBasePath($templateBasePath);
111 // Set template extensions
112 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
113 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
115 // Absolute output path for compiled templates
116 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
118 // Return the prepared instance
119 return $templateInstance;
123 * Getter for current main node
125 * @return $currMainNode Current main node
127 public final function getCurrMainNode () {
128 return $this->currMainNode;
132 * Getter for main node array
134 * @return $mainNodes Array with valid main node names
136 public final function getMainNodes () {
137 return $this->mainNodes;
141 * Getter for sub node array
143 * @return $subNodes Array with valid sub node names
145 public final function getSubNodes () {
146 return $this->subNodes;
150 * Handles the start element of an XML resource
152 * @param $resource XML parser resource (currently ignored)
153 * @param $element The element we shall handle
154 * @param $attributes All attributes
156 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
158 public function startElement ($resource, $element, array $attributes) {
159 // Initial method name which will never be called...
160 $methodName = 'initEmail';
162 // Make the element name lower-case
163 $element = strtolower($element);
165 // Is the element a main node?
166 //* DEBUG: */ echo "START: >".$element."<<br />\n";
167 if (in_array($element, $this->getMainNodes())) {
168 // Okay, main node found!
169 $methodName = 'setEmail' . self::convertToClassName($element);
170 } elseif (in_array($element, $this->getSubNodes())) {
172 $methodName = 'setEmailProperty' . self::convertToClassName($element);
173 } elseif ($element != 'text-mail') {
174 // Invalid node name found
175 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
179 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
180 call_user_func_array(array($this, $methodName), $attributes);
184 * Ends the main or sub node by sending out the gathered data
186 * @param $resource An XML resource pointer (currently ignored)
187 * @param $nodeName Name of the node we want to finish
189 * @throws XmlNodeMismatchException If current main node mismatches the closing one
191 public function finishElement ($resource, $nodeName) {
192 // Make all lower-case
193 $nodeName = strtolower($nodeName);
195 // Does this match with current main node?
196 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
197 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
199 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
200 } elseif (in_array($nodeName, $this->getSubNodes())) {
201 // Silently ignore sub nodes
205 // Construct method name
206 $methodName = 'finish' . self::convertToClassName($nodeName);
208 // Call the corresponding method
209 call_user_func_array(array($this, $methodName), array());
213 * Adds the message text to the template engine
215 * @param $resource XML parser resource (currently ignored)
216 * @param $characters Characters to handle
219 public function characterHandler ($resource, $characters) {
220 // Trim all spaces away
221 $characters = trim($characters);
223 // Is this string empty?
224 if (empty($characters)) {
225 // Then skip it silently
229 // Add the message now
230 $this->assignVariable('message', $characters);
234 * Intializes the mail
237 * @todo Add cache creation here
239 private function initEmail () {
244 * Setter for mail data node
247 * @todo Should we call back the mailer class here?
249 private function setEmailMailData () {
250 // Set current main node
251 $this->currMainNode = 'mail-data';
255 * Setter for sender address property
257 * @param $senderAddress Sender address to set in email
260 private function setEmailPropertySenderAddress ($senderAddress) {
261 // Set the template variable
262 $this->assignVariable('sender', $senderAddress);
266 * Setter for recipient address property
268 * @param $recipientAddress Recipient address to set in email
271 private function setEmailPropertyRecipientAddress ($recipientAddress) {
272 // Set the template variable
273 $this->assignVariable('recipient', $recipientAddress);
277 * Setter for subject line property
281 private function setEmailPropertySubjectLine () {
286 * Method stub to avoid output
290 private function setEmailPropertyMessage () {
295 * Gets the template variable "message", stores it back as raw template data
296 * and compiles all variables so the mail message got prepared for output
300 private function finishMailData () {
301 // Get the message and set it as new raw template data back
302 $message = $this->readVariable('message');
303 $this->setRawTemplateData($message);
305 // Get some variables to compile
306 //$sender = $this->compileRawCode($this->readVariable('sender'));
307 //$this->assignVariable('sender', $sender);
309 // Then compile all variables
310 $this->compileVariables();
314 * Invokes the final mail process
318 private function finishTextMail () {
319 $this->getMailerInstance()->invokeMailDelivery();
323 * Getter for image cache file (FQFN)
325 * @return $fqfn Full-qualified file name of the image cache
328 public function getMailCacheFqfn () {
331 $this->debugBackTrace('Unfinished area!');
338 * Setter for mailer instance
340 * @param $mailerInstance A mailer instance
343 public final function setMailerInstance (DeliverableMail $mailerInstance) {
344 $this->mailerInstance = $mailerInstance;
348 * Getter for mailer instance
350 * @return $mailerInstance A mailer instance
352 protected final function getMailerInstance () {
353 return $this->mailerInstance;
357 * Outputs the mail to the world. This should only the mailer debug class do!
359 * @param $responseInstance An instance of a Responseable class
362 public function transferToResponse (Responseable $responseInstance) {
363 $responseInstance->writeToBody($this->getCompiledData());