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\Parseable;
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 = [
51 * Sub nodes in the XML tree
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, string $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), Parseable::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, string $nodeName) {
205 // Does this match with current main node?
206 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
207 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
209 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), Parseable::EXCEPTION_XML_NODE_MISMATCH);
210 } elseif (in_array($nodeName, $this->getSubNodes())) {
211 // Silently ignore sub nodes
215 // Construct method name
216 $methodName = 'finish' . StringUtils::convertToClassName($nodeName);
218 // Call the corresponding method
219 call_user_func_array(array($this, $methodName), []);
223 * Adds the message text to the template engine
225 * @param $resource XML parser resource (currently ignored)
226 * @param $characters Characters to handle
229 public function characterHandler ($resource, string $characters) {
230 // Trim all spaces away
231 $characters = trim($characters);
233 // Is this string empty?
234 if (empty($characters)) {
235 // Then skip it silently
239 // Add the message now
240 $this->assignVariable('message', $characters);
244 * Intializes the mail
247 * @todo Add cache creation here
249 private function initEmail () {
254 * Setter for mail data node
257 * @todo Should we call back the mailer class here?
259 private function setEmailMailData () {
260 // Set current main node
261 $this->currMainNode = 'mail-data';
265 * Setter for sender address property
267 * @param $senderAddress Sender address to set in email
270 private function setEmailPropertySenderAddress ($senderAddress) {
271 // Set the template variable
272 $this->assignVariable('sender', $senderAddress);
276 * Setter for recipient address property
278 * @param $recipientAddress Recipient address to set in email
281 private function setEmailPropertyRecipientAddress ($recipientAddress) {
282 // Set the template variable
283 $this->assignVariable('recipient', $recipientAddress);
287 * Setter for subject line property
291 private function setEmailPropertySubjectLine () {
296 * Method stub to avoid output
300 private function setEmailPropertyMessage () {
305 * Gets the template variable "message", stores it back as raw template data
306 * and compiles all variables so the mail message got prepared for output
310 private function finishMailData () {
311 // Get the message and set it as new raw template data back
312 $message = $this->readVariable('message');
313 $this->setRawTemplateData($message);
315 // Get some variables to compile
316 //$sender = $this->compileRawCode($this->readVariable('sender'));
317 //$this->assignVariable('sender', $sender);
319 // Then compile all variables
320 $this->compileVariables();
324 * Invokes the final mail process
328 private function finishTextMail () {
329 $this->getMailerInstance()->invokeMailDelivery();
333 * Setter for mailer instance
335 * @param $mailerInstance A mailer instance
338 public final function setMailerInstance (DeliverableMail $mailerInstance) {
339 $this->mailerInstance = $mailerInstance;
343 * Getter for mailer instance
345 * @return $mailerInstance A mailer instance
347 protected final function getMailerInstance () {
348 return $this->mailerInstance;
352 * Outputs the mail to the world. This should only the mailer debug class do!
354 * @param $responseInstance An instance of a Responseable class
357 public function transferToResponse (Responseable $responseInstance) {
358 $responseInstance->writeToBody($this->getCompiledData());