class_MailTemplateEngine.php

Go to the documentation of this file.
00001 <?php
00024 class MailTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
00028         private $mainNodes = array("mail-data");
00029 
00033         private $subNodes = array("subject-line", "sender-address", "recipient-address", "message");
00034 
00038         private $mailerInstance = null;
00039 
00043         private $currMainNode = "";
00044 
00050         protected function __construct () {
00051                 // Call parent constructor
00052                 parent::__construct(__CLASS__);
00053         }
00054 
00069         public final static function createMailTemplateEngine ($basePath, ManageableLanguage  $langInstance, FileIoHandler $ioInstance) {
00070                 // Get a new instance
00071                 $tplInstance = new MailTemplateEngine();
00072 
00073                 // Is the base path valid?
00074                 if (empty($basePath)) {
00075                         // Base path is empty
00076                         throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
00077                 } elseif (!is_string($basePath)) {
00078                         // Is not a string
00079                         throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
00080                 } elseif (!is_dir($basePath)) {
00081                         // Is not a path
00082                         throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
00083                 } elseif (!is_readable($basePath)) {
00084                         // Is not readable
00085                         throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
00086                 }
00087 
00088                 // Get configuration instance
00089                 $cfgInstance = FrameworkConfiguration::getInstance();
00090 
00091                 // Set the base path
00092                 $tplInstance->setBasePath($basePath);
00093 
00094                 // Set the language and IO instances
00095                 $tplInstance->setLanguageInstance($langInstance);
00096                 $tplInstance->setFileIoInstance($ioInstance);
00097 
00098                 // Set template extensions
00099                 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
00100                 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
00101 
00102                 // Absolute output path for compiled templates
00103                 $tplInstance->setCompileOutputPath($cfgInstance->readConfig('base_path') . $cfgInstance->readConfig('compile_output_path'));
00104 
00105                 // Return the prepared instance
00106                 return $tplInstance;
00107         }
00108 
00114         public final function getCurrMainNode () {
00115                 return $this->currMainNode;
00116         }
00117 
00123         public final function getMainNodes () {
00124                 return $this->mainNodes;
00125         }
00126 
00132         public final function getSubNodes () {
00133                 return $this->subNodes;
00134         }
00135 
00145         protected function startElement ($resource, $element, array $attributes) {
00146                 // Initial method name which will never be called...
00147                 $methodName = 'initEmail';
00148 
00149                 // Make the element name lower-case
00150                 $element = strtolower($element);
00151 
00152                 // Is the element a main node?
00153                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
00154                 if (in_array($element, $this->getMainNodes())) {
00155                         // Okay, main node found!
00156                         $methodName = 'setEmail' . $this->convertToClassName($element);
00157                 } elseif (in_array($element, $this->getSubNodes())) {
00158                         // Sub node found
00159                         $methodName = 'setEmailProperty' . $this->convertToClassName($element);
00160                 } elseif ($element != 'text-mail') {
00161                         // Invalid node name found
00162                         throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
00163                 }
00164 
00165                 // Call method
00166                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
00167                 call_user_func_array(array($this, $methodName), $attributes);
00168         }
00169 
00178         protected function endElement ($resource, $nodeName) {
00179                 // Make all lower-case
00180                 $nodeName = strtolower($nodeName);
00181 
00182                 // Does this match with current main node?
00183                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
00184                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
00185                         // Did not match!
00186                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
00187                 } elseif (in_array($nodeName, $this->getSubNodes())) {
00188                         // Silently ignore sub nodes
00189                         return;
00190                 }
00191 
00192                 // Construct method name
00193                 $methodName = 'finish' . $this->convertToClassName($nodeName);
00194 
00195                 // Call the corresponding method
00196                 call_user_func_array(array($this, $methodName), array());
00197         }
00198 
00206         protected function characterHandler ($resource, $characters) {
00207                 // Trim all spaces away
00208                 $characters = trim($characters);
00209 
00210                 // Is this string empty?
00211                 if (empty($characters)) {
00212                         // Then skip it silently
00213                         return false;
00214                 } // END - if
00215 
00216                 // Add the message now
00217                 $this->assignVariable('message', $characters);
00218         }
00219 
00226         private function initEmail () {
00227                 // Unfinished work!
00228         }
00229 
00236         private function setEmailMailData () {
00237                 // Set current main node
00238                 $this->currMainNode = 'mail-data';
00239         }
00240 
00247         private function setEmailPropertySenderAddress ($senderAddress) {
00248                 // Set the template variable
00249                 $this->assignVariable('sender', $senderAddress);
00250         }
00251 
00258         private function setEmailPropertyRecipientAddress ($recipientAddress) {
00259                 // Set the template variable
00260                 $this->assignVariable('recipient', $recipientAddress);
00261         }
00262 
00269         private function setEmailPropertySubjectLine ($subjectLine) {
00270                 // Set the template variable
00271                 $this->assignVariable('subject', $subjectLine);
00272         }
00273 
00279         private function setEmailPropertyMessage () {
00280                 // Empty for now
00281         }
00282 
00289         private function finishMailData () {
00290                 // Get the message and set it as new raw template data back
00291                 $message = $this->readVariable('message');
00292                 $this->setRawTemplateData($message);
00293 
00294                 // Get some variables to compile
00295                 //$sender = $this->compileRawCode($this->readVariable('sender'));
00296                 //$this->assignVariable('sender', $sender);
00297 
00298                 // Then compile all variables
00299                 $this->compileVariables();
00300         }
00301 
00307         private function finishTextMail () {
00308                 $this->getMailerInstance()->invokeMailDelivery();
00309         }
00310 
00317         public function getMailCacheFqfn () {
00318                 // Initialize FQFN
00319                 $fqfn = "";
00320                 $this->debugBackTrace();
00321 
00322                 // Return it
00323                 return $fqfn;
00324         }
00325 
00332         public final function setMailerInstance (DeliverableMail $mailerInstance) {
00333                 $this->mailerInstance = $mailerInstance;
00334         }
00335 
00341         protected final function getMailerInstance () {
00342                 return $this->mailerInstance;
00343         }
00344 
00351         public function transferToResponse (Responseable $responseInstance) {
00352                 $responseInstance->writeToBody($this->getCompiledData());
00353         }
00354 }
00355 
00356 // [EOF]
00357 ?>

Generated on Mon Dec 8 01:06:46 2008 for Ship-Simulator by  doxygen 1.5.6