3 namespace Org\Mxchange\CoreFramework\Template\Engine;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
7 use Org\Mxchange\CoreFramework\Mailer\DeliverableMail;
8 use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser;
9 use Org\Mxchange\CoreFramework\Registry\Registry;
10 use Org\Mxchange\CoreFramework\Response\Responseable;
11 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
12 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
15 use \UnexpectedValueException;
18 * The own template engine for loading caching and sending out images
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 <<<<<<< HEAD:framework/main/classes/template/mail/class_MailTemplateEngine.php
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
26 >>>>>>> Some updates::inc/main/classes/template/mail/class_MailTemplateEngine.php
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
29 * @todo This template engine does not make use of setTemplateType()
31 * This program is free software: you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation, either version 3 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program. If not, see <http://www.gnu.org/licenses/>.
44 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
46 * Main nodes in the XML tree
48 private $mainNodes = array(
53 * Sub nodes in the XML tree
55 private $subNodes = array(
65 private $mailerInstance = NULL;
70 private $currMainNode = '';
73 * Protected constructor
77 protected function __construct () {
78 // Call parent constructor
79 parent::__construct(__CLASS__);
83 * Creates an instance of the class TemplateEngine and prepares it for usage
85 * @return $templateInstance An instance of TemplateEngine
86 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
87 * @throws InvalidDirectoryException If $templateBasePath is no
88 * directory or not found
89 * @throws BasePathReadProtectedException If $templateBasePath is
92 public static final function createMailTemplateEngine () {
94 $templateInstance = new MailTemplateEngine();
96 // Get the application instance from registry
97 $applicationInstance = Registry::getRegistry()->getInstance('app');
99 // Determine base path
100 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
102 // Is the base path valid?
103 if (empty($templateBasePath)) {
104 // Base path is empty
105 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
106 } elseif (!is_string($templateBasePath)) {
108 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
109 } elseif (!is_dir($templateBasePath)) {
111 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
112 } elseif (!is_readable($templateBasePath)) {
114 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
118 $templateInstance->setTemplateBasePath($templateBasePath);
120 // Set template extensions
121 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
122 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
124 // Absolute output path for compiled templates
125 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
127 $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
130 // Return the prepared instance
131 return $templateInstance;
135 * Getter for current main node
137 * @return $currMainNode Current main node
139 public final function getCurrMainNode () {
140 return $this->currMainNode;
144 * Getter for main node array
146 * @return $mainNodes Array with valid main node names
148 public final function getMainNodes () {
149 return $this->mainNodes;
153 * Getter for sub node array
155 * @return $subNodes Array with valid sub node names
157 public final function getSubNodes () {
158 return $this->subNodes;
162 * Handles the start element of an XML resource
164 * @param $resource XML parser resource (currently ignored)
165 * @param $element The element we shall handle
166 * @param $attributes All attributes
168 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
170 public function startElement ($resource, $element, array $attributes) {
171 // Initial method name which will never be called...
172 $methodName = 'initEmail';
174 // Make the element name lower-case
175 $element = strtolower($element);
177 // Is the element a main node?
178 //* DEBUG: */ echo "START: >".$element."<<br />\n";
179 if (in_array($element, $this->getMainNodes())) {
180 // Okay, main node found!
181 $methodName = 'setEmail' . self::convertToClassName($element);
182 } elseif (in_array($element, $this->getSubNodes())) {
184 $methodName = 'setEmailProperty' . self::convertToClassName($element);
185 } elseif ($element != 'text-mail') {
186 // Invalid node name found
187 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
191 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
192 call_user_func_array(array($this, $methodName), $attributes);
196 * Ends the main or sub node by sending out the gathered data
198 * @param $resource An XML resource pointer (currently ignored)
199 * @param $nodeName Name of the node we want to finish
201 * @throws XmlNodeMismatchException If current main node mismatches the closing one
203 public function finishElement ($resource, $nodeName) {
204 // Make all lower-case
205 $nodeName = strtolower($nodeName);
207 // Does this match with current main node?
208 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
209 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
211 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
212 } elseif (in_array($nodeName, $this->getSubNodes())) {
213 // Silently ignore sub nodes
217 // Construct method name
218 $methodName = 'finish' . self::convertToClassName($nodeName);
220 // Call the corresponding method
221 call_user_func_array(array($this, $methodName), array());
225 * Adds the message text to the template engine
227 * @param $resource XML parser resource (currently ignored)
228 * @param $characters Characters to handle
231 public function characterHandler ($resource, $characters) {
232 // Trim all spaces away
233 $characters = trim($characters);
235 // Is this string empty?
236 if (empty($characters)) {
237 // Then skip it silently
241 // Add the message now
242 $this->assignVariable('message', $characters);
246 * Intializes the mail
249 * @todo Add cache creation here
251 private function initEmail () {
256 * Setter for mail data node
259 * @todo Should we call back the mailer class here?
261 private function setEmailMailData () {
262 // Set current main node
263 $this->currMainNode = 'mail-data';
267 * Setter for sender address property
269 * @param $senderAddress Sender address to set in email
272 private function setEmailPropertySenderAddress ($senderAddress) {
273 // Set the template variable
274 $this->assignVariable('sender', $senderAddress);
278 * Setter for recipient address property
280 * @param $recipientAddress Recipient address to set in email
283 private function setEmailPropertyRecipientAddress ($recipientAddress) {
284 // Set the template variable
285 $this->assignVariable('recipient', $recipientAddress);
289 * Setter for subject line property
293 private function setEmailPropertySubjectLine () {
298 * Method stub to avoid output
302 private function setEmailPropertyMessage () {
307 * Gets the template variable "message", stores it back as raw template data
308 * and compiles all variables so the mail message got prepared for output
312 private function finishMailData () {
313 // Get the message and set it as new raw template data back
314 $message = $this->readVariable('message');
315 $this->setRawTemplateData($message);
317 // Get some variables to compile
318 //$sender = $this->compileRawCode($this->readVariable('sender'));
319 //$this->assignVariable('sender', $sender);
321 // Then compile all variables
322 $this->compileVariables();
326 * Invokes the final mail process
330 private function finishTextMail () {
331 $this->getMailerInstance()->invokeMailDelivery();
335 * Setter for mailer instance
337 * @param $mailerInstance A mailer instance
340 public final function setMailerInstance (DeliverableMail $mailerInstance) {
341 $this->mailerInstance = $mailerInstance;
345 * Getter for mailer instance
347 * @return $mailerInstance A mailer instance
349 protected final function getMailerInstance () {
350 return $this->mailerInstance;
354 * Outputs the mail to the world. This should only the mailer debug class do!
356 * @param $responseInstance An instance of a Responseable class
359 public function transferToResponse (Responseable $responseInstance) {
360 $responseInstance->writeToBody($this->getCompiledData());