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 * Handles the start element of an XML resource
118 * @param $resource XML parser resource (currently ignored)
119 * @param $element The element we shall handle
120 * @param $attributes All attributes
122 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
124 public function startElement ($resource, $element, array $attributes) {
125 // Initial method name which will never be called...
126 $methodName = 'initImage';
128 // Make the element name lower-case
129 $element = strtolower($element);
131 // Is the element a main node?
132 //* DEBUG: */ echo "START: >".$element."<<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)) {
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);
145 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
146 call_user_func_array(array($this, $methodName), $attributes);
150 * Ends the main or sub node by sending out the gathered data
152 * @param $resource An XML resource pointer (currently ignored)
153 * @param $nodeName Name of the node we want to finish
155 * @throws XmlNodeMismatchException If current main node mismatches the closing one
157 protected function endElement ($resource, $nodeName) {
158 // Make all lower-case
159 $nodeName = strtolower($nodeName);
161 // Does this match with current main node?
162 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
163 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
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
171 // Construct method name
172 $methodName = 'finish' . $this->convertToClassName($nodeName);
174 // Call the corresponding method
175 call_user_func_array(array($this->imageInstance, $methodName), array());
181 * @param $resource XML parser resource (currently ignored)
182 * @param $characters Characters to handle
184 * @todo Find something usefull with this!
186 protected function characterHandler ($resource, $characters) {
187 // Trim all spaces away
188 $characters = trim($characters);
190 // Is this string empty?
191 if (empty($characters)) {
192 // Then skip it silently
197 $this->partialStub("Handling extra characters is not yet supported!");
201 * Intializes the image
204 * @todo Add cache creation here
206 private function initImage () {
213 * @param $imageType Code fragment or direct value holding the image type
216 private function setImageType ($imageType) {
217 // Set group to general
218 $this->setVariableGroup('general');
220 // Try to compile it first to get the value from variable stack
221 $imageType = $this->compileRawCode($imageType);
223 // Now make a class name of it
224 $className = $this->convertToClassName($imageType.'_image');
226 // And try to initiate it
227 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
229 // Set current main node to type
230 $this->currMainNode = 'type';
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
239 private function setImageResolution () {
240 // Call the image class
241 $this->imageInstance->initResolution();
243 // Current main node is resolution
244 $this->currMainNode = 'resolution';
248 * "Setter" for base information. For more details see above method!
251 * @see ImageTemplateEngine::setImageResolution
253 private function setImageBase () {
254 // Call the image class
255 $this->imageInstance->initBase();
257 // Current main node is resolution
258 $this->currMainNode = 'base';
262 * "Setter" for background-color. For more details see above method!
265 * @see ImageTemplateEngine::setImageResolution
267 private function setImageBackgroundColor () {
268 // Call the image class
269 $this->imageInstance->initBackgroundColor();
271 // Current main node is background-color
272 $this->currMainNode = 'background-color';
276 * "Setter" for foreground-color. For more details see above method!
279 * @see ImageTemplateEngine::setImageResolution
281 private function setImageForegroundColor () {
282 // Call the image class
283 $this->imageInstance->initForegroundColor();
285 // Current main node is foreground-color
286 $this->currMainNode = 'foreground-color';
290 * "Setter" for image-string. For more details see above method!
292 * @param $groupable Wether this image string is groupable
294 * @see ImageTemplateEngine::setImageResolution
296 private function setImageImageString ($groupable = 'single') {
297 // Call the image class
298 $this->imageInstance->initImageString($groupable);
300 // Current main node is foreground-color
301 $this->currMainNode = 'image-string';
305 * Setter for image name
307 * @param $imageName Name of the image
310 private function setImagePropertyName ($imageName) {
311 // Call the image class
312 $this->imageInstance->setImageName($imageName);
316 * Setter for image width
318 * @param $width Width of the image or variable
321 private function setImagePropertyWidth ($width) {
322 // Call the image class
323 $this->imageInstance->setWidth($width);
327 * Setter for image height
329 * @param $height Height of the image or variable
332 private function setImagePropertyHeight ($height) {
333 // Call the image class
334 $this->imageInstance->setHeight($height);
338 * Setter for image red color
340 * @param $red Red color value
343 private function setImagePropertyRed ($red) {
344 // Call the image class
345 $this->imageInstance->setRed($red);
349 * Setter for image green color
351 * @param $green Green color value
354 private function setImagePropertyGreen ($green) {
355 // Call the image class
356 $this->imageInstance->setGreen($green);
360 * Setter for image blue color
362 * @param $blue Blue color value
365 private function setImagePropertyBlue ($blue) {
366 // Call the image class
367 $this->imageInstance->setBlue($blue);
371 * Setter for string name (identifier)
373 * @param $stringName String name (identifier)
376 private function setImagePropertyStringName ($stringName) {
377 // Call the image class
378 $this->imageInstance->setStringName($stringName);
382 * Setter for font size
384 * @param $fontSize Size of the font
387 private function setImagePropertyFontSize ($fontSize) {
388 // Call the image class
389 $this->imageInstance->setFontSize($fontSize);
393 * Setter for image string
395 * @param $imageString Image string to set
398 private function setImagePropertyText ($imageString) {
399 // Call the image class
400 $this->imageInstance->setString($imageString);
404 * Setter for X coordinate
406 * @param $x X coordinate
409 private function setImagePropertyX ($x) {
410 // Call the image class
411 $this->imageInstance->setX($x);
415 * Setter for Y coordinate
417 * @param $y Y coordinate
420 private function setImagePropertyY ($y) {
421 // Call the image class
422 $this->imageInstance->setY($y);
426 * Getter for image cache file (FQFN)
428 * @return $fqfn Full-qualified file name of the image cache
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();
439 * Outputs the image to the world
441 * @param $responseInstance An instance of a Responseable class
444 public function transferToResponse (Responseable $responseInstance) {
445 // Set the image instance
446 $responseInstance->setImageInstance($this->imageInstance);