3 namespace Org\Mxchange\CoreFramework\Template\Engine;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
8 use Org\Mxchange\CoreFramework\Mailer\DeliverableMail;
9 use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Response\Responseable;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
13 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
14 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
17 use \UnexpectedValueException;
20 * The own template engine for loading caching and sending out images
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
27 * @todo This template engine does not make use of setTemplateType()
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
44 * Main nodes in the XML tree
46 private $mainNodes = array(
51 * Sub nodes in the XML tree
53 private $subNodes = array(
63 private $mailerInstance = NULL;
68 private $currMainNode = '';
71 * Protected constructor
75 protected function __construct () {
76 // Call parent constructor
77 parent::__construct(__CLASS__);
80 $this->setTemplateType('mail');
84 * Creates an instance of the class TemplateEngine and prepares it for usage
86 * @return $templateInstance An instance of TemplateEngine
87 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
88 * @throws InvalidDirectoryException If $templateBasePath is no
89 * directory or not found
90 * @throws BasePathReadProtectedException If $templateBasePath is
93 public static final function createMailTemplateEngine () {
95 $templateInstance = new MailTemplateEngine();
97 // Get the application instance from registry
98 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
100 // Determine base path
101 $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
103 // Is the base path valid?
104 if (empty($templateBasePath)) {
105 // Base path is empty
106 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
107 } elseif (!is_string($templateBasePath)) {
109 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
110 } elseif (!is_dir($templateBasePath)) {
112 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
113 } elseif (!is_readable($templateBasePath)) {
115 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
119 $templateInstance->setTemplateBasePath($templateBasePath);
121 // Set template extensions
122 $templateInstance->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
123 $templateInstance->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('code_template_extension'));
125 // Absolute output path for compiled templates
126 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
128 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
131 // Return the prepared instance
132 return $templateInstance;
136 * Getter for current main node
138 * @return $currMainNode Current main node
140 public final function getCurrMainNode () {
141 return $this->currMainNode;
145 * Getter for main node array
147 * @return $mainNodes Array with valid main node names
149 public final function getMainNodes () {
150 return $this->mainNodes;
154 * Getter for sub node array
156 * @return $subNodes Array with valid sub node names
158 public final function getSubNodes () {
159 return $this->subNodes;
163 * Handles the start element of an XML resource
165 * @param $resource XML parser resource (currently ignored)
166 * @param $element The element we shall handle
167 * @param $attributes All attributes
169 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
171 public function startElement ($resource, $element, array $attributes) {
172 // Initial method name which will never be called...
173 $methodName = 'initEmail';
175 // Make the element name lower-case
176 $element = strtolower($element);
178 // Is the element a main node?
179 //* DEBUG: */ echo "START: >".$element."<<br />\n";
180 if (in_array($element, $this->getMainNodes())) {
181 // Okay, main node found!
182 $methodName = 'setEmail' . StringUtils::convertToClassName($element);
183 } elseif (in_array($element, $this->getSubNodes())) {
185 $methodName = 'setEmailProperty' . StringUtils::convertToClassName($element);
186 } elseif ($element != 'text-mail') {
187 // Invalid node name found
188 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
192 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
193 call_user_func_array(array($this, $methodName), $attributes);
197 * Ends the main or sub node by sending out the gathered data
199 * @param $resource An XML resource pointer (currently ignored)
200 * @param $nodeName Name of the node we want to finish
202 * @throws XmlNodeMismatchException If current main node mismatches the closing one
204 public function finishElement ($resource, $nodeName) {
205 // Make all lower-case
206 $nodeName = strtolower($nodeName);
208 // Does this match with current main node?
209 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
210 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
212 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
213 } elseif (in_array($nodeName, $this->getSubNodes())) {
214 // Silently ignore sub nodes
218 // Construct method name
219 $methodName = 'finish' . StringUtils::convertToClassName($nodeName);
221 // Call the corresponding method
222 call_user_func_array(array($this, $methodName), array());
226 * Adds the message text to the template engine
228 * @param $resource XML parser resource (currently ignored)
229 * @param $characters Characters to handle
232 public function characterHandler ($resource, $characters) {
233 // Trim all spaces away
234 $characters = trim($characters);
236 // Is this string empty?
237 if (empty($characters)) {
238 // Then skip it silently
242 // Add the message now
243 $this->assignVariable('message', $characters);
247 * Intializes the mail
250 * @todo Add cache creation here
252 private function initEmail () {
257 * Setter for mail data node
260 * @todo Should we call back the mailer class here?
262 private function setEmailMailData () {
263 // Set current main node
264 $this->currMainNode = 'mail-data';
268 * Setter for sender address property
270 * @param $senderAddress Sender address to set in email
273 private function setEmailPropertySenderAddress ($senderAddress) {
274 // Set the template variable
275 $this->assignVariable('sender', $senderAddress);
279 * Setter for recipient address property
281 * @param $recipientAddress Recipient address to set in email
284 private function setEmailPropertyRecipientAddress ($recipientAddress) {
285 // Set the template variable
286 $this->assignVariable('recipient', $recipientAddress);
290 * Setter for subject line property
294 private function setEmailPropertySubjectLine () {
299 * Method stub to avoid output
303 private function setEmailPropertyMessage () {
308 * Gets the template variable "message", stores it back as raw template data
309 * and compiles all variables so the mail message got prepared for output
313 private function finishMailData () {
314 // Get the message and set it as new raw template data back
315 $message = $this->readVariable('message');
316 $this->setRawTemplateData($message);
318 // Get some variables to compile
319 //$sender = $this->compileRawCode($this->readVariable('sender'));
320 //$this->assignVariable('sender', $sender);
322 // Then compile all variables
323 $this->compileVariables();
327 * Invokes the final mail process
331 private function finishTextMail () {
332 $this->getMailerInstance()->invokeMailDelivery();
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());