3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Filesystem\InvalidDirectoryException;
7 use CoreFramework\Mailer\DeliverableMail;
8 use CoreFramework\Parser\Xml\XmlParser;
9 use CoreFramework\Registry\Registry;
10 use CoreFramework\Response\Responseable;
11 use CoreFramework\Template\CompileableTemplate;
14 use \UnexpectedValueException;
17 * The own template engine for loading caching and sending out images
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
24 * @todo This template engine does not make use of setTemplateType()
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
41 * Main nodes in the XML tree
43 private $mainNodes = array(
48 * Sub nodes in the XML tree
50 private $subNodes = array(
60 private $mailerInstance = NULL;
65 private $currMainNode = '';
68 * Protected constructor
72 protected function __construct () {
73 // Call parent constructor
74 parent::__construct(__CLASS__);
78 * Creates an instance of the class TemplateEngine and prepares it for usage
80 * @return $templateInstance An instance of TemplateEngine
81 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
82 * @throws InvalidDirectoryException If $templateBasePath is no
83 * directory or not found
84 * @throws BasePathReadProtectedException If $templateBasePath is
87 public static final function createMailTemplateEngine () {
89 $templateInstance = new MailTemplateEngine();
91 // Get the application instance from registry
92 $applicationInstance = Registry::getRegistry()->getInstance('app');
94 // Determine base path
95 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
97 // Is the base path valid?
98 if (empty($templateBasePath)) {
100 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
101 } elseif (!is_string($templateBasePath)) {
103 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
104 } elseif (!is_dir($templateBasePath)) {
106 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107 } elseif (!is_readable($templateBasePath)) {
109 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
113 $templateInstance->setTemplateBasePath($templateBasePath);
115 // Set template extensions
116 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
117 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
119 // Absolute output path for compiled templates
120 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
122 $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
125 // Return the prepared instance
126 return $templateInstance;
130 * Getter for current main node
132 * @return $currMainNode Current main node
134 public final function getCurrMainNode () {
135 return $this->currMainNode;
139 * Getter for main node array
141 * @return $mainNodes Array with valid main node names
143 public final function getMainNodes () {
144 return $this->mainNodes;
148 * Getter for sub node array
150 * @return $subNodes Array with valid sub node names
152 public final function getSubNodes () {
153 return $this->subNodes;
157 * Handles the start element of an XML resource
159 * @param $resource XML parser resource (currently ignored)
160 * @param $element The element we shall handle
161 * @param $attributes All attributes
163 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
165 public function startElement ($resource, $element, array $attributes) {
166 // Initial method name which will never be called...
167 $methodName = 'initEmail';
169 // Make the element name lower-case
170 $element = strtolower($element);
172 // Is the element a main node?
173 //* DEBUG: */ echo "START: >".$element."<<br />\n";
174 if (in_array($element, $this->getMainNodes())) {
175 // Okay, main node found!
176 $methodName = 'setEmail' . self::convertToClassName($element);
177 } elseif (in_array($element, $this->getSubNodes())) {
179 $methodName = 'setEmailProperty' . self::convertToClassName($element);
180 } elseif ($element != 'text-mail') {
181 // Invalid node name found
182 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
186 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
187 call_user_func_array(array($this, $methodName), $attributes);
191 * Ends the main or sub node by sending out the gathered data
193 * @param $resource An XML resource pointer (currently ignored)
194 * @param $nodeName Name of the node we want to finish
196 * @throws XmlNodeMismatchException If current main node mismatches the closing one
198 public function finishElement ($resource, $nodeName) {
199 // Make all lower-case
200 $nodeName = strtolower($nodeName);
202 // Does this match with current main node?
203 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
204 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
206 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
207 } elseif (in_array($nodeName, $this->getSubNodes())) {
208 // Silently ignore sub nodes
212 // Construct method name
213 $methodName = 'finish' . self::convertToClassName($nodeName);
215 // Call the corresponding method
216 call_user_func_array(array($this, $methodName), array());
220 * Adds the message text to the template engine
222 * @param $resource XML parser resource (currently ignored)
223 * @param $characters Characters to handle
226 public function characterHandler ($resource, $characters) {
227 // Trim all spaces away
228 $characters = trim($characters);
230 // Is this string empty?
231 if (empty($characters)) {
232 // Then skip it silently
236 // Add the message now
237 $this->assignVariable('message', $characters);
241 * Intializes the mail
244 * @todo Add cache creation here
246 private function initEmail () {
251 * Setter for mail data node
254 * @todo Should we call back the mailer class here?
256 private function setEmailMailData () {
257 // Set current main node
258 $this->currMainNode = 'mail-data';
262 * Setter for sender address property
264 * @param $senderAddress Sender address to set in email
267 private function setEmailPropertySenderAddress ($senderAddress) {
268 // Set the template variable
269 $this->assignVariable('sender', $senderAddress);
273 * Setter for recipient address property
275 * @param $recipientAddress Recipient address to set in email
278 private function setEmailPropertyRecipientAddress ($recipientAddress) {
279 // Set the template variable
280 $this->assignVariable('recipient', $recipientAddress);
284 * Setter for subject line property
288 private function setEmailPropertySubjectLine () {
293 * Method stub to avoid output
297 private function setEmailPropertyMessage () {
302 * Gets the template variable "message", stores it back as raw template data
303 * and compiles all variables so the mail message got prepared for output
307 private function finishMailData () {
308 // Get the message and set it as new raw template data back
309 $message = $this->readVariable('message');
310 $this->setRawTemplateData($message);
312 // Get some variables to compile
313 //$sender = $this->compileRawCode($this->readVariable('sender'));
314 //$this->assignVariable('sender', $sender);
316 // Then compile all variables
317 $this->compileVariables();
321 * Invokes the final mail process
325 private function finishTextMail () {
326 $this->getMailerInstance()->invokeMailDelivery();
330 * Getter for image cache file (FQFN)
332 * @return $fqfn Full-qualified file name of the image cache
335 public function getMailCacheFqfn () {
338 $this->debugBackTrace('Unfinished area!');
345 * Setter for mailer instance
347 * @param $mailerInstance A mailer instance
350 public final function setMailerInstance (DeliverableMail $mailerInstance) {
351 $this->mailerInstance = $mailerInstance;
355 * Getter for mailer instance
357 * @return $mailerInstance A mailer instance
359 protected final function getMailerInstance () {
360 return $this->mailerInstance;
364 * Outputs the mail to the world. This should only the mailer debug class do!
366 * @param $responseInstance An instance of a Responseable class
369 public function transferToResponse (Responseable $responseInstance) {
370 $responseInstance->writeToBody($this->getCompiledData());