A lot rewrites and fixes for weak redirect methods
[shipsimu.git] / inc / classes / main / template / image / class_ImageTemplateEngine.php
1 <?php
2 /**
3  * The own template engine for loading caching and sending out images
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
25         /**
26          * Main nodes in the XML tree ('image' is ignored)
27          */
28         private $mainNodes = array("base", "type", "resolution", "background-color", "foreground-color", "image-string");
29
30         /**
31          * Sub nodes in the XML tree
32          */
33         private $subNodes = array("name", "string-name", "x", "y", "font-size", "width", "height", "red", "green", "blue", "text");
34
35         /**
36          * Image instance
37          */
38         private $imageInstance = null;
39
40         /**
41          * Current main node
42          */
43         private $currMainNode = "";
44
45         /**
46          * Protected constructor
47          *
48          * @return      void
49          */
50         protected function __construct () {
51                 // Call parent constructor
52                 parent::__construct(__CLASS__);
53         }
54
55         /**
56          * Creates an instance of the class TemplateEngine and prepares it for usage
57          *
58          * @param       $basePath               The local base path for all templates
59          * @param       $langInstance   An instance of LanguageSystem (default)
60          * @param       $ioInstance             An instance of FileIoHandler (default, middleware!)
61          * @return      $tplInstance    An instance of TemplateEngine
62          * @throws      BasePathIsEmptyException                If the provided $basePath is empty
63          * @throws      InvalidBasePathStringException  If $basePath is no string
64          * @throws      BasePathIsNoDirectoryException  If $basePath is no
65          *                                                                                      directory or not found
66          * @throws      BasePathReadProtectedException  If $basePath is
67          *                                                                                      read-protected
68          */
69         public final static function createImageTemplateEngine ($basePath, ManageableLanguage  $langInstance, FileIoHandler $ioInstance) {
70                 // Get a new instance
71                 $tplInstance = new ImageTemplateEngine();
72
73                 // Is the base path valid?
74                 if (empty($basePath)) {
75                         // Base path is empty
76                         throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77                 } elseif (!is_string($basePath)) {
78                         // Is not a string
79                         throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
80                 } elseif (!is_dir($basePath)) {
81                         // Is not a path
82                         throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
83                 } elseif (!is_readable($basePath)) {
84                         // Is not readable
85                         throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
86                 }
87
88                 // Get configuration instance
89                 $cfgInstance = FrameworkConfiguration::getInstance();
90
91                 // Set the base path
92                 $tplInstance->setBasePath($basePath);
93
94                 // Set the language and IO instances
95                 $tplInstance->setLanguageInstance($langInstance);
96                 $tplInstance->setFileIoInstance($ioInstance);
97
98                 // Set template extensions
99                 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
100                 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
101
102                 // Absolute output path for compiled templates
103                 $tplInstance->setCompileOutputPath($cfgInstance->readConfig('base_path') . $cfgInstance->readConfig('compile_output_path'));
104
105                 // Return the prepared instance
106                 return $tplInstance;
107         }
108
109         /**
110          * Getter for current main node
111          *
112          * @return      $currMainNode   Current main node
113          */
114         public final function getCurrMainNode () {
115                 return $this->currMainNode;
116         }
117
118         /**
119          * Getter for main node array
120          *
121          * @return      $mainNodes      Array with valid main node names
122          */
123         public final function getMainNodes () {
124                 return $this->mainNodes;
125         }
126
127         /**
128          * Getter for sub node array
129          *
130          * @return      $subNodes       Array with valid sub node names
131          */
132         public final function getSubNodes () {
133                 return $this->subNodes;
134         }
135
136         /**
137          * Handles the start element of an XML resource
138          *
139          * @param       $resource               XML parser resource (currently ignored)
140          * @param       $element                The element we shall handle
141          * @param       $attributes             All attributes
142          * @return      void
143          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
144          */
145         public function startElement ($resource, $element, array $attributes) {
146                 // Initial method name which will never be called...
147                 $methodName = 'initImage';
148
149                 // Make the element name lower-case
150                 $element = strtolower($element);
151
152                 // Is the element a main node?
153                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
154                 if (in_array($element, $this->mainNodes)) {
155                         // Okay, main node found!
156                         $methodName = 'setImage' . $this->convertToClassName($element);
157                 } elseif (in_array($element, $this->subNodes)) {
158                         // Sub node found
159                         $methodName = 'setImageProperty' . $this->convertToClassName($element);
160                 } elseif ($element != 'image') {
161                         // Invalid node name found
162                         throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
163                 }
164
165                 // Call method
166                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
167                 call_user_func_array(array($this, $methodName), $attributes);
168         }
169
170         /**
171          * Ends the main or sub node by sending out the gathered data
172          *
173          * @param       $resource       An XML resource pointer (currently ignored)
174          * @param       $nodeName       Name of the node we want to finish
175          * @return      void
176          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
177          */
178         protected function endElement ($resource, $nodeName) {
179                 // Make all lower-case
180                 $nodeName = strtolower($nodeName);
181
182                 // Does this match with current main node?
183                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
184                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
185                         // Did not match!
186                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
187                 } elseif (in_array($nodeName, $this->getSubNodes())) {
188                         // Silently ignore sub nodes
189                         return;
190                 }
191
192                 // Construct method name
193                 $methodName = 'finish' . $this->convertToClassName($nodeName);
194
195                 // Call the corresponding method
196                 call_user_func_array(array($this->imageInstance, $methodName), array());
197         }
198
199         /**
200          * Currently not used
201          *
202          * @param       $resource               XML parser resource (currently ignored)
203          * @param       $characters             Characters to handle
204          * @return      void
205          * @todo        Find something usefull with this!
206          */
207         protected function characterHandler ($resource, $characters) {
208                 // Trim all spaces away
209                 $characters = trim($characters);
210
211                 // Is this string empty?
212                 if (empty($characters)) {
213                         // Then skip it silently
214                         return false;
215                 } // END - if
216
217                 // Unfinished work!
218                 $this->partialStub("Handling extra characters is not yet supported!");
219         }
220
221         /**
222          * Intializes the image
223          *
224          * @return      void
225          * @todo        Add cache creation here
226          */
227         private function initImage () {
228                 // Unfinished work!
229         }
230
231         /**
232          * Set the image type
233          *
234          * @param       $imageType      Code fragment or direct value holding the image type
235          * @return      void
236          */
237         private function setImageType ($imageType) {
238                 // Set group to general
239                 $this->setVariableGroup('general');
240
241                 // Try to compile it first to get the value from variable stack
242                 $imageType = $this->compileRawCode($imageType);
243
244                 // Now make a class name of it
245                 $className = $this->convertToClassName($imageType.'_image');
246
247                 // And try to initiate it
248                 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
249
250                 // Set current main node to type
251                 $this->currMainNode = 'type';
252         }
253
254         /**
255          * "Setter" for resolution, we first need to collect the resolution from the
256          * sub-nodes. So first, this method will prepare an array for it
257          *
258          * @return      void
259          */
260         private function setImageResolution () {
261                 // Call the image class
262                 $this->imageInstance->initResolution();
263
264                 // Current main node is resolution
265                 $this->currMainNode = 'resolution';
266         }
267
268         /**
269          * "Setter" for base information. For more details see above method!
270          *
271          * @return      void
272          * @see         ImageTemplateEngine::setImageResolution
273          */
274         private function setImageBase () {
275                 // Call the image class
276                 $this->imageInstance->initBase();
277
278                 // Current main node is resolution
279                 $this->currMainNode = 'base';
280         }
281
282         /**
283          * "Setter" for background-color. For more details see above method!
284          *
285          * @return      void
286          * @see         ImageTemplateEngine::setImageResolution
287          */
288         private function setImageBackgroundColor () {
289                 // Call the image class
290                 $this->imageInstance->initBackgroundColor();
291
292                 // Current main node is background-color
293                 $this->currMainNode = 'background-color';
294         }
295
296         /**
297          * "Setter" for foreground-color. For more details see above method!
298          *
299          * @return      void
300          * @see         ImageTemplateEngine::setImageResolution
301          */
302         private function setImageForegroundColor () {
303                 // Call the image class
304                 $this->imageInstance->initForegroundColor();
305
306                 // Current main node is foreground-color
307                 $this->currMainNode = 'foreground-color';
308         }
309
310         /**
311          * "Setter" for image-string. For more details see above method!
312          *
313          * @param       $groupable      Wether this image string is groupable
314          * @return      void
315          * @see         ImageTemplateEngine::setImageResolution
316          */
317         private function setImageImageString ($groupable = 'single') {
318                 // Call the image class
319                 $this->imageInstance->initImageString($groupable);
320
321                 // Current main node is foreground-color
322                 $this->currMainNode = 'image-string';
323         }
324
325         /**
326          * Setter for image name
327          *
328          * @param       $imageName      Name of the image
329          * @return      void
330          */
331         private function setImagePropertyName ($imageName) {
332                 // Call the image class
333                 $this->imageInstance->setImageName($imageName);
334         }
335
336         /**
337          * Setter for image width
338          *
339          * @param       $width  Width of the image or variable
340          * @return      void
341          */
342         private function setImagePropertyWidth ($width) {
343                 // Call the image class
344                 $this->imageInstance->setWidth($width);
345         }
346
347         /**
348          * Setter for image height
349          *
350          * @param       $height Height of the image or variable
351          * @return      void
352          */
353         private function setImagePropertyHeight ($height) {
354                 // Call the image class
355                 $this->imageInstance->setHeight($height);
356         }
357
358         /**
359          * Setter for image red color
360          *
361          * @param       $red    Red color value
362          * @return      void
363          */
364         private function setImagePropertyRed ($red) {
365                 // Call the image class
366                 $this->imageInstance->setRed($red);
367         }
368
369         /**
370          * Setter for image green color
371          *
372          * @param       $green  Green color value
373          * @return      void
374          */
375         private function setImagePropertyGreen ($green) {
376                 // Call the image class
377                 $this->imageInstance->setGreen($green);
378         }
379
380         /**
381          * Setter for image blue color
382          *
383          * @param       $blue   Blue color value
384          * @return      void
385          */
386         private function setImagePropertyBlue ($blue) {
387                 // Call the image class
388                 $this->imageInstance->setBlue($blue);
389         }
390
391         /**
392          * Setter for string name (identifier)
393          *
394          * @param       $stringName             String name (identifier)
395          * @return      void
396          */
397         private function setImagePropertyStringName ($stringName) {
398                 // Call the image class
399                 $this->imageInstance->setStringName($stringName);
400         }
401
402         /**
403          * Setter for font size
404          *
405          * @param       $fontSize       Size of the font
406          * @return      void
407          */
408         private function setImagePropertyFontSize ($fontSize) {
409                 // Call the image class
410                 $this->imageInstance->setFontSize($fontSize);
411         }
412
413         /**
414          * Setter for image string
415          *
416          * @param       $imageString    Image string to set
417          * @return      void
418          */
419         private function setImagePropertyText ($imageString) {
420                 // Call the image class
421                 $this->imageInstance->setString($imageString);
422         }
423
424         /**
425          * Setter for X coordinate
426          *
427          * @param       $x      X coordinate
428          * @return      void
429          */
430         private function setImagePropertyX ($x) {
431                 // Call the image class
432                 $this->imageInstance->setX($x);
433         }
434
435         /**
436          * Setter for Y coordinate
437          *
438          * @param       $y      Y coordinate
439          * @return      void
440          */
441         private function setImagePropertyY ($y) {
442                 // Call the image class
443                 $this->imageInstance->setY($y);
444         }
445
446         /**
447          * Getter for image cache file (FQFN)
448          *
449          * @return      $fqfn   Full-qualified file name of the image cache
450          */
451         public function getImageCacheFqfn () {
452                 // Get the FQFN ready
453                 $fqfn = $this->getBasePath().'_cache/' . md5($this->imageInstance->getImageName().":".$this->__toString().":".$this->imageInstance->__toString()) . "." . $this->imageInstance->getImageType();
454
455                 // Return it
456                 return $fqfn;
457         }
458
459         /**
460          * Outputs the image to the world
461          *
462          * @param       $responseInstance       An instance of a Responseable class
463          * @return      void
464          */
465         public function transferToResponse (Responseable $responseInstance) {
466                 // Set the image instance
467                 $responseInstance->setImageInstance($this->imageInstance);
468         }
469 }
470
471 // [EOF]
472 ?>