3 namespace Org\Mxchange\CoreFramework\Template\Engine;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
9 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
10 use Org\Mxchange\CoreFramework\Image\BaseImage;
11 use Org\Mxchange\CoreFramework\Parser\Parseable;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Response\Responseable;
14 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
15 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
16 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
20 use \UnexpectedValueException;
23 * The own template engine for loading caching and sending out images
25 * @author Roland Haeder <webmaster@shipsimu.org>
27 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
28 * @license GNU GPL 3.0 or any newer version
29 * @link http://www.shipsimu.org
31 * This program is free software: you can redistribute it and/or modify
32 * it under the terms of the GNU General Public License as published by
33 * the Free Software Foundation, either version 3 of the License, or
34 * (at your option) any later version.
36 * This program is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 * GNU General Public License for more details.
41 * You should have received a copy of the GNU General Public License
42 * along with this program. If not, see <http://www.gnu.org/licenses/>.
44 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
46 * Main nodes in the XML tree ('image' is ignored)
48 private $mainNodes = array(
58 * Sub nodes in the XML tree
60 private $subNodes = array(
77 private $currMainNode = '';
80 * Instance of the image
82 private $imageInstance = NULL;
85 * Protected constructor
89 private function __construct () {
90 // Call parent constructor
91 parent::__construct(__CLASS__);
95 * Creates an instance of the class TemplateEngine and prepares it for usage
97 * @return $templateInstance An instance of TemplateEngine
98 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
99 * @throws InvalidDirectoryException If $templateBasePath is no
100 * directory or not found
101 * @throws BasePathReadProtectedException If $templateBasePath is
104 public static final function createImageTemplateEngine () {
105 // Get a new instance
106 $templateInstance = new ImageTemplateEngine();
108 // Get the application instance from registry
109 $applicationInstance = ApplicationHelper::getSelfInstance();
111 // Determine base path
112 $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
114 // Is the base path valid?
115 if (empty($templateBasePath)) {
116 // Base path is empty
117 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
118 } elseif (!is_dir($templateBasePath)) {
120 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
121 } elseif (!is_readable($templateBasePath)) {
123 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
127 $templateInstance->setTemplateBasePath($templateBasePath);
129 // Set template extensions
130 $templateInstance->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
131 $templateInstance->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('code_template_extension'));
133 // Absolute output path for compiled templates
134 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
136 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
139 // Return the prepared instance
140 return $templateInstance;
144 * Getter for current main node
146 * @return $currMainNode Current main node
148 public final function getCurrMainNode () {
149 return $this->currMainNode;
153 * Getter for main node array
155 * @return $mainNodes Array with valid main node names
157 public final function getMainNodes () {
158 return $this->mainNodes;
162 * Getter for sub node array
164 * @return $subNodes Array with valid sub node names
166 public final function getSubNodes () {
167 return $this->subNodes;
171 * Setter for image instance
173 * @param $imageInstance An instance of an image
176 public final function setImageInstance (BaseImage $imageInstance) {
177 $this->imageInstance = $imageInstance;
181 * Getter for image instance
183 * @return $imageInstance An instance of an image
185 public final function getImageInstance () {
186 return $this->imageInstance;
190 * Handles the start element of an XML resource
192 * @param $resource XML parser resource (currently ignored)
193 * @param $element The element we shall handle
194 * @param $attributes All attributes
196 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
198 public function startElement ($resource, string $element, array $attributes) {
199 // Initial method name which will never be called...
200 $methodName = 'initImage';
202 // Make the element name lower-case
203 $element = strtolower($element);
205 // Is the element a main node?
206 //* DEBUG: */ echo "START: >".$element."<<br />\n";
207 if (in_array($element, $this->mainNodes)) {
208 // Okay, main node found!
209 $methodName = 'setImage' . StringUtils::convertToClassName($element);
210 } elseif (in_array($element, $this->subNodes)) {
212 $methodName = 'setImageProperty' . StringUtils::convertToClassName($element);
213 } elseif ($element != 'image') {
214 // Invalid node name found
215 throw new InvalidXmlNodeException([$this, $element, $attributes], Parseable::EXCEPTION_XML_NODE_UNKNOWN);
219 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
220 call_user_func_array(array($this, $methodName), $attributes);
224 * Ends the main or sub node by sending out the gathered data
226 * @param $resource An XML resource pointer (currently ignored)
227 * @param $nodeName Name of the node we want to finish
229 * @throws XmlNodeMismatchException If current main node mismatches the closing one
231 public function finishElement ($resource, string $nodeName) {
232 // Does this match with current main node?
233 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
234 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
236 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), Parseable::EXCEPTION_XML_NODE_MISMATCH);
237 } elseif (in_array($nodeName, $this->getSubNodes())) {
238 // Silently ignore sub nodes
242 // Construct method name
243 $methodName = sprintf('finish%s', StringUtils::convertToClassName($nodeName));
245 // Call the corresponding method
246 call_user_func_array(array($this->getImageInstance(), $methodName), []);
252 * @param $resource XML parser resource (currently ignored)
253 * @param $characters Characters to handle
255 * @todo Find something usefull with this!
257 public function characterHandler ($resource, string $characters) {
258 // Trim all spaces away
259 $characters = trim($characters);
261 // Is this string empty?
262 if (empty($characters)) {
263 // Then skip it silently
268 $this->partialStub('Handling extra characters is not yet supported!');
272 * Intializes the image
275 * @todo Add cache creation here
277 private function initImage () {
284 * @param $imageType Code fragment or direct value holding the image type
287 private function setImageType (string $imageType) {
288 // Set group to general
289 $this->setVariableGroup('general');
291 // Try to compile it first to get the value from variable stack
292 $imageType = $this->compileRawCode($imageType);
294 // Now make a class name of it
295 $className = StringUtils::convertToClassName($imageType.'_image');
297 // And try to initiate it
298 $this->setImageInstance(ObjectFactory::createObjectByName($className, [$this]));
300 // Set current main node to type
301 $this->currMainNode = 'type';
305 * "Setter" for resolution, we first need to collect the resolution from the
306 * sub-nodes. So first, this method will prepare an array for it
310 private function setImageResolution () {
311 // Call the image class
312 $this->getImageInstance()->initResolution();
314 // Current main node is resolution
315 $this->currMainNode = 'resolution';
319 * "Setter" for base information. For more details see above method!
322 * @see ImageTemplateEngine::setImageResolution
324 private function setImageBase () {
325 // Call the image class
326 $this->getImageInstance()->initBase();
328 // Current main node is resolution
329 $this->currMainNode = 'base';
333 * "Setter" for background-color. For more details see above method!
336 * @see ImageTemplateEngine::setImageResolution
338 private function setImageBackgroundColor () {
339 // Call the image class
340 $this->getImageInstance()->initBackgroundColor();
342 // Current main node is background-color
343 $this->currMainNode = 'background-color';
347 * "Setter" for foreground-color. For more details see above method!
350 * @see ImageTemplateEngine::setImageResolution
352 private function setImageForegroundColor () {
353 // Call the image class
354 $this->getImageInstance()->initForegroundColor();
356 // Current main node is foreground-color
357 $this->currMainNode = 'foreground-color';
361 * "Setter" for image-string. For more details see above method!
363 * @param $groupable Whether this image string is groupable
365 * @see ImageTemplateEngine::setImageResolution
367 private function setImageImageString (string $groupable = 'single') {
368 // Call the image class
369 $this->getImageInstance()->initImageString($groupable);
371 // Current main node is foreground-color
372 $this->currMainNode = 'image-string';
376 * Setter for image name
378 * @param $imageName Name of the image
381 private function setImagePropertyName (string $imageName) {
382 // Call the image class
383 $this->getImageInstance()->setImageName($imageName);
387 * Setter for image width
389 * @param $width Width of the image or variable
392 private function setImagePropertyWidth (int $width) {
393 // Call the image class
394 $this->getImageInstance()->setWidth($width);
398 * Setter for image height
400 * @param $height Height of the image or variable
403 private function setImagePropertyHeight (int $height) {
404 // Call the image class
405 $this->getImageInstance()->setHeight($height);
409 * Setter for image red color
411 * @param $red Red color value
414 private function setImagePropertyRed ($red) {
415 // Call the image class
416 $this->getImageInstance()->setRed($red);
420 * Setter for image green color
422 * @param $green Green color value
425 private function setImagePropertyGreen ($green) {
426 // Call the image class
427 $this->getImageInstance()->setGreen($green);
431 * Setter for image blue color
433 * @param $blue Blue color value
436 private function setImagePropertyBlue ($blue) {
437 // Call the image class
438 $this->getImageInstance()->setBlue($blue);
442 * Setter for string name (identifier)
444 * @param $stringName String name (identifier)
447 private function setImagePropertyStringName (string $stringName) {
448 // Call the image class
449 $this->getImageInstance()->setStringName($stringName);
453 * Setter for font size
455 * @param $fontSize Size of the font
458 private function setImagePropertyFontSize (int $fontSize) {
459 // Call the image class
460 $this->getImageInstance()->setFontSize($fontSize);
464 * Setter for image string
466 * @param $imageString Image string to set
469 private function setImagePropertyText (string $imageString) {
470 // Call the image class
471 $this->getImageInstance()->setString($imageString);
475 * Setter for X coordinate
477 * @param $x X coordinate
480 private function setImagePropertyX (int $x) {
481 // Call the image class
482 $this->getImageInstance()->setX($x);
486 * Setter for Y coordinate
488 * @param $y Y coordinate
491 private function setImagePropertyY (int $y) {
492 // Call the image class
493 $this->getImageInstance()->setY($y);
497 * Getter for image cache file instance
499 * @return $fileInstance An instance of a SplFileInfo class
501 public function getImageCacheFile () {
502 // Get the instance ready
503 $fileInstance = new SplFileInfo(sprintf('%s%s%s/%s.%s',
504 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('root_base_path'),
505 $this->getGenericBasePath(),
508 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
510 $this->getImageInstance()->getImageType()
514 return $fileInstance;
518 * Outputs the image to the world
520 * @param $responseInstance An instance of a Responseable class
522 * @todo Nothing to really "transfer" here?
524 public function transferToResponse (Responseable $responseInstance) {
525 // Set the image instance
526 $responseInstance->setImageInstance($this->getImageInstance());
530 * Load a specified image template into the engine
532 * @param $template The image template we shall load which is
533 * located in 'image' by default
536 public function loadImageTemplate (string $template) {
538 $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('image_template_type'));
540 // Load the special template
541 $this->loadTemplate($template);