3 * The own template engine for loading caching and sending out images
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 $currMainNode = '';
60 * Protected constructor
64 protected function __construct () {
65 // Call parent constructor
66 parent::__construct(__CLASS__);
70 * Creates an instance of the class TemplateEngine and prepares it for usage
72 * @return $templateInstance An instance of TemplateEngine
73 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
74 * @throws InvalidBasePathStringException If $templateBasePath is no string
75 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
76 * directory or not found
77 * @throws BasePathReadProtectedException If $templateBasePath is
80 public static final function createImageTemplateEngine () {
82 $templateInstance = new ImageTemplateEngine();
84 // Get the application instance from registry
85 $applicationInstance = Registry::getRegistry()->getInstance('app');
87 // Determine base path
88 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
90 // Is the base path valid?
91 if (empty($templateBasePath)) {
93 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
94 } elseif (!is_string($templateBasePath)) {
96 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
97 } elseif (!is_dir($templateBasePath)) {
99 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
100 } elseif (!is_readable($templateBasePath)) {
102 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
106 $templateInstance->setTemplateBasePath($templateBasePath);
108 // Set template extensions
109 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
110 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
112 // Absolute output path for compiled templates
113 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
115 // Return the prepared instance
116 return $templateInstance;
120 * Getter for current main node
122 * @return $currMainNode Current main node
124 public final function getCurrMainNode () {
125 return $this->currMainNode;
129 * Getter for main node array
131 * @return $mainNodes Array with valid main node names
133 public final function getMainNodes () {
134 return $this->mainNodes;
138 * Getter for sub node array
140 * @return $subNodes Array with valid sub node names
142 public final function getSubNodes () {
143 return $this->subNodes;
147 * Handles the start element of an XML resource
149 * @param $resource XML parser resource (currently ignored)
150 * @param $element The element we shall handle
151 * @param $attributes All attributes
153 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
155 public function startElement ($resource, $element, array $attributes) {
156 // Initial method name which will never be called...
157 $methodName = 'initImage';
159 // Make the element name lower-case
160 $element = strtolower($element);
162 // Is the element a main node?
163 //* DEBUG: */ echo "START: >".$element."<<br />\n";
164 if (in_array($element, $this->mainNodes)) {
165 // Okay, main node found!
166 $methodName = 'setImage' . $this->convertToClassName($element);
167 } elseif (in_array($element, $this->subNodes)) {
169 $methodName = 'setImageProperty' . $this->convertToClassName($element);
170 } elseif ($element != 'image') {
171 // Invalid node name found
172 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
176 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
177 call_user_func_array(array($this, $methodName), $attributes);
181 * Ends the main or sub node by sending out the gathered data
183 * @param $resource An XML resource pointer (currently ignored)
184 * @param $nodeName Name of the node we want to finish
186 * @throws XmlNodeMismatchException If current main node mismatches the closing one
188 public function finishElement ($resource, $nodeName) {
189 // Make all lower-case
190 $nodeName = strtolower($nodeName);
192 // Does this match with current main node?
193 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
194 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
196 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
197 } elseif (in_array($nodeName, $this->getSubNodes())) {
198 // Silently ignore sub nodes
202 // Construct method name
203 $methodName = 'finish' . $this->convertToClassName($nodeName);
205 // Call the corresponding method
206 call_user_func_array(array($this->getImageInstance(), $methodName), array());
212 * @param $resource XML parser resource (currently ignored)
213 * @param $characters Characters to handle
215 * @todo Find something usefull with this!
217 public function characterHandler ($resource, $characters) {
218 // Trim all spaces away
219 $characters = trim($characters);
221 // Is this string empty?
222 if (empty($characters)) {
223 // Then skip it silently
228 $this->partialStub('Handling extra characters is not yet supported!');
232 * Intializes the image
235 * @todo Add cache creation here
237 private function initImage () {
244 * @param $imageType Code fragment or direct value holding the image type
247 private function setImageType ($imageType) {
248 // Set group to general
249 $this->setVariableGroup('general');
251 // Try to compile it first to get the value from variable stack
252 $imageType = $this->compileRawCode($imageType);
254 // Now make a class name of it
255 $className = $this->convertToClassName($imageType.'_image');
257 // And try to initiate it
258 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
260 // Set current main node to type
261 $this->currMainNode = 'type';
265 * "Setter" for resolution, we first need to collect the resolution from the
266 * sub-nodes. So first, this method will prepare an array for it
270 private function setImageResolution () {
271 // Call the image class
272 $this->getImageInstance()->initResolution();
274 // Current main node is resolution
275 $this->currMainNode = 'resolution';
279 * "Setter" for base information. For more details see above method!
282 * @see ImageTemplateEngine::setImageResolution
284 private function setImageBase () {
285 // Call the image class
286 $this->getImageInstance()->initBase();
288 // Current main node is resolution
289 $this->currMainNode = 'base';
293 * "Setter" for background-color. For more details see above method!
296 * @see ImageTemplateEngine::setImageResolution
298 private function setImageBackgroundColor () {
299 // Call the image class
300 $this->getImageInstance()->initBackgroundColor();
302 // Current main node is background-color
303 $this->currMainNode = 'background-color';
307 * "Setter" for foreground-color. For more details see above method!
310 * @see ImageTemplateEngine::setImageResolution
312 private function setImageForegroundColor () {
313 // Call the image class
314 $this->getImageInstance()->initForegroundColor();
316 // Current main node is foreground-color
317 $this->currMainNode = 'foreground-color';
321 * "Setter" for image-string. For more details see above method!
323 * @param $groupable Whether this image string is groupable
325 * @see ImageTemplateEngine::setImageResolution
327 private function setImageImageString ($groupable = 'single') {
328 // Call the image class
329 $this->getImageInstance()->initImageString($groupable);
331 // Current main node is foreground-color
332 $this->currMainNode = 'image-string';
336 * Setter for image name
338 * @param $imageName Name of the image
341 private function setImagePropertyName ($imageName) {
342 // Call the image class
343 $this->getImageInstance()->setImageName($imageName);
347 * Setter for image width
349 * @param $width Width of the image or variable
352 private function setImagePropertyWidth ($width) {
353 // Call the image class
354 $this->getImageInstance()->setWidth($width);
358 * Setter for image height
360 * @param $height Height of the image or variable
363 private function setImagePropertyHeight ($height) {
364 // Call the image class
365 $this->getImageInstance()->setHeight($height);
369 * Setter for image red color
371 * @param $red Red color value
374 private function setImagePropertyRed ($red) {
375 // Call the image class
376 $this->getImageInstance()->setRed($red);
380 * Setter for image green color
382 * @param $green Green color value
385 private function setImagePropertyGreen ($green) {
386 // Call the image class
387 $this->getImageInstance()->setGreen($green);
391 * Setter for image blue color
393 * @param $blue Blue color value
396 private function setImagePropertyBlue ($blue) {
397 // Call the image class
398 $this->getImageInstance()->setBlue($blue);
402 * Setter for string name (identifier)
404 * @param $stringName String name (identifier)
407 private function setImagePropertyStringName ($stringName) {
408 // Call the image class
409 $this->getImageInstance()->setStringName($stringName);
413 * Setter for font size
415 * @param $fontSize Size of the font
418 private function setImagePropertyFontSize ($fontSize) {
419 // Call the image class
420 $this->getImageInstance()->setFontSize($fontSize);
424 * Setter for image string
426 * @param $imageString Image string to set
429 private function setImagePropertyText ($imageString) {
430 // Call the image class
431 $this->getImageInstance()->setString($imageString);
435 * Setter for X coordinate
437 * @param $x X coordinate
440 private function setImagePropertyX ($x) {
441 // Call the image class
442 $this->getImageInstance()->setX($x);
446 * Setter for Y coordinate
448 * @param $y Y coordinate
451 private function setImagePropertyY ($y) {
452 // Call the image class
453 $this->getImageInstance()->setY($y);
457 * Getter for image cache file (FQFN)
459 * @return $fqfn Full-qualified file name of the image cache
461 public function getImageCacheFqfn () {
462 // Get the FQFN ready
463 $fqfn = sprintf("%s%s%s/%s.%s",
464 $this->getConfigInstance()->getConfigEntry('base_path'),
465 $this->getGenericBasePath(),
468 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
470 $this->getImageInstance()->getImageType()
478 * Outputs the image to the world
480 * @param $responseInstance An instance of a Responseable class
483 public function transferToResponse (Responseable $responseInstance) {
484 // Set the image instance
485 $responseInstance->setImageInstance($this->getImageInstance());
489 * Load a specified image template into the engine
491 * @param $template The image template we shall load which is
492 * located in 'image' by default
495 public function loadImageTemplate ($template) {
497 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
499 // Load the special template
500 $this->loadTemplate($template);