3 namespace Org\Mxchange\CoreFramework\Template\Engine;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
8 use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser;
9 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
10 use Org\Mxchange\CoreFramework\Response\Responseable;
11 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
12 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
16 use \UnexpectedValueException;
19 * The own template engine for loading caching and sending out images
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
42 * Main nodes in the XML tree ('image' is ignored)
44 private $mainNodes = array(
54 * Sub nodes in the XML tree
56 private $subNodes = array(
73 private $currMainNode = '';
76 * Protected constructor
80 protected function __construct () {
81 // Call parent constructor
82 parent::__construct(__CLASS__);
86 * Creates an instance of the class TemplateEngine and prepares it for usage
88 * @return $templateInstance An instance of TemplateEngine
89 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
90 * @throws InvalidDirectoryException If $templateBasePath is no
91 * directory or not found
92 * @throws BasePathReadProtectedException If $templateBasePath is
95 public static final function createImageTemplateEngine () {
97 $templateInstance = new ImageTemplateEngine();
99 // Get the application instance from registry
100 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
102 // Determine base path
103 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
105 // Is the base path valid?
106 if (empty($templateBasePath)) {
107 // Base path is empty
108 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
109 } elseif (!is_string($templateBasePath)) {
111 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
112 } elseif (!is_dir($templateBasePath)) {
114 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
115 } elseif (!is_readable($templateBasePath)) {
117 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
121 $templateInstance->setTemplateBasePath($templateBasePath);
123 // Set template extensions
124 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
125 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
127 // Absolute output path for compiled templates
128 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
130 $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
133 // Return the prepared instance
134 return $templateInstance;
138 * Getter for current main node
140 * @return $currMainNode Current main node
142 public final function getCurrMainNode () {
143 return $this->currMainNode;
147 * Getter for main node array
149 * @return $mainNodes Array with valid main node names
151 public final function getMainNodes () {
152 return $this->mainNodes;
156 * Getter for sub node array
158 * @return $subNodes Array with valid sub node names
160 public final function getSubNodes () {
161 return $this->subNodes;
165 * Handles the start element of an XML resource
167 * @param $resource XML parser resource (currently ignored)
168 * @param $element The element we shall handle
169 * @param $attributes All attributes
171 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
173 public function startElement ($resource, $element, array $attributes) {
174 // Initial method name which will never be called...
175 $methodName = 'initImage';
177 // Make the element name lower-case
178 $element = strtolower($element);
180 // Is the element a main node?
181 //* DEBUG: */ echo "START: >".$element."<<br />\n";
182 if (in_array($element, $this->mainNodes)) {
183 // Okay, main node found!
184 $methodName = 'setImage' . self::convertToClassName($element);
185 } elseif (in_array($element, $this->subNodes)) {
187 $methodName = 'setImageProperty' . self::convertToClassName($element);
188 } elseif ($element != 'image') {
189 // Invalid node name found
190 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
194 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
195 call_user_func_array(array($this, $methodName), $attributes);
199 * Ends the main or sub node by sending out the gathered data
201 * @param $resource An XML resource pointer (currently ignored)
202 * @param $nodeName Name of the node we want to finish
204 * @throws XmlNodeMismatchException If current main node mismatches the closing one
206 public function finishElement ($resource, $nodeName) {
207 // Make all lower-case
208 $nodeName = strtolower($nodeName);
210 // Does this match with current main node?
211 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
212 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
214 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
215 } elseif (in_array($nodeName, $this->getSubNodes())) {
216 // Silently ignore sub nodes
220 // Construct method name
221 $methodName = 'finish' . self::convertToClassName($nodeName);
223 // Call the corresponding method
224 call_user_func_array(array($this->getImageInstance(), $methodName), array());
230 * @param $resource XML parser resource (currently ignored)
231 * @param $characters Characters to handle
233 * @todo Find something usefull with this!
235 public function characterHandler ($resource, $characters) {
236 // Trim all spaces away
237 $characters = trim($characters);
239 // Is this string empty?
240 if (empty($characters)) {
241 // Then skip it silently
246 $this->partialStub('Handling extra characters is not yet supported!');
250 * Intializes the image
253 * @todo Add cache creation here
255 private function initImage () {
262 * @param $imageType Code fragment or direct value holding the image type
265 private function setImageType ($imageType) {
266 // Set group to general
267 $this->setVariableGroup('general');
269 // Try to compile it first to get the value from variable stack
270 $imageType = $this->compileRawCode($imageType);
272 // Now make a class name of it
273 $className = self::convertToClassName($imageType.'_image');
275 // And try to initiate it
276 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
278 // Set current main node to type
279 $this->currMainNode = 'type';
283 * "Setter" for resolution, we first need to collect the resolution from the
284 * sub-nodes. So first, this method will prepare an array for it
288 private function setImageResolution () {
289 // Call the image class
290 $this->getImageInstance()->initResolution();
292 // Current main node is resolution
293 $this->currMainNode = 'resolution';
297 * "Setter" for base information. For more details see above method!
300 * @see ImageTemplateEngine::setImageResolution
302 private function setImageBase () {
303 // Call the image class
304 $this->getImageInstance()->initBase();
306 // Current main node is resolution
307 $this->currMainNode = 'base';
311 * "Setter" for background-color. For more details see above method!
314 * @see ImageTemplateEngine::setImageResolution
316 private function setImageBackgroundColor () {
317 // Call the image class
318 $this->getImageInstance()->initBackgroundColor();
320 // Current main node is background-color
321 $this->currMainNode = 'background-color';
325 * "Setter" for foreground-color. For more details see above method!
328 * @see ImageTemplateEngine::setImageResolution
330 private function setImageForegroundColor () {
331 // Call the image class
332 $this->getImageInstance()->initForegroundColor();
334 // Current main node is foreground-color
335 $this->currMainNode = 'foreground-color';
339 * "Setter" for image-string. For more details see above method!
341 * @param $groupable Whether this image string is groupable
343 * @see ImageTemplateEngine::setImageResolution
345 private function setImageImageString ($groupable = 'single') {
346 // Call the image class
347 $this->getImageInstance()->initImageString($groupable);
349 // Current main node is foreground-color
350 $this->currMainNode = 'image-string';
354 * Setter for image name
356 * @param $imageName Name of the image
359 private function setImagePropertyName ($imageName) {
360 // Call the image class
361 $this->getImageInstance()->setImageName($imageName);
365 * Setter for image width
367 * @param $width Width of the image or variable
370 private function setImagePropertyWidth ($width) {
371 // Call the image class
372 $this->getImageInstance()->setWidth($width);
376 * Setter for image height
378 * @param $height Height of the image or variable
381 private function setImagePropertyHeight ($height) {
382 // Call the image class
383 $this->getImageInstance()->setHeight($height);
387 * Setter for image red color
389 * @param $red Red color value
392 private function setImagePropertyRed ($red) {
393 // Call the image class
394 $this->getImageInstance()->setRed($red);
398 * Setter for image green color
400 * @param $green Green color value
403 private function setImagePropertyGreen ($green) {
404 // Call the image class
405 $this->getImageInstance()->setGreen($green);
409 * Setter for image blue color
411 * @param $blue Blue color value
414 private function setImagePropertyBlue ($blue) {
415 // Call the image class
416 $this->getImageInstance()->setBlue($blue);
420 * Setter for string name (identifier)
422 * @param $stringName String name (identifier)
425 private function setImagePropertyStringName ($stringName) {
426 // Call the image class
427 $this->getImageInstance()->setStringName($stringName);
431 * Setter for font size
433 * @param $fontSize Size of the font
436 private function setImagePropertyFontSize ($fontSize) {
437 // Call the image class
438 $this->getImageInstance()->setFontSize($fontSize);
442 * Setter for image string
444 * @param $imageString Image string to set
447 private function setImagePropertyText ($imageString) {
448 // Call the image class
449 $this->getImageInstance()->setString($imageString);
453 * Setter for X coordinate
455 * @param $x X coordinate
458 private function setImagePropertyX ($x) {
459 // Call the image class
460 $this->getImageInstance()->setX($x);
464 * Setter for Y coordinate
466 * @param $y Y coordinate
469 private function setImagePropertyY ($y) {
470 // Call the image class
471 $this->getImageInstance()->setY($y);
475 * Getter for image cache file instance
477 * @return $fileInstance An instance of a SplFileInfo class
479 public function getImageCacheFile () {
480 // Get the instance ready
481 $fileInstance = new SplFileInfo(sprintf('%s%s%s/%s.%s',
482 $this->getConfigInstance()->getConfigEntry('root_base_path'),
483 $this->getGenericBasePath(),
486 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
488 $this->getImageInstance()->getImageType()
492 return $fileInstance;
496 * Outputs the image to the world
498 * @param $responseInstance An instance of a Responseable class
501 public function transferToResponse (Responseable $responseInstance) {
502 // Set the image instance
503 $responseInstance->setImageInstance($this->getImageInstance());
507 * Load a specified image template into the engine
509 * @param $template The image template we shall load which is
510 * located in 'image' by default
513 public function loadImageTemplate ($template) {
515 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
517 // Load the special template
518 $this->loadTemplate($template);