d60f102359ae04cd55c101a3008181c1d15f17f8
[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                 // Set part description
55                 $this->setObjectDescription("Image template engine");
56
57                 // Create unique ID number
58                 $this->generateUniqueId();
59         }
60
61         /**
62          * Creates an instance of the class TemplateEngine and prepares it for usage
63          *
64          * @param       $basePath               The local base path for all templates
65          * @param       $langInstance   An instance of LanguageSystem (default)
66          * @param       $ioInstance             An instance of FileIoHandler (default, middleware!)
67          * @return      $tplInstance    An instance of TemplateEngine
68          * @throws      BasePathIsEmptyException                If the provided $basePath is empty
69          * @throws      InvalidBasePathStringException  If $basePath is no string
70          * @throws      BasePathIsNoDirectoryException  If $basePath is no
71          *                                                                                      directory or not found
72          * @throws      BasePathReadProtectedException  If $basePath is
73          *                                                                                      read-protected
74          */
75         public final static function createImageTemplateEngine ($basePath, ManageableLanguage  $langInstance, FileIoHandler $ioInstance) {
76                 // Get a new instance
77                 $tplInstance = new ImageTemplateEngine();
78
79                 // Is the base path valid?
80                 if (empty($basePath)) {
81                         // Base path is empty
82                         throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
83                 } elseif (!is_string($basePath)) {
84                         // Is not a string
85                         throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
86                 } elseif (!is_dir($basePath)) {
87                         // Is not a path
88                         throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
89                 } elseif (!is_readable($basePath)) {
90                         // Is not readable
91                         throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
92                 }
93
94                 // Get configuration instance
95                 $cfgInstance = FrameworkConfiguration::getInstance();
96
97                 // Set the base path
98                 $tplInstance->setBasePath($basePath);
99
100                 // Set the language and IO instances
101                 $tplInstance->setLanguageInstance($langInstance);
102                 $tplInstance->setFileIoInstance($ioInstance);
103
104                 // Set template extensions
105                 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
106                 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
107
108                 // Absolute output path for compiled templates
109                 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
110
111                 // Return the prepared instance
112                 return $tplInstance;
113         }
114
115         /**
116          * Handles the start element of an XML resource
117          *
118          * @param       $resource               XML parser resource (currently ignored)
119          * @param       $element                The element we shall handle
120          * @param       $attributes             All attributes
121          * @return      void
122          * @throws      InvalidXmlNodeException         If an unknown/invalid XML node name was found
123          */
124         public function startElement ($resource, $element, array $attributes) {
125                 // Initial method name which will never be called...
126                 $methodName = 'initImage';
127
128                 // Make the element name lower-case
129                 $element = strtolower($element);
130
131                 // Is the element a main node?
132                 //* DEBUG: */ echo "START: &gt;".$element."&lt;<br />\n";
133                 if (in_array($element, $this->mainNodes)) {
134                         // Okay, main node found!
135                         $methodName = 'setImage' . $this->convertToClassName($element);
136                 } elseif (in_array($element, $this->subNodes)) {
137                         // Sub node found
138                         $methodName = 'setImageProperty' . $this->convertToClassName($element);
139                 } elseif ($element != 'image') {
140                         // Invalid node name found
141                         throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
142                 }
143
144                 // Call method
145                 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
146                 call_user_func_array(array($this, $methodName), $attributes);
147         }
148
149         /**
150          * Ends the main or sub node by sending out the gathered data
151          *
152          * @param       $resource       An XML resource pointer (currently ignored)
153          * @param       $nodeName       Name of the node we want to finish
154          * @return      void
155          * @throws      XmlNodeMismatchException        If current main node mismatches the closing one
156          */
157         protected function endElement ($resource, $nodeName) {
158                 // Make all lower-case
159                 $nodeName = strtolower($nodeName);
160
161                 // Does this match with current main node?
162                 //* DEBUG: */ echo "END: &gt;".$nodeName."&lt;<br />\n";
163                 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
164                         // Did not match!
165                         throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
166                 } elseif (in_array($nodeName, $this->getSubNodes())) {
167                         // Silently ignore sub nodes
168                         return;
169                 }
170
171                 // Construct method name
172                 $methodName = 'finish' . $this->convertToClassName($nodeName);
173
174                 // Call the corresponding method
175                 call_user_func_array(array($this->imageInstance, $methodName), array());
176         }
177
178         /**
179          * Currently not used
180          *
181          * @param       $resource               XML parser resource (currently ignored)
182          * @param       $characters             Characters to handle
183          * @return      void
184          * @todo        Find something usefull with this!
185          */
186         protected function characterHandler ($resource, $characters) {
187                 // Trim all spaces away
188                 $characters = trim($characters);
189
190                 // Is this string empty?
191                 if (empty($characters)) {
192                         // Then skip it silently
193                         return false;
194                 } // END - if
195
196                 // Unfinished work!
197                 $this->partialStub("Handling extra characters is not yet supported!");
198         }
199
200         /**
201          * Intializes the image
202          *
203          * @return      void
204          * @todo        Add cache creation here
205          */
206         private function initImage () {
207                 // Unfinished work!
208         }
209
210         /**
211          * Set the image type
212          *
213          * @param       $imageType      Code fragment or direct value holding the image type
214          * @return      void
215          */
216         private function setImageType ($imageType) {
217                 // Set group to general
218                 $this->setVariableGroup('general');
219
220                 // Try to compile it first to get the value from variable stack
221                 $imageType = $this->compileRawCode($imageType);
222
223                 // Now make a class name of it
224                 $className = $this->convertToClassName($imageType.'_image');
225
226                 // And try to initiate it
227                 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
228
229                 // Set current main node to type
230                 $this->currMainNode = 'type';
231         }
232
233         /**
234          * "Setter" for resolution, we first need to collect the resolution from the
235          * sub-nodes. So first, this method will prepare an array for it
236          *
237          * @return      void
238          */
239         private function setImageResolution () {
240                 // Call the image class
241                 $this->imageInstance->initResolution();
242
243                 // Current main node is resolution
244                 $this->currMainNode = 'resolution';
245         }
246
247         /**
248          * "Setter" for base information. For more details see above method!
249          *
250          * @return      void
251          * @see         ImageTemplateEngine::setImageResolution
252          */
253         private function setImageBase () {
254                 // Call the image class
255                 $this->imageInstance->initBase();
256
257                 // Current main node is resolution
258                 $this->currMainNode = 'base';
259         }
260
261         /**
262          * "Setter" for background-color. For more details see above method!
263          *
264          * @return      void
265          * @see         ImageTemplateEngine::setImageResolution
266          */
267         private function setImageBackgroundColor () {
268                 // Call the image class
269                 $this->imageInstance->initBackgroundColor();
270
271                 // Current main node is background-color
272                 $this->currMainNode = 'background-color';
273         }
274
275         /**
276          * "Setter" for foreground-color. For more details see above method!
277          *
278          * @return      void
279          * @see         ImageTemplateEngine::setImageResolution
280          */
281         private function setImageForegroundColor () {
282                 // Call the image class
283                 $this->imageInstance->initForegroundColor();
284
285                 // Current main node is foreground-color
286                 $this->currMainNode = 'foreground-color';
287         }
288
289         /**
290          * "Setter" for image-string. For more details see above method!
291          *
292          * @param       $groupable      Wether this image string is groupable
293          * @return      void
294          * @see         ImageTemplateEngine::setImageResolution
295          */
296         private function setImageImageString ($groupable = 'single') {
297                 // Call the image class
298                 $this->imageInstance->initImageString($groupable);
299
300                 // Current main node is foreground-color
301                 $this->currMainNode = 'image-string';
302         }
303
304         /**
305          * Setter for image name
306          *
307          * @param       $imageName      Name of the image
308          * @return      void
309          */
310         private function setImagePropertyName ($imageName) {
311                 // Call the image class
312                 $this->imageInstance->setImageName($imageName);
313         }
314
315         /**
316          * Setter for image width
317          *
318          * @param       $width  Width of the image or variable
319          * @return      void
320          */
321         private function setImagePropertyWidth ($width) {
322                 // Call the image class
323                 $this->imageInstance->setWidth($width);
324         }
325
326         /**
327          * Setter for image height
328          *
329          * @param       $height Height of the image or variable
330          * @return      void
331          */
332         private function setImagePropertyHeight ($height) {
333                 // Call the image class
334                 $this->imageInstance->setHeight($height);
335         }
336
337         /**
338          * Setter for image red color
339          *
340          * @param       $red    Red color value
341          * @return      void
342          */
343         private function setImagePropertyRed ($red) {
344                 // Call the image class
345                 $this->imageInstance->setRed($red);
346         }
347
348         /**
349          * Setter for image green color
350          *
351          * @param       $green  Green color value
352          * @return      void
353          */
354         private function setImagePropertyGreen ($green) {
355                 // Call the image class
356                 $this->imageInstance->setGreen($green);
357         }
358
359         /**
360          * Setter for image blue color
361          *
362          * @param       $blue   Blue color value
363          * @return      void
364          */
365         private function setImagePropertyBlue ($blue) {
366                 // Call the image class
367                 $this->imageInstance->setBlue($blue);
368         }
369
370         /**
371          * Setter for string name (identifier)
372          *
373          * @param       $stringName             String name (identifier)
374          * @return      void
375          */
376         private function setImagePropertyStringName ($stringName) {
377                 // Call the image class
378                 $this->imageInstance->setStringName($stringName);
379         }
380
381         /**
382          * Setter for font size
383          *
384          * @param       $fontSize       Size of the font
385          * @return      void
386          */
387         private function setImagePropertyFontSize ($fontSize) {
388                 // Call the image class
389                 $this->imageInstance->setFontSize($fontSize);
390         }
391
392         /**
393          * Setter for image string
394          *
395          * @param       $imageString    Image string to set
396          * @return      void
397          */
398         private function setImagePropertyText ($imageString) {
399                 // Call the image class
400                 $this->imageInstance->setString($imageString);
401         }
402
403         /**
404          * Setter for X coordinate
405          *
406          * @param       $x      X coordinate
407          * @return      void
408          */
409         private function setImagePropertyX ($x) {
410                 // Call the image class
411                 $this->imageInstance->setX($x);
412         }
413
414         /**
415          * Setter for Y coordinate
416          *
417          * @param       $y      Y coordinate
418          * @return      void
419          */
420         private function setImagePropertyY ($y) {
421                 // Call the image class
422                 $this->imageInstance->setY($y);
423         }
424
425         /**
426          * Getter for image cache file (FQFN)
427          *
428          * @return      $fqfn   Full-qualified file name of the image cache
429          */
430         public function getImageCacheFqfn () {
431                 // Get the FQFN ready
432                 $fqfn = $this->getBasePath().'_cache/' . md5($this->imageInstance->getImageName().":".$this->__toString().":".$this->imageInstance->__toString()) . "." . $this->imageInstance->getImageType();
433
434                 // Return it
435                 return $fqfn;
436         }
437
438         /**
439          * Outputs the image to the world
440          *
441          * @param       $responseInstance       An instance of a Responseable class
442          * @return      void
443          */
444         public function transferToResponse (Responseable $responseInstance) {
445                 // Set the image instance
446                 $responseInstance->setImageInstance($this->imageInstance);
447         }
448 }
449
450 // [EOF]
451 ?>