3 * The own template engine for loading caching and sending out images
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
26 * Main nodes in the XML tree ('image' is ignored)
28 private $mainNodes = array("base", "type", "resolution", "background-color", "foreground-color", "image-string");
31 * Sub nodes in the XML tree
33 private $subNodes = array("name", "string-name", "x", "y", "font-size", "width", "height", "red", "green", "blue", "text");
38 private $imageInstance = null;
43 private $currMainNode = "";
46 * Protected constructor
50 protected function __construct () {
51 // Call parent constructor
52 parent::__construct(__CLASS__);
54 // Set part description
55 $this->setObjectDescription("Image template engine");
57 // Create unique ID number
58 $this->generateUniqueId();
62 * Creates an instance of the class TemplateEngine and prepares it for usage
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
75 public final static function createImageTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
77 $tplInstance = new ImageTemplateEngine();
79 // Is the base path valid?
80 if (empty($basePath)) {
82 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
83 } elseif (!is_string($basePath)) {
85 throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
86 } elseif (!is_dir($basePath)) {
88 throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
89 } elseif (!is_readable($basePath)) {
91 throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
94 // Get configuration instance
95 $cfgInstance = FrameworkConfiguration::getInstance();
98 $tplInstance->setBasePath($basePath);
100 // Set the language and IO instances
101 $tplInstance->setLanguageInstance($langInstance);
102 $tplInstance->setFileIoInstance($ioInstance);
104 // Set template extensions
105 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
106 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
108 // Absolute output path for compiled templates
109 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
111 // Return the prepared instance
116 * Getter for current main node
118 * @return $currMainNode Current main node
120 public final function getCurrMainNode () {
121 return $this->currMainNode;
125 * Getter for main node array
127 * @return $mainNodes Array with valid main node names
129 public final function getMainNodes () {
130 return $this->mainNodes;
134 * Getter for sub node array
136 * @return $subNodes Array with valid sub node names
138 public final function getSubNodes () {
139 return $this->subNodes;
143 * Handles the start element of an XML resource
145 * @param $resource XML parser resource (currently ignored)
146 * @param $element The element we shall handle
147 * @param $attributes All attributes
149 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
151 public function startElement ($resource, $element, array $attributes) {
152 // Initial method name which will never be called...
153 $methodName = 'initImage';
155 // Make the element name lower-case
156 $element = strtolower($element);
158 // Is the element a main node?
159 //* DEBUG: */ echo "START: >".$element."<<br />\n";
160 if (in_array($element, $this->mainNodes)) {
161 // Okay, main node found!
162 $methodName = 'setImage' . $this->convertToClassName($element);
163 } elseif (in_array($element, $this->subNodes)) {
165 $methodName = 'setImageProperty' . $this->convertToClassName($element);
166 } elseif ($element != 'image') {
167 // Invalid node name found
168 throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
172 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
173 call_user_func_array(array($this, $methodName), $attributes);
177 * Ends the main or sub node by sending out the gathered data
179 * @param $resource An XML resource pointer (currently ignored)
180 * @param $nodeName Name of the node we want to finish
182 * @throws XmlNodeMismatchException If current main node mismatches the closing one
184 protected function endElement ($resource, $nodeName) {
185 // Make all lower-case
186 $nodeName = strtolower($nodeName);
188 // Does this match with current main node?
189 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
190 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
192 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
193 } elseif (in_array($nodeName, $this->getSubNodes())) {
194 // Silently ignore sub nodes
198 // Construct method name
199 $methodName = 'finish' . $this->convertToClassName($nodeName);
201 // Call the corresponding method
202 call_user_func_array(array($this->imageInstance, $methodName), array());
208 * @param $resource XML parser resource (currently ignored)
209 * @param $characters Characters to handle
211 * @todo Find something usefull with this!
213 protected function characterHandler ($resource, $characters) {
214 // Trim all spaces away
215 $characters = trim($characters);
217 // Is this string empty?
218 if (empty($characters)) {
219 // Then skip it silently
224 $this->partialStub("Handling extra characters is not yet supported!");
228 * Intializes the image
231 * @todo Add cache creation here
233 private function initImage () {
240 * @param $imageType Code fragment or direct value holding the image type
243 private function setImageType ($imageType) {
244 // Set group to general
245 $this->setVariableGroup('general');
247 // Try to compile it first to get the value from variable stack
248 $imageType = $this->compileRawCode($imageType);
250 // Now make a class name of it
251 $className = $this->convertToClassName($imageType.'_image');
253 // And try to initiate it
254 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
256 // Set current main node to type
257 $this->currMainNode = 'type';
261 * "Setter" for resolution, we first need to collect the resolution from the
262 * sub-nodes. So first, this method will prepare an array for it
266 private function setImageResolution () {
267 // Call the image class
268 $this->imageInstance->initResolution();
270 // Current main node is resolution
271 $this->currMainNode = 'resolution';
275 * "Setter" for base information. For more details see above method!
278 * @see ImageTemplateEngine::setImageResolution
280 private function setImageBase () {
281 // Call the image class
282 $this->imageInstance->initBase();
284 // Current main node is resolution
285 $this->currMainNode = 'base';
289 * "Setter" for background-color. For more details see above method!
292 * @see ImageTemplateEngine::setImageResolution
294 private function setImageBackgroundColor () {
295 // Call the image class
296 $this->imageInstance->initBackgroundColor();
298 // Current main node is background-color
299 $this->currMainNode = 'background-color';
303 * "Setter" for foreground-color. For more details see above method!
306 * @see ImageTemplateEngine::setImageResolution
308 private function setImageForegroundColor () {
309 // Call the image class
310 $this->imageInstance->initForegroundColor();
312 // Current main node is foreground-color
313 $this->currMainNode = 'foreground-color';
317 * "Setter" for image-string. For more details see above method!
319 * @param $groupable Wether this image string is groupable
321 * @see ImageTemplateEngine::setImageResolution
323 private function setImageImageString ($groupable = 'single') {
324 // Call the image class
325 $this->imageInstance->initImageString($groupable);
327 // Current main node is foreground-color
328 $this->currMainNode = 'image-string';
332 * Setter for image name
334 * @param $imageName Name of the image
337 private function setImagePropertyName ($imageName) {
338 // Call the image class
339 $this->imageInstance->setImageName($imageName);
343 * Setter for image width
345 * @param $width Width of the image or variable
348 private function setImagePropertyWidth ($width) {
349 // Call the image class
350 $this->imageInstance->setWidth($width);
354 * Setter for image height
356 * @param $height Height of the image or variable
359 private function setImagePropertyHeight ($height) {
360 // Call the image class
361 $this->imageInstance->setHeight($height);
365 * Setter for image red color
367 * @param $red Red color value
370 private function setImagePropertyRed ($red) {
371 // Call the image class
372 $this->imageInstance->setRed($red);
376 * Setter for image green color
378 * @param $green Green color value
381 private function setImagePropertyGreen ($green) {
382 // Call the image class
383 $this->imageInstance->setGreen($green);
387 * Setter for image blue color
389 * @param $blue Blue color value
392 private function setImagePropertyBlue ($blue) {
393 // Call the image class
394 $this->imageInstance->setBlue($blue);
398 * Setter for string name (identifier)
400 * @param $stringName String name (identifier)
403 private function setImagePropertyStringName ($stringName) {
404 // Call the image class
405 $this->imageInstance->setStringName($stringName);
409 * Setter for font size
411 * @param $fontSize Size of the font
414 private function setImagePropertyFontSize ($fontSize) {
415 // Call the image class
416 $this->imageInstance->setFontSize($fontSize);
420 * Setter for image string
422 * @param $imageString Image string to set
425 private function setImagePropertyText ($imageString) {
426 // Call the image class
427 $this->imageInstance->setString($imageString);
431 * Setter for X coordinate
433 * @param $x X coordinate
436 private function setImagePropertyX ($x) {
437 // Call the image class
438 $this->imageInstance->setX($x);
442 * Setter for Y coordinate
444 * @param $y Y coordinate
447 private function setImagePropertyY ($y) {
448 // Call the image class
449 $this->imageInstance->setY($y);
453 * Getter for image cache file (FQFN)
455 * @return $fqfn Full-qualified file name of the image cache
457 public function getImageCacheFqfn () {
458 // Get the FQFN ready
459 $fqfn = $this->getBasePath().'_cache/' . md5($this->imageInstance->getImageName().":".$this->__toString().":".$this->imageInstance->__toString()) . "." . $this->imageInstance->getImageType();
466 * Outputs the image to the world
468 * @param $responseInstance An instance of a Responseable class
471 public function transferToResponse (Responseable $responseInstance) {
472 // Set the image instance
473 $responseInstance->setImageInstance($this->imageInstance);