class_ImageTemplateEngine.php

Go to the documentation of this file.
00001 <?php
00024 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
00028         private $mainNodes = array("base", "type", "resolution", "background-color", "foreground-color", "image-string");
00029 
00033         private $subNodes = array("name", "string-name", "x", "y", "font-size", "width", "height", "red", "green", "blue", "text");
00034 
00038         private $imageInstance = 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 createImageTemplateEngine ($basePath, ManageableLanguage  $langInstance, FileIoHandler $ioInstance) {
00070                 // Get a new instance
00071                 $tplInstance = new ImageTemplateEngine();
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         public function startElement ($resource, $element, array $attributes) {
00146                 // Initial method name which will never be called...
00147                 $methodName = 'initImage';
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->mainNodes)) {
00155                         // Okay, main node found!
00156                         $methodName = 'setImage' . $this->convertToClassName($element);
00157                 } elseif (in_array($element, $this->subNodes)) {
00158                         // Sub node found
00159                         $methodName = 'setImageProperty' . $this->convertToClassName($element);
00160                 } elseif ($element != 'image') {
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->imageInstance, $methodName), array());
00197         }
00198 
00207         protected function characterHandler ($resource, $characters) {
00208                 // Trim all spaces away
00209                 $characters = trim($characters);
00210 
00211                 // Is this string empty?
00212                 if (empty($characters)) {
00213                         // Then skip it silently
00214                         return false;
00215                 } // END - if
00216 
00217                 // Unfinished work!
00218                 $this->partialStub("Handling extra characters is not yet supported!");
00219         }
00220 
00227         private function initImage () {
00228                 // Unfinished work!
00229         }
00230 
00237         private function setImageType ($imageType) {
00238                 // Set group to general
00239                 $this->setVariableGroup('general');
00240 
00241                 // Try to compile it first to get the value from variable stack
00242                 $imageType = $this->compileRawCode($imageType);
00243 
00244                 // Now make a class name of it
00245                 $className = $this->convertToClassName($imageType.'_image');
00246 
00247                 // And try to initiate it
00248                 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
00249 
00250                 // Set current main node to type
00251                 $this->currMainNode = 'type';
00252         }
00253 
00260         private function setImageResolution () {
00261                 // Call the image class
00262                 $this->imageInstance->initResolution();
00263 
00264                 // Current main node is resolution
00265                 $this->currMainNode = 'resolution';
00266         }
00267 
00274         private function setImageBase () {
00275                 // Call the image class
00276                 $this->imageInstance->initBase();
00277 
00278                 // Current main node is resolution
00279                 $this->currMainNode = 'base';
00280         }
00281 
00288         private function setImageBackgroundColor () {
00289                 // Call the image class
00290                 $this->imageInstance->initBackgroundColor();
00291 
00292                 // Current main node is background-color
00293                 $this->currMainNode = 'background-color';
00294         }
00295 
00302         private function setImageForegroundColor () {
00303                 // Call the image class
00304                 $this->imageInstance->initForegroundColor();
00305 
00306                 // Current main node is foreground-color
00307                 $this->currMainNode = 'foreground-color';
00308         }
00309 
00317         private function setImageImageString ($groupable = 'single') {
00318                 // Call the image class
00319                 $this->imageInstance->initImageString($groupable);
00320 
00321                 // Current main node is foreground-color
00322                 $this->currMainNode = 'image-string';
00323         }
00324 
00331         private function setImagePropertyName ($imageName) {
00332                 // Call the image class
00333                 $this->imageInstance->setImageName($imageName);
00334         }
00335 
00342         private function setImagePropertyWidth ($width) {
00343                 // Call the image class
00344                 $this->imageInstance->setWidth($width);
00345         }
00346 
00353         private function setImagePropertyHeight ($height) {
00354                 // Call the image class
00355                 $this->imageInstance->setHeight($height);
00356         }
00357 
00364         private function setImagePropertyRed ($red) {
00365                 // Call the image class
00366                 $this->imageInstance->setRed($red);
00367         }
00368 
00375         private function setImagePropertyGreen ($green) {
00376                 // Call the image class
00377                 $this->imageInstance->setGreen($green);
00378         }
00379 
00386         private function setImagePropertyBlue ($blue) {
00387                 // Call the image class
00388                 $this->imageInstance->setBlue($blue);
00389         }
00390 
00397         private function setImagePropertyStringName ($stringName) {
00398                 // Call the image class
00399                 $this->imageInstance->setStringName($stringName);
00400         }
00401 
00408         private function setImagePropertyFontSize ($fontSize) {
00409                 // Call the image class
00410                 $this->imageInstance->setFontSize($fontSize);
00411         }
00412 
00419         private function setImagePropertyText ($imageString) {
00420                 // Call the image class
00421                 $this->imageInstance->setString($imageString);
00422         }
00423 
00430         private function setImagePropertyX ($x) {
00431                 // Call the image class
00432                 $this->imageInstance->setX($x);
00433         }
00434 
00441         private function setImagePropertyY ($y) {
00442                 // Call the image class
00443                 $this->imageInstance->setY($y);
00444         }
00445 
00451         public function getImageCacheFqfn () {
00452                 // Get the FQFN ready
00453                 $fqfn = $this->getBasePath().'_cache/' . md5($this->imageInstance->getImageName().":".$this->__toString().":".$this->imageInstance->__toString()) . "." . $this->imageInstance->getImageType();
00454 
00455                 // Return it
00456                 return $fqfn;
00457         }
00458 
00465         public function transferToResponse (Responseable $responseInstance) {
00466                 // Set the image instance
00467                 $responseInstance->setImageInstance($this->imageInstance);
00468         }
00469 }
00470 
00471 // [EOF]
00472 ?>

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