3 namespace Org\Mxchange\CoreFramework\Template\Engine;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
9 use Org\Mxchange\CoreFramework\Parser\Xml\XmlParser;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Response\Responseable;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
13 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
17 use \UnexpectedValueException;
20 * The own template engine for loading caching and sending out images
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
43 * Main nodes in the XML tree ('image' is ignored)
45 private $mainNodes = array(
55 * Sub nodes in the XML tree
57 private $subNodes = array(
74 private $currMainNode = '';
77 * Protected constructor
81 protected function __construct () {
82 // Call parent constructor
83 parent::__construct(__CLASS__);
87 * Creates an instance of the class TemplateEngine and prepares it for usage
89 * @return $templateInstance An instance of TemplateEngine
90 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
91 * @throws InvalidDirectoryException If $templateBasePath is no
92 * directory or not found
93 * @throws BasePathReadProtectedException If $templateBasePath is
96 public static final function createImageTemplateEngine () {
98 $templateInstance = new ImageTemplateEngine();
100 // Get the application instance from registry
101 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
103 // Determine base path
104 $templateBasePath = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
106 // Is the base path valid?
107 if (empty($templateBasePath)) {
108 // Base path is empty
109 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
110 } elseif (!is_string($templateBasePath)) {
112 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
113 } elseif (!is_dir($templateBasePath)) {
115 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
116 } elseif (!is_readable($templateBasePath)) {
118 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
122 $templateInstance->setTemplateBasePath($templateBasePath);
124 // Set template extensions
125 $templateInstance->setRawTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('raw_template_extension'));
126 $templateInstance->setCodeTemplateExtension(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('code_template_extension'));
128 // Absolute output path for compiled templates
129 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
131 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('compile_output_path')
134 // Return the prepared instance
135 return $templateInstance;
139 * Getter for current main node
141 * @return $currMainNode Current main node
143 public final function getCurrMainNode () {
144 return $this->currMainNode;
148 * Getter for main node array
150 * @return $mainNodes Array with valid main node names
152 public final function getMainNodes () {
153 return $this->mainNodes;
157 * Getter for sub node array
159 * @return $subNodes Array with valid sub node names
161 public final function getSubNodes () {
162 return $this->subNodes;
166 * Handles the start element of an XML resource
168 * @param $resource XML parser resource (currently ignored)
169 * @param $element The element we shall handle
170 * @param $attributes All attributes
172 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
174 public function startElement ($resource, $element, array $attributes) {
175 // Initial method name which will never be called...
176 $methodName = 'initImage';
178 // Make the element name lower-case
179 $element = strtolower($element);
181 // Is the element a main node?
182 //* DEBUG: */ echo "START: >".$element."<<br />\n";
183 if (in_array($element, $this->mainNodes)) {
184 // Okay, main node found!
185 $methodName = 'setImage' . self::convertToClassName($element);
186 } elseif (in_array($element, $this->subNodes)) {
188 $methodName = 'setImageProperty' . self::convertToClassName($element);
189 } elseif ($element != 'image') {
190 // Invalid node name found
191 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
195 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
196 call_user_func_array(array($this, $methodName), $attributes);
200 * Ends the main or sub node by sending out the gathered data
202 * @param $resource An XML resource pointer (currently ignored)
203 * @param $nodeName Name of the node we want to finish
205 * @throws XmlNodeMismatchException If current main node mismatches the closing one
207 public function finishElement ($resource, $nodeName) {
208 // Make all lower-case
209 $nodeName = strtolower($nodeName);
211 // Does this match with current main node?
212 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
213 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
215 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
216 } elseif (in_array($nodeName, $this->getSubNodes())) {
217 // Silently ignore sub nodes
221 // Construct method name
222 $methodName = 'finish' . self::convertToClassName($nodeName);
224 // Call the corresponding method
225 call_user_func_array(array($this->getImageInstance(), $methodName), array());
231 * @param $resource XML parser resource (currently ignored)
232 * @param $characters Characters to handle
234 * @todo Find something usefull with this!
236 public function characterHandler ($resource, $characters) {
237 // Trim all spaces away
238 $characters = trim($characters);
240 // Is this string empty?
241 if (empty($characters)) {
242 // Then skip it silently
247 $this->partialStub('Handling extra characters is not yet supported!');
251 * Intializes the image
254 * @todo Add cache creation here
256 private function initImage () {
263 * @param $imageType Code fragment or direct value holding the image type
266 private function setImageType ($imageType) {
267 // Set group to general
268 $this->setVariableGroup('general');
270 // Try to compile it first to get the value from variable stack
271 $imageType = $this->compileRawCode($imageType);
273 // Now make a class name of it
274 $className = self::convertToClassName($imageType.'_image');
276 // And try to initiate it
277 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
279 // Set current main node to type
280 $this->currMainNode = 'type';
284 * "Setter" for resolution, we first need to collect the resolution from the
285 * sub-nodes. So first, this method will prepare an array for it
289 private function setImageResolution () {
290 // Call the image class
291 $this->getImageInstance()->initResolution();
293 // Current main node is resolution
294 $this->currMainNode = 'resolution';
298 * "Setter" for base information. For more details see above method!
301 * @see ImageTemplateEngine::setImageResolution
303 private function setImageBase () {
304 // Call the image class
305 $this->getImageInstance()->initBase();
307 // Current main node is resolution
308 $this->currMainNode = 'base';
312 * "Setter" for background-color. For more details see above method!
315 * @see ImageTemplateEngine::setImageResolution
317 private function setImageBackgroundColor () {
318 // Call the image class
319 $this->getImageInstance()->initBackgroundColor();
321 // Current main node is background-color
322 $this->currMainNode = 'background-color';
326 * "Setter" for foreground-color. For more details see above method!
329 * @see ImageTemplateEngine::setImageResolution
331 private function setImageForegroundColor () {
332 // Call the image class
333 $this->getImageInstance()->initForegroundColor();
335 // Current main node is foreground-color
336 $this->currMainNode = 'foreground-color';
340 * "Setter" for image-string. For more details see above method!
342 * @param $groupable Whether this image string is groupable
344 * @see ImageTemplateEngine::setImageResolution
346 private function setImageImageString ($groupable = 'single') {
347 // Call the image class
348 $this->getImageInstance()->initImageString($groupable);
350 // Current main node is foreground-color
351 $this->currMainNode = 'image-string';
355 * Setter for image name
357 * @param $imageName Name of the image
360 private function setImagePropertyName ($imageName) {
361 // Call the image class
362 $this->getImageInstance()->setImageName($imageName);
366 * Setter for image width
368 * @param $width Width of the image or variable
371 private function setImagePropertyWidth ($width) {
372 // Call the image class
373 $this->getImageInstance()->setWidth($width);
377 * Setter for image height
379 * @param $height Height of the image or variable
382 private function setImagePropertyHeight ($height) {
383 // Call the image class
384 $this->getImageInstance()->setHeight($height);
388 * Setter for image red color
390 * @param $red Red color value
393 private function setImagePropertyRed ($red) {
394 // Call the image class
395 $this->getImageInstance()->setRed($red);
399 * Setter for image green color
401 * @param $green Green color value
404 private function setImagePropertyGreen ($green) {
405 // Call the image class
406 $this->getImageInstance()->setGreen($green);
410 * Setter for image blue color
412 * @param $blue Blue color value
415 private function setImagePropertyBlue ($blue) {
416 // Call the image class
417 $this->getImageInstance()->setBlue($blue);
421 * Setter for string name (identifier)
423 * @param $stringName String name (identifier)
426 private function setImagePropertyStringName ($stringName) {
427 // Call the image class
428 $this->getImageInstance()->setStringName($stringName);
432 * Setter for font size
434 * @param $fontSize Size of the font
437 private function setImagePropertyFontSize ($fontSize) {
438 // Call the image class
439 $this->getImageInstance()->setFontSize($fontSize);
443 * Setter for image string
445 * @param $imageString Image string to set
448 private function setImagePropertyText ($imageString) {
449 // Call the image class
450 $this->getImageInstance()->setString($imageString);
454 * Setter for X coordinate
456 * @param $x X coordinate
459 private function setImagePropertyX ($x) {
460 // Call the image class
461 $this->getImageInstance()->setX($x);
465 * Setter for Y coordinate
467 * @param $y Y coordinate
470 private function setImagePropertyY ($y) {
471 // Call the image class
472 $this->getImageInstance()->setY($y);
476 * Getter for image cache file instance
478 * @return $fileInstance An instance of a SplFileInfo class
480 public function getImageCacheFile () {
481 // Get the instance ready
482 $fileInstance = new SplFileInfo(sprintf('%s%s%s/%s.%s',
483 FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('root_base_path'),
484 $this->getGenericBasePath(),
487 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
489 $this->getImageInstance()->getImageType()
493 return $fileInstance;
497 * Outputs the image to the world
499 * @param $responseInstance An instance of a Responseable class
502 public function transferToResponse (Responseable $responseInstance) {
503 // Set the image instance
504 $responseInstance->setImageInstance($this->getImageInstance());
508 * Load a specified image template into the engine
510 * @param $template The image template we shall load which is
511 * located in 'image' by default
514 public function loadImageTemplate ($template) {
516 $this->setTemplateType(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('image_template_type'));
518 // Load the special template
519 $this->loadTemplate($template);