3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Mailer\DeliverableMail;
7 use CoreFramework\Parser\Xml\XmlParser;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Response\Responseable;
10 use CoreFramework\Template\CompileableTemplate;
13 * The own template engine for loading caching and sending out images
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
20 * @todo This template engine does not make use of setTemplateType()
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
37 * Main nodes in the XML tree
39 private $mainNodes = array(
44 * Sub nodes in the XML tree
46 private $subNodes = array(
56 private $mailerInstance = NULL;
61 private $currMainNode = '';
64 * Protected constructor
68 protected function __construct () {
69 // Call parent constructor
70 parent::__construct(__CLASS__);
74 * Creates an instance of the class TemplateEngine and prepares it for usage
76 * @return $templateInstance An instance of TemplateEngine
77 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
78 * @throws InvalidBasePathStringException If $templateBasePath is no string
79 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
80 * directory or not found
81 * @throws BasePathReadProtectedException If $templateBasePath is
84 public static final function createMailTemplateEngine () {
86 $templateInstance = new MailTemplateEngine();
88 // Get the application instance from registry
89 $applicationInstance = Registry::getRegistry()->getInstance('app');
91 // Determine base path
92 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
94 // Is the base path valid?
95 if (empty($templateBasePath)) {
97 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
98 } elseif (!is_string($templateBasePath)) {
100 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
101 } elseif (!is_dir($templateBasePath)) {
103 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
104 } elseif (!is_readable($templateBasePath)) {
106 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
110 $templateInstance->setTemplateBasePath($templateBasePath);
112 // Set template extensions
113 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
114 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
116 // Absolute output path for compiled templates
117 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
119 // Return the prepared instance
120 return $templateInstance;
124 * Getter for current main node
126 * @return $currMainNode Current main node
128 public final function getCurrMainNode () {
129 return $this->currMainNode;
133 * Getter for main node array
135 * @return $mainNodes Array with valid main node names
137 public final function getMainNodes () {
138 return $this->mainNodes;
142 * Getter for sub node array
144 * @return $subNodes Array with valid sub node names
146 public final function getSubNodes () {
147 return $this->subNodes;
151 * Handles the start element of an XML resource
153 * @param $resource XML parser resource (currently ignored)
154 * @param $element The element we shall handle
155 * @param $attributes All attributes
157 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
159 public function startElement ($resource, $element, array $attributes) {
160 // Initial method name which will never be called...
161 $methodName = 'initEmail';
163 // Make the element name lower-case
164 $element = strtolower($element);
166 // Is the element a main node?
167 //* DEBUG: */ echo "START: >".$element."<<br />\n";
168 if (in_array($element, $this->getMainNodes())) {
169 // Okay, main node found!
170 $methodName = 'setEmail' . self::convertToClassName($element);
171 } elseif (in_array($element, $this->getSubNodes())) {
173 $methodName = 'setEmailProperty' . self::convertToClassName($element);
174 } elseif ($element != 'text-mail') {
175 // Invalid node name found
176 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
180 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
181 call_user_func_array(array($this, $methodName), $attributes);
185 * Ends the main or sub node by sending out the gathered data
187 * @param $resource An XML resource pointer (currently ignored)
188 * @param $nodeName Name of the node we want to finish
190 * @throws XmlNodeMismatchException If current main node mismatches the closing one
192 public function finishElement ($resource, $nodeName) {
193 // Make all lower-case
194 $nodeName = strtolower($nodeName);
196 // Does this match with current main node?
197 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
198 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
200 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
201 } elseif (in_array($nodeName, $this->getSubNodes())) {
202 // Silently ignore sub nodes
206 // Construct method name
207 $methodName = 'finish' . self::convertToClassName($nodeName);
209 // Call the corresponding method
210 call_user_func_array(array($this, $methodName), array());
214 * Adds the message text to the template engine
216 * @param $resource XML parser resource (currently ignored)
217 * @param $characters Characters to handle
220 public function characterHandler ($resource, $characters) {
221 // Trim all spaces away
222 $characters = trim($characters);
224 // Is this string empty?
225 if (empty($characters)) {
226 // Then skip it silently
230 // Add the message now
231 $this->assignVariable('message', $characters);
235 * Intializes the mail
238 * @todo Add cache creation here
240 private function initEmail () {
245 * Setter for mail data node
248 * @todo Should we call back the mailer class here?
250 private function setEmailMailData () {
251 // Set current main node
252 $this->currMainNode = 'mail-data';
256 * Setter for sender address property
258 * @param $senderAddress Sender address to set in email
261 private function setEmailPropertySenderAddress ($senderAddress) {
262 // Set the template variable
263 $this->assignVariable('sender', $senderAddress);
267 * Setter for recipient address property
269 * @param $recipientAddress Recipient address to set in email
272 private function setEmailPropertyRecipientAddress ($recipientAddress) {
273 // Set the template variable
274 $this->assignVariable('recipient', $recipientAddress);
278 * Setter for subject line property
282 private function setEmailPropertySubjectLine () {
287 * Method stub to avoid output
291 private function setEmailPropertyMessage () {
296 * Gets the template variable "message", stores it back as raw template data
297 * and compiles all variables so the mail message got prepared for output
301 private function finishMailData () {
302 // Get the message and set it as new raw template data back
303 $message = $this->readVariable('message');
304 $this->setRawTemplateData($message);
306 // Get some variables to compile
307 //$sender = $this->compileRawCode($this->readVariable('sender'));
308 //$this->assignVariable('sender', $sender);
310 // Then compile all variables
311 $this->compileVariables();
315 * Invokes the final mail process
319 private function finishTextMail () {
320 $this->getMailerInstance()->invokeMailDelivery();
324 * Getter for image cache file (FQFN)
326 * @return $fqfn Full-qualified file name of the image cache
329 public function getMailCacheFqfn () {
332 $this->debugBackTrace('Unfinished area!');
339 * Setter for mailer instance
341 * @param $mailerInstance A mailer instance
344 public final function setMailerInstance (DeliverableMail $mailerInstance) {
345 $this->mailerInstance = $mailerInstance;
349 * Getter for mailer instance
351 * @return $mailerInstance A mailer instance
353 protected final function getMailerInstance () {
354 return $this->mailerInstance;
358 * Outputs the mail to the world. This should only the mailer debug class do!
360 * @param $responseInstance An instance of a Responseable class
363 public function transferToResponse (Responseable $responseInstance) {
364 $responseInstance->writeToBody($this->getCompiledData());