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\Middleware\Debug\DebugMiddleware;
12 use Org\Mxchange\CoreFramework\Parser\Parseable;
13 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
14 use Org\Mxchange\CoreFramework\Response\Responseable;
15 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
16 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
17 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
21 use \UnexpectedValueException;
24 * The own template engine for loading caching and sending out images
26 * @author Roland Haeder <webmaster@shipsimu.org>
28 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
29 * @license GNU GPL 3.0 or any newer version
30 * @link http://www.shipsimu.org
32 * This program is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
42 * You should have received a copy of the GNU General Public License
43 * along with this program. If not, see <http://www.gnu.org/licenses/>.
45 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
47 * Main nodes in the XML tree ('image' is ignored)
49 private $mainNodes = array(
59 * Sub nodes in the XML tree
61 private $subNodes = array(
78 private $currMainNode = '';
81 * Instance of the image
83 private $imageInstance = NULL;
86 * Protected constructor
90 private function __construct () {
91 // Call parent constructor
92 parent::__construct(__CLASS__);
96 * Creates an instance of the class TemplateEngine and prepares it for usage
98 * @return $templateInstance An instance of TemplateEngine
99 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
100 * @throws InvalidDirectoryException If $templateBasePath is no
101 * directory or not found
102 * @throws BasePathReadProtectedException If $templateBasePath is
105 public static final function createImageTemplateEngine () {
106 // Get a new instance
107 $templateInstance = new ImageTemplateEngine();
109 // Get the application instance from registry
110 $applicationInstance = ApplicationHelper::getSelfInstance();
112 // Determine base path
113 $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
115 // Is the base path valid?
116 if (empty($templateBasePath)) {
117 // Base path is empty
118 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
119 } elseif (!is_dir($templateBasePath)) {
121 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
122 } elseif (!is_readable($templateBasePath)) {
124 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
128 $templateInstance->setTemplateBasePath($templateBasePath);
130 // Set template extensions
131 $templateInstance->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
132 $templateInstance->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('code_template_extension'));
134 // Absolute output path for compiled templates
135 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
137 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
140 // Return the prepared instance
141 return $templateInstance;
145 * Getter for current main node
147 * @return $currMainNode Current main node
149 public final function getCurrMainNode () {
150 return $this->currMainNode;
154 * Getter for main node array
156 * @return $mainNodes Array with valid main node names
158 public final function getMainNodes () {
159 return $this->mainNodes;
163 * Getter for sub node array
165 * @return $subNodes Array with valid sub node names
167 public final function getSubNodes () {
168 return $this->subNodes;
172 * Setter for image instance
174 * @param $imageInstance An instance of an image
177 public final function setImageInstance (BaseImage $imageInstance) {
178 $this->imageInstance = $imageInstance;
182 * Getter for image instance
184 * @return $imageInstance An instance of an image
186 public final function getImageInstance () {
187 return $this->imageInstance;
191 * Handles the start element of an XML resource
193 * @param $resource XML parser resource (currently ignored)
194 * @param $element The element we shall handle
195 * @param $attributes All attributes
197 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
199 public function startElement ($resource, string $element, array $attributes) {
200 // Initial method name which will never be called...
201 $methodName = 'initImage';
203 // Make the element name lower-case
204 $element = strtolower($element);
206 // Is the element a main node?
207 //* DEBUG: */ echo "START: >".$element."<<br />\n";
208 if (in_array($element, $this->mainNodes)) {
209 // Okay, main node found!
210 $methodName = 'setImage' . StringUtils::convertToClassName($element);
211 } elseif (in_array($element, $this->subNodes)) {
213 $methodName = 'setImageProperty' . StringUtils::convertToClassName($element);
214 } elseif ($element != 'image') {
215 // Invalid node name found
216 throw new InvalidXmlNodeException([$this, $element, $attributes], Parseable::EXCEPTION_XML_NODE_UNKNOWN);
220 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
221 call_user_func_array(array($this, $methodName), $attributes);
225 * Ends the main or sub node by sending out the gathered data
227 * @param $resource An XML resource pointer (currently ignored)
228 * @param $nodeName Name of the node we want to finish
230 * @throws XmlNodeMismatchException If current main node mismatches the closing one
232 public function finishElement ($resource, string $nodeName) {
233 // Does this match with current main node?
234 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
235 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
237 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), Parseable::EXCEPTION_XML_NODE_MISMATCH);
238 } elseif (in_array($nodeName, $this->getSubNodes())) {
239 // Silently ignore sub nodes
243 // Construct method name
244 $methodName = sprintf('finish%s', StringUtils::convertToClassName($nodeName));
246 // Call the corresponding method
247 call_user_func_array(array($this->getImageInstance(), $methodName), []);
253 * @param $resource XML parser resource (currently ignored)
254 * @param $characters Characters to handle
256 * @todo Find something usefull with this!
258 public function characterHandler ($resource, string $characters) {
259 // Trim all spaces away
260 $characters = trim($characters);
262 // Is this string empty?
263 if (empty($characters)) {
264 // Then skip it silently
269 DebugMiddleware::getSelfInstance()->partialStub('Handling extra characters is not yet supported!');
273 * Intializes the image
276 * @todo Add cache creation here
278 private function initImage () {
285 * @param $imageType Code fragment or direct value holding the image type
288 private function setImageType (string $imageType) {
289 // Set group to general
290 $this->setVariableGroup('general');
292 // Try to compile it first to get the value from variable stack
293 $imageType = $this->compileRawCode($imageType);
295 // Now make a class name of it
296 $className = StringUtils::convertToClassName($imageType.'_image');
298 // And try to initiate it
299 $this->setImageInstance(ObjectFactory::createObjectByName($className, [$this]));
301 // Set current main node to type
302 $this->currMainNode = 'type';
306 * "Setter" for resolution, we first need to collect the resolution from the
307 * sub-nodes. So first, this method will prepare an array for it
311 private function setImageResolution () {
312 // Call the image class
313 $this->getImageInstance()->initResolution();
315 // Current main node is resolution
316 $this->currMainNode = 'resolution';
320 * "Setter" for base information. For more details see above method!
323 * @see ImageTemplateEngine::setImageResolution
325 private function setImageBase () {
326 // Call the image class
327 $this->getImageInstance()->initBase();
329 // Current main node is resolution
330 $this->currMainNode = 'base';
334 * "Setter" for background-color. For more details see above method!
337 * @see ImageTemplateEngine::setImageResolution
339 private function setImageBackgroundColor () {
340 // Call the image class
341 $this->getImageInstance()->initBackgroundColor();
343 // Current main node is background-color
344 $this->currMainNode = 'background-color';
348 * "Setter" for foreground-color. For more details see above method!
351 * @see ImageTemplateEngine::setImageResolution
353 private function setImageForegroundColor () {
354 // Call the image class
355 $this->getImageInstance()->initForegroundColor();
357 // Current main node is foreground-color
358 $this->currMainNode = 'foreground-color';
362 * "Setter" for image-string. For more details see above method!
364 * @param $groupable Whether this image string is groupable
366 * @see ImageTemplateEngine::setImageResolution
368 private function setImageImageString (string $groupable = 'single') {
369 // Call the image class
370 $this->getImageInstance()->initImageString($groupable);
372 // Current main node is foreground-color
373 $this->currMainNode = 'image-string';
377 * Setter for image name
379 * @param $imageName Name of the image
382 private function setImagePropertyName (string $imageName) {
383 // Call the image class
384 $this->getImageInstance()->setImageName($imageName);
388 * Setter for image width
390 * @param $width Width of the image or variable
393 private function setImagePropertyWidth (int $width) {
394 // Call the image class
395 $this->getImageInstance()->setWidth($width);
399 * Setter for image height
401 * @param $height Height of the image or variable
404 private function setImagePropertyHeight (int $height) {
405 // Call the image class
406 $this->getImageInstance()->setHeight($height);
410 * Setter for image red color
412 * @param $red Red color value
415 private function setImagePropertyRed ($red) {
416 // Call the image class
417 $this->getImageInstance()->setRed($red);
421 * Setter for image green color
423 * @param $green Green color value
426 private function setImagePropertyGreen ($green) {
427 // Call the image class
428 $this->getImageInstance()->setGreen($green);
432 * Setter for image blue color
434 * @param $blue Blue color value
437 private function setImagePropertyBlue ($blue) {
438 // Call the image class
439 $this->getImageInstance()->setBlue($blue);
443 * Setter for string name (identifier)
445 * @param $stringName String name (identifier)
448 private function setImagePropertyStringName (string $stringName) {
449 // Call the image class
450 $this->getImageInstance()->setStringName($stringName);
454 * Setter for font size
456 * @param $fontSize Size of the font
459 private function setImagePropertyFontSize (int $fontSize) {
460 // Call the image class
461 $this->getImageInstance()->setFontSize($fontSize);
465 * Setter for image string
467 * @param $imageString Image string to set
470 private function setImagePropertyText (string $imageString) {
471 // Call the image class
472 $this->getImageInstance()->setString($imageString);
476 * Setter for X coordinate
478 * @param $x X coordinate
481 private function setImagePropertyX (int $x) {
482 // Call the image class
483 $this->getImageInstance()->setX($x);
487 * Setter for Y coordinate
489 * @param $y Y coordinate
492 private function setImagePropertyY (int $y) {
493 // Call the image class
494 $this->getImageInstance()->setY($y);
498 * Getter for image cache file instance
500 * @return $fileInstance An instance of a SplFileInfo class
502 public function getImageCacheFile () {
503 // Get the instance ready
504 $fileInstance = new SplFileInfo(sprintf('%s%s%s/%s.%s',
505 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('root_base_path'),
506 $this->getGenericBasePath(),
509 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
511 $this->getImageInstance()->getImageType()
515 return $fileInstance;
519 * Outputs the image to the world
521 * @param $responseInstance An instance of a Responseable class
523 * @todo Nothing to really "transfer" here?
525 public function transferToResponse (Responseable $responseInstance) {
526 // Set the image instance
527 $responseInstance->setImageInstance($this->getImageInstance());
531 * Load a specified image template into the engine
533 * @param $template The image template we shall load which is
534 * located in 'image' by default
537 public function loadImageTemplate (string $template) {
539 $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('image_template_type'));
541 // Load the special template
542 $this->loadTemplate($template);