3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Parser\Xml\XmlParser;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Response\Responseable;
10 use CoreFramework\Template\CompileableTemplate;
13 * The own template engine for loading caching and sending out images
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
36 * Main nodes in the XML tree ('image' is ignored)
38 private $mainNodes = array(
48 * Sub nodes in the XML tree
50 private $subNodes = array(
67 private $currMainNode = '';
70 * Protected constructor
74 protected function __construct () {
75 // Call parent constructor
76 parent::__construct(__CLASS__);
80 * Creates an instance of the class TemplateEngine and prepares it for usage
82 * @return $templateInstance An instance of TemplateEngine
83 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
84 * @throws InvalidBasePathStringException If $templateBasePath is no string
85 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
86 * directory or not found
87 * @throws BasePathReadProtectedException If $templateBasePath is
90 public static final function createImageTemplateEngine () {
92 $templateInstance = new ImageTemplateEngine();
94 // Get the application instance from registry
95 $applicationInstance = Registry::getRegistry()->getInstance('app');
97 // Determine base path
98 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
100 // Is the base path valid?
101 if (empty($templateBasePath)) {
102 // Base path is empty
103 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
104 } elseif (!is_string($templateBasePath)) {
106 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
107 } elseif (!is_dir($templateBasePath)) {
109 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
110 } elseif (!is_readable($templateBasePath)) {
112 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
116 $templateInstance->setTemplateBasePath($templateBasePath);
118 // Set template extensions
119 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
120 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
122 // Absolute output path for compiled templates
123 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
125 // Return the prepared instance
126 return $templateInstance;
130 * Getter for current main node
132 * @return $currMainNode Current main node
134 public final function getCurrMainNode () {
135 return $this->currMainNode;
139 * Getter for main node array
141 * @return $mainNodes Array with valid main node names
143 public final function getMainNodes () {
144 return $this->mainNodes;
148 * Getter for sub node array
150 * @return $subNodes Array with valid sub node names
152 public final function getSubNodes () {
153 return $this->subNodes;
157 * Handles the start element of an XML resource
159 * @param $resource XML parser resource (currently ignored)
160 * @param $element The element we shall handle
161 * @param $attributes All attributes
163 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
165 public function startElement ($resource, $element, array $attributes) {
166 // Initial method name which will never be called...
167 $methodName = 'initImage';
169 // Make the element name lower-case
170 $element = strtolower($element);
172 // Is the element a main node?
173 //* DEBUG: */ echo "START: >".$element."<<br />\n";
174 if (in_array($element, $this->mainNodes)) {
175 // Okay, main node found!
176 $methodName = 'setImage' . self::convertToClassName($element);
177 } elseif (in_array($element, $this->subNodes)) {
179 $methodName = 'setImageProperty' . self::convertToClassName($element);
180 } elseif ($element != 'image') {
181 // Invalid node name found
182 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
186 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
187 call_user_func_array(array($this, $methodName), $attributes);
191 * Ends the main or sub node by sending out the gathered data
193 * @param $resource An XML resource pointer (currently ignored)
194 * @param $nodeName Name of the node we want to finish
196 * @throws XmlNodeMismatchException If current main node mismatches the closing one
198 public function finishElement ($resource, $nodeName) {
199 // Make all lower-case
200 $nodeName = strtolower($nodeName);
202 // Does this match with current main node?
203 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
204 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
206 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
207 } elseif (in_array($nodeName, $this->getSubNodes())) {
208 // Silently ignore sub nodes
212 // Construct method name
213 $methodName = 'finish' . self::convertToClassName($nodeName);
215 // Call the corresponding method
216 call_user_func_array(array($this->getImageInstance(), $methodName), array());
222 * @param $resource XML parser resource (currently ignored)
223 * @param $characters Characters to handle
225 * @todo Find something usefull with this!
227 public function characterHandler ($resource, $characters) {
228 // Trim all spaces away
229 $characters = trim($characters);
231 // Is this string empty?
232 if (empty($characters)) {
233 // Then skip it silently
238 $this->partialStub('Handling extra characters is not yet supported!');
242 * Intializes the image
245 * @todo Add cache creation here
247 private function initImage () {
254 * @param $imageType Code fragment or direct value holding the image type
257 private function setImageType ($imageType) {
258 // Set group to general
259 $this->setVariableGroup('general');
261 // Try to compile it first to get the value from variable stack
262 $imageType = $this->compileRawCode($imageType);
264 // Now make a class name of it
265 $className = self::convertToClassName($imageType.'_image');
267 // And try to initiate it
268 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
270 // Set current main node to type
271 $this->currMainNode = 'type';
275 * "Setter" for resolution, we first need to collect the resolution from the
276 * sub-nodes. So first, this method will prepare an array for it
280 private function setImageResolution () {
281 // Call the image class
282 $this->getImageInstance()->initResolution();
284 // Current main node is resolution
285 $this->currMainNode = 'resolution';
289 * "Setter" for base information. For more details see above method!
292 * @see ImageTemplateEngine::setImageResolution
294 private function setImageBase () {
295 // Call the image class
296 $this->getImageInstance()->initBase();
298 // Current main node is resolution
299 $this->currMainNode = 'base';
303 * "Setter" for background-color. For more details see above method!
306 * @see ImageTemplateEngine::setImageResolution
308 private function setImageBackgroundColor () {
309 // Call the image class
310 $this->getImageInstance()->initBackgroundColor();
312 // Current main node is background-color
313 $this->currMainNode = 'background-color';
317 * "Setter" for foreground-color. For more details see above method!
320 * @see ImageTemplateEngine::setImageResolution
322 private function setImageForegroundColor () {
323 // Call the image class
324 $this->getImageInstance()->initForegroundColor();
326 // Current main node is foreground-color
327 $this->currMainNode = 'foreground-color';
331 * "Setter" for image-string. For more details see above method!
333 * @param $groupable Whether this image string is groupable
335 * @see ImageTemplateEngine::setImageResolution
337 private function setImageImageString ($groupable = 'single') {
338 // Call the image class
339 $this->getImageInstance()->initImageString($groupable);
341 // Current main node is foreground-color
342 $this->currMainNode = 'image-string';
346 * Setter for image name
348 * @param $imageName Name of the image
351 private function setImagePropertyName ($imageName) {
352 // Call the image class
353 $this->getImageInstance()->setImageName($imageName);
357 * Setter for image width
359 * @param $width Width of the image or variable
362 private function setImagePropertyWidth ($width) {
363 // Call the image class
364 $this->getImageInstance()->setWidth($width);
368 * Setter for image height
370 * @param $height Height of the image or variable
373 private function setImagePropertyHeight ($height) {
374 // Call the image class
375 $this->getImageInstance()->setHeight($height);
379 * Setter for image red color
381 * @param $red Red color value
384 private function setImagePropertyRed ($red) {
385 // Call the image class
386 $this->getImageInstance()->setRed($red);
390 * Setter for image green color
392 * @param $green Green color value
395 private function setImagePropertyGreen ($green) {
396 // Call the image class
397 $this->getImageInstance()->setGreen($green);
401 * Setter for image blue color
403 * @param $blue Blue color value
406 private function setImagePropertyBlue ($blue) {
407 // Call the image class
408 $this->getImageInstance()->setBlue($blue);
412 * Setter for string name (identifier)
414 * @param $stringName String name (identifier)
417 private function setImagePropertyStringName ($stringName) {
418 // Call the image class
419 $this->getImageInstance()->setStringName($stringName);
423 * Setter for font size
425 * @param $fontSize Size of the font
428 private function setImagePropertyFontSize ($fontSize) {
429 // Call the image class
430 $this->getImageInstance()->setFontSize($fontSize);
434 * Setter for image string
436 * @param $imageString Image string to set
439 private function setImagePropertyText ($imageString) {
440 // Call the image class
441 $this->getImageInstance()->setString($imageString);
445 * Setter for X coordinate
447 * @param $x X coordinate
450 private function setImagePropertyX ($x) {
451 // Call the image class
452 $this->getImageInstance()->setX($x);
456 * Setter for Y coordinate
458 * @param $y Y coordinate
461 private function setImagePropertyY ($y) {
462 // Call the image class
463 $this->getImageInstance()->setY($y);
467 * Getter for image cache file (FQFN)
469 * @return $fqfn Full-qualified file name of the image cache
471 public function getImageCacheFqfn () {
472 // Get the FQFN ready
473 $fqfn = sprintf('%s%s%s/%s.%s',
474 $this->getConfigInstance()->getConfigEntry('base_path'),
475 $this->getGenericBasePath(),
478 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
480 $this->getImageInstance()->getImageType()
488 * Outputs the image to the world
490 * @param $responseInstance An instance of a Responseable class
493 public function transferToResponse (Responseable $responseInstance) {
494 // Set the image instance
495 $responseInstance->setImageInstance($this->getImageInstance());
499 * Load a specified image template into the engine
501 * @param $template The image template we shall load which is
502 * located in 'image' by default
505 public function loadImageTemplate ($template) {
507 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
509 // Load the special template
510 $this->loadTemplate($template);