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 - 2009 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(
38 * Sub nodes in the XML tree
40 private $subNodes = array(
57 private $imageInstance = null;
62 private $currMainNode = '';
65 * Protected constructor
69 protected function __construct () {
70 // Call parent constructor
71 parent::__construct(__CLASS__);
75 * Creates an instance of the class TemplateEngine and prepares it for usage
77 * @param $basePath The local base path for all templates
78 * @param $langInstance An instance of LanguageSystem (default)
79 * @param $ioInstance An instance of FileIoHandler (default, middleware!)
80 * @return $tplInstance An instance of TemplateEngine
81 * @throws BasePathIsEmptyException If the provided $basePath is empty
82 * @throws InvalidBasePathStringException If $basePath is no string
83 * @throws BasePathIsNoDirectoryException If $basePath is no
84 * directory or not found
85 * @throws BasePathReadProtectedException If $basePath is
88 public final static function createImageTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
90 $tplInstance = new ImageTemplateEngine();
92 // Is the base path valid?
93 if (empty($basePath)) {
95 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
96 } elseif (!is_string($basePath)) {
98 throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
99 } elseif (!is_dir($basePath)) {
101 throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
102 } elseif (!is_readable($basePath)) {
104 throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
107 // Get configuration instance
108 $configInstance = FrameworkConfiguration::getInstance();
111 $tplInstance->setBasePath($basePath);
113 // Set the language and IO instances
114 $tplInstance->setLanguageInstance($langInstance);
115 $tplInstance->setFileIoInstance($ioInstance);
117 // Set template extensions
118 $tplInstance->setRawTemplateExtension($configInstance->readConfig('raw_template_extension'));
119 $tplInstance->setCodeTemplateExtension($configInstance->readConfig('code_template_extension'));
121 // Absolute output path for compiled templates
122 $tplInstance->setCompileOutputPath($configInstance->readConfig('base_path') . $configInstance->readConfig('compile_output_path'));
124 // Return the prepared instance
129 * Getter for current main node
131 * @return $currMainNode Current main node
133 public final function getCurrMainNode () {
134 return $this->currMainNode;
138 * Getter for main node array
140 * @return $mainNodes Array with valid main node names
142 public final function getMainNodes () {
143 return $this->mainNodes;
147 * Getter for sub node array
149 * @return $subNodes Array with valid sub node names
151 public final function getSubNodes () {
152 return $this->subNodes;
156 * Handles the start element of an XML resource
158 * @param $resource XML parser resource (currently ignored)
159 * @param $element The element we shall handle
160 * @param $attributes All attributes
162 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
164 public function startElement ($resource, $element, array $attributes) {
165 // Initial method name which will never be called...
166 $methodName = 'initImage';
168 // Make the element name lower-case
169 $element = strtolower($element);
171 // Is the element a main node?
172 //* DEBUG: */ echo "START: >".$element."<<br />\n";
173 if (in_array($element, $this->mainNodes)) {
174 // Okay, main node found!
175 $methodName = 'setImage' . $this->convertToClassName($element);
176 } elseif (in_array($element, $this->subNodes)) {
178 $methodName = 'setImageProperty' . $this->convertToClassName($element);
179 } elseif ($element != 'image') {
180 // Invalid node name found
181 throw new InvalidXmlNodeException(array($this, $element, $attributes), BaseHelper::EXCEPTION_XML_NODE_UNKNOWN);
185 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
186 call_user_func_array(array($this, $methodName), $attributes);
190 * Ends the main or sub node by sending out the gathered data
192 * @param $resource An XML resource pointer (currently ignored)
193 * @param $nodeName Name of the node we want to finish
195 * @throws XmlNodeMismatchException If current main node mismatches the closing one
197 protected function endElement ($resource, $nodeName) {
198 // Make all lower-case
199 $nodeName = strtolower($nodeName);
201 // Does this match with current main node?
202 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
203 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
205 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), BaseHelper::EXCEPTION_XML_NODE_MISMATCH);
206 } elseif (in_array($nodeName, $this->getSubNodes())) {
207 // Silently ignore sub nodes
211 // Construct method name
212 $methodName = 'finish' . $this->convertToClassName($nodeName);
214 // Call the corresponding method
215 call_user_func_array(array($this->imageInstance, $methodName), array());
221 * @param $resource XML parser resource (currently ignored)
222 * @param $characters Characters to handle
224 * @todo Find something usefull with this!
226 protected function characterHandler ($resource, $characters) {
227 // Trim all spaces away
228 $characters = trim($characters);
230 // Is this string empty?
231 if (empty($characters)) {
232 // Then skip it silently
237 $this->partialStub("Handling extra characters is not yet supported!");
241 * Intializes the image
244 * @todo Add cache creation here
246 private function initImage () {
253 * @param $imageType Code fragment or direct value holding the image type
256 private function setImageType ($imageType) {
257 // Set group to general
258 $this->setVariableGroup('general');
260 // Try to compile it first to get the value from variable stack
261 $imageType = $this->compileRawCode($imageType);
263 // Now make a class name of it
264 $className = $this->convertToClassName($imageType.'_image');
266 // And try to initiate it
267 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
269 // Set current main node to type
270 $this->currMainNode = 'type';
274 * "Setter" for resolution, we first need to collect the resolution from the
275 * sub-nodes. So first, this method will prepare an array for it
279 private function setImageResolution () {
280 // Call the image class
281 $this->imageInstance->initResolution();
283 // Current main node is resolution
284 $this->currMainNode = 'resolution';
288 * "Setter" for base information. For more details see above method!
291 * @see ImageTemplateEngine::setImageResolution
293 private function setImageBase () {
294 // Call the image class
295 $this->imageInstance->initBase();
297 // Current main node is resolution
298 $this->currMainNode = 'base';
302 * "Setter" for background-color. For more details see above method!
305 * @see ImageTemplateEngine::setImageResolution
307 private function setImageBackgroundColor () {
308 // Call the image class
309 $this->imageInstance->initBackgroundColor();
311 // Current main node is background-color
312 $this->currMainNode = 'background-color';
316 * "Setter" for foreground-color. For more details see above method!
319 * @see ImageTemplateEngine::setImageResolution
321 private function setImageForegroundColor () {
322 // Call the image class
323 $this->imageInstance->initForegroundColor();
325 // Current main node is foreground-color
326 $this->currMainNode = 'foreground-color';
330 * "Setter" for image-string. For more details see above method!
332 * @param $groupable Wether this image string is groupable
334 * @see ImageTemplateEngine::setImageResolution
336 private function setImageImageString ($groupable = 'single') {
337 // Call the image class
338 $this->imageInstance->initImageString($groupable);
340 // Current main node is foreground-color
341 $this->currMainNode = 'image-string';
345 * Setter for image name
347 * @param $imageName Name of the image
350 private function setImagePropertyName ($imageName) {
351 // Call the image class
352 $this->imageInstance->setImageName($imageName);
356 * Setter for image width
358 * @param $width Width of the image or variable
361 private function setImagePropertyWidth ($width) {
362 // Call the image class
363 $this->imageInstance->setWidth($width);
367 * Setter for image height
369 * @param $height Height of the image or variable
372 private function setImagePropertyHeight ($height) {
373 // Call the image class
374 $this->imageInstance->setHeight($height);
378 * Setter for image red color
380 * @param $red Red color value
383 private function setImagePropertyRed ($red) {
384 // Call the image class
385 $this->imageInstance->setRed($red);
389 * Setter for image green color
391 * @param $green Green color value
394 private function setImagePropertyGreen ($green) {
395 // Call the image class
396 $this->imageInstance->setGreen($green);
400 * Setter for image blue color
402 * @param $blue Blue color value
405 private function setImagePropertyBlue ($blue) {
406 // Call the image class
407 $this->imageInstance->setBlue($blue);
411 * Setter for string name (identifier)
413 * @param $stringName String name (identifier)
416 private function setImagePropertyStringName ($stringName) {
417 // Call the image class
418 $this->imageInstance->setStringName($stringName);
422 * Setter for font size
424 * @param $fontSize Size of the font
427 private function setImagePropertyFontSize ($fontSize) {
428 // Call the image class
429 $this->imageInstance->setFontSize($fontSize);
433 * Setter for image string
435 * @param $imageString Image string to set
438 private function setImagePropertyText ($imageString) {
439 // Call the image class
440 $this->imageInstance->setString($imageString);
444 * Setter for X coordinate
446 * @param $x X coordinate
449 private function setImagePropertyX ($x) {
450 // Call the image class
451 $this->imageInstance->setX($x);
455 * Setter for Y coordinate
457 * @param $y Y coordinate
460 private function setImagePropertyY ($y) {
461 // Call the image class
462 $this->imageInstance->setY($y);
466 * Getter for image cache file (FQFN)
468 * @return $fqfn Full-qualified file name of the image cache
470 public function getImageCacheFqfn () {
471 // Get the FQFN ready
472 $fqfn = $this->getBasePath().'_cache/' . md5($this->imageInstance->getImageName().':'.$this->__toString().':'.$this->imageInstance->__toString()) . '.' . $this->imageInstance->getImageType();
479 * Outputs the image to the world
481 * @param $responseInstance An instance of a Responseable class
484 public function transferToResponse (Responseable $responseInstance) {
485 // Set the image instance
486 $responseInstance->setImageInstance($this->imageInstance);