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, 2009 Core Developer Team
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 $appInstance A manageable application
78 * @return $tplInstance An instance of TemplateEngine
79 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
80 * @throws InvalidBasePathStringException If $templateBasePath is no string
81 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
82 * directory or not found
83 * @throws BasePathReadProtectedException If $templateBasePath is
86 public final static function createImageTemplateEngine (ManageableApplication $appInstance) {
88 $templateInstance = new ImageTemplateEngine();
90 // Get language and file I/O instances from application
91 $langInstance = $appInstance->getLanguageInstance();
92 $ioInstance = $appInstance->getFileIoInstance();
94 // Determine base path
95 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $appInstance->getRequestInstance()->getRequestElement('app') . '/';
97 // Is the base path valid?
98 if (empty($templateBasePath)) {
100 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
101 } elseif (!is_string($templateBasePath)) {
103 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
104 } elseif (!is_dir($templateBasePath)) {
106 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
107 } elseif (!is_readable($templateBasePath)) {
109 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
112 // Get configuration instance
113 $configInstance = FrameworkConfiguration::getInstance();
116 $templateInstance->setTemplateBasePath($templateBasePath);
118 // Set the language and IO instances
119 $templateInstance->setLanguageInstance($langInstance);
120 $templateInstance->setFileIoInstance($ioInstance);
122 // Set template extensions
123 $templateInstance->setRawTemplateExtension($configInstance->getConfigEntry('raw_template_extension'));
124 $templateInstance->setCodeTemplateExtension($configInstance->getConfigEntry('code_template_extension'));
126 // Absolute output path for compiled templates
127 $templateInstance->setCompileOutputPath($configInstance->getConfigEntry('base_path') . $configInstance->getConfigEntry('compile_output_path'));
129 // Return the prepared instance
130 return $templateInstance;
134 * Getter for current main node
136 * @return $currMainNode Current main node
138 public final function getCurrMainNode () {
139 return $this->currMainNode;
143 * Getter for main node array
145 * @return $mainNodes Array with valid main node names
147 public final function getMainNodes () {
148 return $this->mainNodes;
152 * Getter for sub node array
154 * @return $subNodes Array with valid sub node names
156 public final function getSubNodes () {
157 return $this->subNodes;
161 * Handles the start element of an XML resource
163 * @param $resource XML parser resource (currently ignored)
164 * @param $element The element we shall handle
165 * @param $attributes All attributes
167 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
169 public function startElement ($resource, $element, array $attributes) {
170 // Initial method name which will never be called...
171 $methodName = 'initImage';
173 // Make the element name lower-case
174 $element = strtolower($element);
176 // Is the element a main node?
177 //* DEBUG: */ echo "START: >".$element."<<br />\n";
178 if (in_array($element, $this->mainNodes)) {
179 // Okay, main node found!
180 $methodName = 'setImage' . $this->convertToClassName($element);
181 } elseif (in_array($element, $this->subNodes)) {
183 $methodName = 'setImageProperty' . $this->convertToClassName($element);
184 } elseif ($element != 'image') {
185 // Invalid node name found
186 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
190 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
191 call_user_func_array(array($this, $methodName), $attributes);
195 * Ends the main or sub node by sending out the gathered data
197 * @param $resource An XML resource pointer (currently ignored)
198 * @param $nodeName Name of the node we want to finish
200 * @throws XmlNodeMismatchException If current main node mismatches the closing one
202 public function endElement ($resource, $nodeName) {
203 // Make all lower-case
204 $nodeName = strtolower($nodeName);
206 // Does this match with current main node?
207 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
208 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
210 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
211 } elseif (in_array($nodeName, $this->getSubNodes())) {
212 // Silently ignore sub nodes
216 // Construct method name
217 $methodName = 'finish' . $this->convertToClassName($nodeName);
219 // Call the corresponding method
220 call_user_func_array(array($this->imageInstance, $methodName), array());
226 * @param $resource XML parser resource (currently ignored)
227 * @param $characters Characters to handle
229 * @todo Find something usefull with this!
231 public function characterHandler ($resource, $characters) {
232 // Trim all spaces away
233 $characters = trim($characters);
235 // Is this string empty?
236 if (empty($characters)) {
237 // Then skip it silently
242 $this->partialStub("Handling extra characters is not yet supported!");
246 * Intializes the image
249 * @todo Add cache creation here
251 private function initImage () {
258 * @param $imageType Code fragment or direct value holding the image type
261 private function setImageType ($imageType) {
262 // Set group to general
263 $this->setVariableGroup('general');
265 // Try to compile it first to get the value from variable stack
266 $imageType = $this->compileRawCode($imageType);
268 // Now make a class name of it
269 $className = $this->convertToClassName($imageType.'_image');
271 // And try to initiate it
272 $this->imageInstance = ObjectFactory::createObjectByName($className, array($this));
274 // Set current main node to type
275 $this->currMainNode = 'type';
279 * "Setter" for resolution, we first need to collect the resolution from the
280 * sub-nodes. So first, this method will prepare an array for it
284 private function setImageResolution () {
285 // Call the image class
286 $this->imageInstance->initResolution();
288 // Current main node is resolution
289 $this->currMainNode = 'resolution';
293 * "Setter" for base information. For more details see above method!
296 * @see ImageTemplateEngine::setImageResolution
298 private function setImageBase () {
299 // Call the image class
300 $this->imageInstance->initBase();
302 // Current main node is resolution
303 $this->currMainNode = 'base';
307 * "Setter" for background-color. For more details see above method!
310 * @see ImageTemplateEngine::setImageResolution
312 private function setImageBackgroundColor () {
313 // Call the image class
314 $this->imageInstance->initBackgroundColor();
316 // Current main node is background-color
317 $this->currMainNode = 'background-color';
321 * "Setter" for foreground-color. For more details see above method!
324 * @see ImageTemplateEngine::setImageResolution
326 private function setImageForegroundColor () {
327 // Call the image class
328 $this->imageInstance->initForegroundColor();
330 // Current main node is foreground-color
331 $this->currMainNode = 'foreground-color';
335 * "Setter" for image-string. For more details see above method!
337 * @param $groupable Wether this image string is groupable
339 * @see ImageTemplateEngine::setImageResolution
341 private function setImageImageString ($groupable = 'single') {
342 // Call the image class
343 $this->imageInstance->initImageString($groupable);
345 // Current main node is foreground-color
346 $this->currMainNode = 'image-string';
350 * Setter for image name
352 * @param $imageName Name of the image
355 private function setImagePropertyName ($imageName) {
356 // Call the image class
357 $this->imageInstance->setImageName($imageName);
361 * Setter for image width
363 * @param $width Width of the image or variable
366 private function setImagePropertyWidth ($width) {
367 // Call the image class
368 $this->imageInstance->setWidth($width);
372 * Setter for image height
374 * @param $height Height of the image or variable
377 private function setImagePropertyHeight ($height) {
378 // Call the image class
379 $this->imageInstance->setHeight($height);
383 * Setter for image red color
385 * @param $red Red color value
388 private function setImagePropertyRed ($red) {
389 // Call the image class
390 $this->imageInstance->setRed($red);
394 * Setter for image green color
396 * @param $green Green color value
399 private function setImagePropertyGreen ($green) {
400 // Call the image class
401 $this->imageInstance->setGreen($green);
405 * Setter for image blue color
407 * @param $blue Blue color value
410 private function setImagePropertyBlue ($blue) {
411 // Call the image class
412 $this->imageInstance->setBlue($blue);
416 * Setter for string name (identifier)
418 * @param $stringName String name (identifier)
421 private function setImagePropertyStringName ($stringName) {
422 // Call the image class
423 $this->imageInstance->setStringName($stringName);
427 * Setter for font size
429 * @param $fontSize Size of the font
432 private function setImagePropertyFontSize ($fontSize) {
433 // Call the image class
434 $this->imageInstance->setFontSize($fontSize);
438 * Setter for image string
440 * @param $imageString Image string to set
443 private function setImagePropertyText ($imageString) {
444 // Call the image class
445 $this->imageInstance->setString($imageString);
449 * Setter for X coordinate
451 * @param $x X coordinate
454 private function setImagePropertyX ($x) {
455 // Call the image class
456 $this->imageInstance->setX($x);
460 * Setter for Y coordinate
462 * @param $y Y coordinate
465 private function setImagePropertyY ($y) {
466 // Call the image class
467 $this->imageInstance->setY($y);
471 * Getter for image cache file (FQFN)
473 * @return $fqfn Full-qualified file name of the image cache
475 public function getImageCacheFqfn () {
476 // Get the FQFN ready
477 $fqfn = sprintf("%s%s%s/%s.%s",
478 $this->getConfigInstance()->getConfigEntry('base_path'),
479 $this->getGenericBasePath(),
482 $this->imageInstance->getImageName() . ':' . $this->__toString() . ':' . $this->imageInstance->__toString()
484 $this->imageInstance->getImageType()
492 * Outputs the image to the world
494 * @param $responseInstance An instance of a Responseable class
497 public function transferToResponse (Responseable $responseInstance) {
498 // Set the image instance
499 $responseInstance->setImageInstance($this->imageInstance);
503 * Load a specified image template into the engine
505 * @param $template The image template we shall load which is
506 * located in 'image' by default
509 public function loadImageTemplate ($template) {
511 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
513 // Load the special template
514 $this->loadTemplate($template);