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