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__);
56 * Creates an instance of the class TemplateEngine and prepares it for usage
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
69 public final static function createImageTemplateEngine ($basePath, ManageableLanguage $langInstance, FileIoHandler $ioInstance) {
71 $tplInstance = new ImageTemplateEngine();
73 // Is the base path valid?
74 if (empty($basePath)) {
76 throw new BasePathIsEmptyException($tplInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
77 } elseif (!is_string($basePath)) {
79 throw new InvalidBasePathStringException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_STRING);
80 } elseif (!is_dir($basePath)) {
82 throw new BasePathIsNoDirectoryException(array($tplInstance, $basePath), self::EXCEPTION_INVALID_PATH_NAME);
83 } elseif (!is_readable($basePath)) {
85 throw new BasePathReadProtectedException(array($tplInstance, $basePath), self::EXCEPTION_READ_PROTECED_PATH);
88 // Get configuration instance
89 $cfgInstance = FrameworkConfiguration::getInstance();
92 $tplInstance->setBasePath($basePath);
94 // Set the language and IO instances
95 $tplInstance->setLanguageInstance($langInstance);
96 $tplInstance->setFileIoInstance($ioInstance);
98 // Set template extensions
99 $tplInstance->setRawTemplateExtension($cfgInstance->readConfig('raw_template_extension'));
100 $tplInstance->setCodeTemplateExtension($cfgInstance->readConfig('code_template_extension'));
102 // Absolute output path for compiled templates
103 $tplInstance->setCompileOutputPath(PATH . $cfgInstance->readConfig('compile_output_path'));
105 // Return the prepared instance
110 * Getter for current main node
112 * @return $currMainNode Current main node
114 public final function getCurrMainNode () {
115 return $this->currMainNode;
119 * Getter for main node array
121 * @return $mainNodes Array with valid main node names
123 public final function getMainNodes () {
124 return $this->mainNodes;
128 * Getter for sub node array
130 * @return $subNodes Array with valid sub node names
132 public final function getSubNodes () {
133 return $this->subNodes;
137 * Handles the start element of an XML resource
139 * @param $resource XML parser resource (currently ignored)
140 * @param $element The element we shall handle
141 * @param $attributes All attributes
143 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
145 public function startElement ($resource, $element, array $attributes) {
146 // Initial method name which will never be called...
147 $methodName = 'initImage';
149 // Make the element name lower-case
150 $element = strtolower($element);
152 // Is the element a main node?
153 //* DEBUG: */ echo "START: >".$element."<<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)) {
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);
166 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
167 call_user_func_array(array($this, $methodName), $attributes);
171 * Ends the main or sub node by sending out the gathered data
173 * @param $resource An XML resource pointer (currently ignored)
174 * @param $nodeName Name of the node we want to finish
176 * @throws XmlNodeMismatchException If current main node mismatches the closing one
178 protected function endElement ($resource, $nodeName) {
179 // Make all lower-case
180 $nodeName = strtolower($nodeName);
182 // Does this match with current main node?
183 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
184 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
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
192 // Construct method name
193 $methodName = 'finish' . $this->convertToClassName($nodeName);
195 // Call the corresponding method
196 call_user_func_array(array($this->imageInstance, $methodName), array());
202 * @param $resource XML parser resource (currently ignored)
203 * @param $characters Characters to handle
205 * @todo Find something usefull with this!
207 protected function characterHandler ($resource, $characters) {
208 // Trim all spaces away
209 $characters = trim($characters);
211 // Is this string empty?
212 if (empty($characters)) {
213 // Then skip it silently
218 $this->partialStub("Handling extra characters is not yet supported!");
222 * Intializes the image
225 * @todo Add cache creation here
227 private function initImage () {
234 * @param $imageType Code fragment or direct value holding the image type
237 private function setImageType ($imageType) {
238 // Set group to general
239 $this->setVariableGroup('general');
241 // Try to compile it first to get the value from variable stack
242 $imageType = $this->compileRawCode($imageType);
244 // Now make a class name of it
245 $className = $this->convertToClassName($imageType.'_image');
247 // And try to initiate it
248 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
250 // Set current main node to type
251 $this->currMainNode = 'type';
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
260 private function setImageResolution () {
261 // Call the image class
262 $this->imageInstance->initResolution();
264 // Current main node is resolution
265 $this->currMainNode = 'resolution';
269 * "Setter" for base information. For more details see above method!
272 * @see ImageTemplateEngine::setImageResolution
274 private function setImageBase () {
275 // Call the image class
276 $this->imageInstance->initBase();
278 // Current main node is resolution
279 $this->currMainNode = 'base';
283 * "Setter" for background-color. For more details see above method!
286 * @see ImageTemplateEngine::setImageResolution
288 private function setImageBackgroundColor () {
289 // Call the image class
290 $this->imageInstance->initBackgroundColor();
292 // Current main node is background-color
293 $this->currMainNode = 'background-color';
297 * "Setter" for foreground-color. For more details see above method!
300 * @see ImageTemplateEngine::setImageResolution
302 private function setImageForegroundColor () {
303 // Call the image class
304 $this->imageInstance->initForegroundColor();
306 // Current main node is foreground-color
307 $this->currMainNode = 'foreground-color';
311 * "Setter" for image-string. For more details see above method!
313 * @param $groupable Wether this image string is groupable
315 * @see ImageTemplateEngine::setImageResolution
317 private function setImageImageString ($groupable = 'single') {
318 // Call the image class
319 $this->imageInstance->initImageString($groupable);
321 // Current main node is foreground-color
322 $this->currMainNode = 'image-string';
326 * Setter for image name
328 * @param $imageName Name of the image
331 private function setImagePropertyName ($imageName) {
332 // Call the image class
333 $this->imageInstance->setImageName($imageName);
337 * Setter for image width
339 * @param $width Width of the image or variable
342 private function setImagePropertyWidth ($width) {
343 // Call the image class
344 $this->imageInstance->setWidth($width);
348 * Setter for image height
350 * @param $height Height of the image or variable
353 private function setImagePropertyHeight ($height) {
354 // Call the image class
355 $this->imageInstance->setHeight($height);
359 * Setter for image red color
361 * @param $red Red color value
364 private function setImagePropertyRed ($red) {
365 // Call the image class
366 $this->imageInstance->setRed($red);
370 * Setter for image green color
372 * @param $green Green color value
375 private function setImagePropertyGreen ($green) {
376 // Call the image class
377 $this->imageInstance->setGreen($green);
381 * Setter for image blue color
383 * @param $blue Blue color value
386 private function setImagePropertyBlue ($blue) {
387 // Call the image class
388 $this->imageInstance->setBlue($blue);
392 * Setter for string name (identifier)
394 * @param $stringName String name (identifier)
397 private function setImagePropertyStringName ($stringName) {
398 // Call the image class
399 $this->imageInstance->setStringName($stringName);
403 * Setter for font size
405 * @param $fontSize Size of the font
408 private function setImagePropertyFontSize ($fontSize) {
409 // Call the image class
410 $this->imageInstance->setFontSize($fontSize);
414 * Setter for image string
416 * @param $imageString Image string to set
419 private function setImagePropertyText ($imageString) {
420 // Call the image class
421 $this->imageInstance->setString($imageString);
425 * Setter for X coordinate
427 * @param $x X coordinate
430 private function setImagePropertyX ($x) {
431 // Call the image class
432 $this->imageInstance->setX($x);
436 * Setter for Y coordinate
438 * @param $y Y coordinate
441 private function setImagePropertyY ($y) {
442 // Call the image class
443 $this->imageInstance->setY($y);
447 * Getter for image cache file (FQFN)
449 * @return $fqfn Full-qualified file name of the image cache
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();
460 * Outputs the image to the world
462 * @param $responseInstance An instance of a Responseable class
465 public function transferToResponse (Responseable $responseInstance) {
466 // Set the image instance
467 $responseInstance->setImageInstance($this->imageInstance);