3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Filesystem\InvalidDirectoryException;
8 use CoreFramework\Parser\Xml\XmlParser;
9 use CoreFramework\Registry\Registry;
10 use CoreFramework\Response\Responseable;
11 use CoreFramework\Template\CompileableTemplate;
14 use \UnexpectedValueException;
17 * The own template engine for loading caching and sending out images
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
40 * Main nodes in the XML tree ('image' is ignored)
42 private $mainNodes = array(
52 * Sub nodes in the XML tree
54 private $subNodes = array(
71 private $currMainNode = '';
74 * Protected constructor
78 protected function __construct () {
79 // Call parent constructor
80 parent::__construct(__CLASS__);
84 * Creates an instance of the class TemplateEngine and prepares it for usage
86 * @return $templateInstance An instance of TemplateEngine
87 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
88 * @throws InvalidDirectoryException If $templateBasePath is no
89 * directory or not found
90 * @throws BasePathReadProtectedException If $templateBasePath is
93 public static final function createImageTemplateEngine () {
95 $templateInstance = new ImageTemplateEngine();
97 // Get the application instance from registry
98 $applicationInstance = Registry::getRegistry()->getInstance('app');
100 // Determine base path
101 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
103 // Is the base path valid?
104 if (empty($templateBasePath)) {
105 // Base path is empty
106 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
107 } elseif (!is_string($templateBasePath)) {
109 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
110 } elseif (!is_dir($templateBasePath)) {
112 throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
113 } elseif (!is_readable($templateBasePath)) {
115 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
119 $templateInstance->setTemplateBasePath($templateBasePath);
121 // Set template extensions
122 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
123 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
125 // Absolute output path for compiled templates
126 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
128 // Return the prepared instance
129 return $templateInstance;
133 * Getter for current main node
135 * @return $currMainNode Current main node
137 public final function getCurrMainNode () {
138 return $this->currMainNode;
142 * Getter for main node array
144 * @return $mainNodes Array with valid main node names
146 public final function getMainNodes () {
147 return $this->mainNodes;
151 * Getter for sub node array
153 * @return $subNodes Array with valid sub node names
155 public final function getSubNodes () {
156 return $this->subNodes;
160 * Handles the start element of an XML resource
162 * @param $resource XML parser resource (currently ignored)
163 * @param $element The element we shall handle
164 * @param $attributes All attributes
166 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
168 public function startElement ($resource, $element, array $attributes) {
169 // Initial method name which will never be called...
170 $methodName = 'initImage';
172 // Make the element name lower-case
173 $element = strtolower($element);
175 // Is the element a main node?
176 //* DEBUG: */ echo "START: >".$element."<<br />\n";
177 if (in_array($element, $this->mainNodes)) {
178 // Okay, main node found!
179 $methodName = 'setImage' . self::convertToClassName($element);
180 } elseif (in_array($element, $this->subNodes)) {
182 $methodName = 'setImageProperty' . self::convertToClassName($element);
183 } elseif ($element != 'image') {
184 // Invalid node name found
185 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
189 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
190 call_user_func_array(array($this, $methodName), $attributes);
194 * Ends the main or sub node by sending out the gathered data
196 * @param $resource An XML resource pointer (currently ignored)
197 * @param $nodeName Name of the node we want to finish
199 * @throws XmlNodeMismatchException If current main node mismatches the closing one
201 public function finishElement ($resource, $nodeName) {
202 // Make all lower-case
203 $nodeName = strtolower($nodeName);
205 // Does this match with current main node?
206 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
207 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
209 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
210 } elseif (in_array($nodeName, $this->getSubNodes())) {
211 // Silently ignore sub nodes
215 // Construct method name
216 $methodName = 'finish' . self::convertToClassName($nodeName);
218 // Call the corresponding method
219 call_user_func_array(array($this->getImageInstance(), $methodName), array());
225 * @param $resource XML parser resource (currently ignored)
226 * @param $characters Characters to handle
228 * @todo Find something usefull with this!
230 public function characterHandler ($resource, $characters) {
231 // Trim all spaces away
232 $characters = trim($characters);
234 // Is this string empty?
235 if (empty($characters)) {
236 // Then skip it silently
241 $this->partialStub('Handling extra characters is not yet supported!');
245 * Intializes the image
248 * @todo Add cache creation here
250 private function initImage () {
257 * @param $imageType Code fragment or direct value holding the image type
260 private function setImageType ($imageType) {
261 // Set group to general
262 $this->setVariableGroup('general');
264 // Try to compile it first to get the value from variable stack
265 $imageType = $this->compileRawCode($imageType);
267 // Now make a class name of it
268 $className = self::convertToClassName($imageType.'_image');
270 // And try to initiate it
271 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
273 // Set current main node to type
274 $this->currMainNode = 'type';
278 * "Setter" for resolution, we first need to collect the resolution from the
279 * sub-nodes. So first, this method will prepare an array for it
283 private function setImageResolution () {
284 // Call the image class
285 $this->getImageInstance()->initResolution();
287 // Current main node is resolution
288 $this->currMainNode = 'resolution';
292 * "Setter" for base information. For more details see above method!
295 * @see ImageTemplateEngine::setImageResolution
297 private function setImageBase () {
298 // Call the image class
299 $this->getImageInstance()->initBase();
301 // Current main node is resolution
302 $this->currMainNode = 'base';
306 * "Setter" for background-color. For more details see above method!
309 * @see ImageTemplateEngine::setImageResolution
311 private function setImageBackgroundColor () {
312 // Call the image class
313 $this->getImageInstance()->initBackgroundColor();
315 // Current main node is background-color
316 $this->currMainNode = 'background-color';
320 * "Setter" for foreground-color. For more details see above method!
323 * @see ImageTemplateEngine::setImageResolution
325 private function setImageForegroundColor () {
326 // Call the image class
327 $this->getImageInstance()->initForegroundColor();
329 // Current main node is foreground-color
330 $this->currMainNode = 'foreground-color';
334 * "Setter" for image-string. For more details see above method!
336 * @param $groupable Whether this image string is groupable
338 * @see ImageTemplateEngine::setImageResolution
340 private function setImageImageString ($groupable = 'single') {
341 // Call the image class
342 $this->getImageInstance()->initImageString($groupable);
344 // Current main node is foreground-color
345 $this->currMainNode = 'image-string';
349 * Setter for image name
351 * @param $imageName Name of the image
354 private function setImagePropertyName ($imageName) {
355 // Call the image class
356 $this->getImageInstance()->setImageName($imageName);
360 * Setter for image width
362 * @param $width Width of the image or variable
365 private function setImagePropertyWidth ($width) {
366 // Call the image class
367 $this->getImageInstance()->setWidth($width);
371 * Setter for image height
373 * @param $height Height of the image or variable
376 private function setImagePropertyHeight ($height) {
377 // Call the image class
378 $this->getImageInstance()->setHeight($height);
382 * Setter for image red color
384 * @param $red Red color value
387 private function setImagePropertyRed ($red) {
388 // Call the image class
389 $this->getImageInstance()->setRed($red);
393 * Setter for image green color
395 * @param $green Green color value
398 private function setImagePropertyGreen ($green) {
399 // Call the image class
400 $this->getImageInstance()->setGreen($green);
404 * Setter for image blue color
406 * @param $blue Blue color value
409 private function setImagePropertyBlue ($blue) {
410 // Call the image class
411 $this->getImageInstance()->setBlue($blue);
415 * Setter for string name (identifier)
417 * @param $stringName String name (identifier)
420 private function setImagePropertyStringName ($stringName) {
421 // Call the image class
422 $this->getImageInstance()->setStringName($stringName);
426 * Setter for font size
428 * @param $fontSize Size of the font
431 private function setImagePropertyFontSize ($fontSize) {
432 // Call the image class
433 $this->getImageInstance()->setFontSize($fontSize);
437 * Setter for image string
439 * @param $imageString Image string to set
442 private function setImagePropertyText ($imageString) {
443 // Call the image class
444 $this->getImageInstance()->setString($imageString);
448 * Setter for X coordinate
450 * @param $x X coordinate
453 private function setImagePropertyX ($x) {
454 // Call the image class
455 $this->getImageInstance()->setX($x);
459 * Setter for Y coordinate
461 * @param $y Y coordinate
464 private function setImagePropertyY ($y) {
465 // Call the image class
466 $this->getImageInstance()->setY($y);
470 * Getter for image cache file (FQFN)
472 * @return $fqfn Full-qualified file name of the image cache
474 public function getImageCacheFqfn () {
475 // Get the FQFN ready
476 $fqfn = sprintf('%s%s%s/%s.%s',
477 $this->getConfigInstance()->getConfigEntry('base_path'),
478 $this->getGenericBasePath(),
481 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
483 $this->getImageInstance()->getImageType()
491 * Outputs the image to the world
493 * @param $responseInstance An instance of a Responseable class
496 public function transferToResponse (Responseable $responseInstance) {
497 // Set the image instance
498 $responseInstance->setImageInstance($this->getImageInstance());
502 * Load a specified image template into the engine
504 * @param $template The image template we shall load which is
505 * located in 'image' by default
508 public function loadImageTemplate ($template) {
510 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
512 // Load the special template
513 $this->loadTemplate($template);