3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Registry\Registry;
8 use CoreFramework\Response\Responseable;
9 use CoreFramework\Template\CompileableTemplate;
12 * The own template engine for loading caching and sending out images
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
35 * Main nodes in the XML tree ('image' is ignored)
37 private $mainNodes = array(
47 * Sub nodes in the XML tree
49 private $subNodes = array(
66 private $currMainNode = '';
69 * Protected constructor
73 protected function __construct () {
74 // Call parent constructor
75 parent::__construct(__CLASS__);
79 * Creates an instance of the class TemplateEngine and prepares it for usage
81 * @return $templateInstance An instance of TemplateEngine
82 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
83 * @throws InvalidBasePathStringException If $templateBasePath is no string
84 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
85 * directory or not found
86 * @throws BasePathReadProtectedException If $templateBasePath is
89 public static final function createImageTemplateEngine () {
91 $templateInstance = new ImageTemplateEngine();
93 // Get the application instance from registry
94 $applicationInstance = Registry::getRegistry()->getInstance('app');
96 // Determine base path
97 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
99 // Is the base path valid?
100 if (empty($templateBasePath)) {
101 // Base path is empty
102 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
103 } elseif (!is_string($templateBasePath)) {
105 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
106 } elseif (!is_dir($templateBasePath)) {
108 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
109 } elseif (!is_readable($templateBasePath)) {
111 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
115 $templateInstance->setTemplateBasePath($templateBasePath);
117 // Set template extensions
118 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
119 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
121 // Absolute output path for compiled templates
122 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
124 // Return the prepared instance
125 return $templateInstance;
129 * Getter for current main node
131 * @return $currMainNode Current main node
133 public final function getCurrMainNode () {
134 return $this->currMainNode;
138 * Getter for main node array
140 * @return $mainNodes Array with valid main node names
142 public final function getMainNodes () {
143 return $this->mainNodes;
147 * Getter for sub node array
149 * @return $subNodes Array with valid sub node names
151 public final function getSubNodes () {
152 return $this->subNodes;
156 * Handles the start element of an XML resource
158 * @param $resource XML parser resource (currently ignored)
159 * @param $element The element we shall handle
160 * @param $attributes All attributes
162 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
164 public function startElement ($resource, $element, array $attributes) {
165 // Initial method name which will never be called...
166 $methodName = 'initImage';
168 // Make the element name lower-case
169 $element = strtolower($element);
171 // Is the element a main node?
172 //* DEBUG: */ echo "START: >".$element."<<br />\n";
173 if (in_array($element, $this->mainNodes)) {
174 // Okay, main node found!
175 $methodName = 'setImage' . self::convertToClassName($element);
176 } elseif (in_array($element, $this->subNodes)) {
178 $methodName = 'setImageProperty' . self::convertToClassName($element);
179 } elseif ($element != 'image') {
180 // Invalid node name found
181 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
185 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
186 call_user_func_array(array($this, $methodName), $attributes);
190 * Ends the main or sub node by sending out the gathered data
192 * @param $resource An XML resource pointer (currently ignored)
193 * @param $nodeName Name of the node we want to finish
195 * @throws XmlNodeMismatchException If current main node mismatches the closing one
197 public function finishElement ($resource, $nodeName) {
198 // Make all lower-case
199 $nodeName = strtolower($nodeName);
201 // Does this match with current main node?
202 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
203 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
205 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
206 } elseif (in_array($nodeName, $this->getSubNodes())) {
207 // Silently ignore sub nodes
211 // Construct method name
212 $methodName = 'finish' . self::convertToClassName($nodeName);
214 // Call the corresponding method
215 call_user_func_array(array($this->getImageInstance(), $methodName), array());
221 * @param $resource XML parser resource (currently ignored)
222 * @param $characters Characters to handle
224 * @todo Find something usefull with this!
226 public function characterHandler ($resource, $characters) {
227 // Trim all spaces away
228 $characters = trim($characters);
230 // Is this string empty?
231 if (empty($characters)) {
232 // Then skip it silently
237 $this->partialStub('Handling extra characters is not yet supported!');
241 * Intializes the image
244 * @todo Add cache creation here
246 private function initImage () {
253 * @param $imageType Code fragment or direct value holding the image type
256 private function setImageType ($imageType) {
257 // Set group to general
258 $this->setVariableGroup('general');
260 // Try to compile it first to get the value from variable stack
261 $imageType = $this->compileRawCode($imageType);
263 // Now make a class name of it
264 $className = self::convertToClassName($imageType.'_image');
266 // And try to initiate it
267 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
269 // Set current main node to type
270 $this->currMainNode = 'type';
274 * "Setter" for resolution, we first need to collect the resolution from the
275 * sub-nodes. So first, this method will prepare an array for it
279 private function setImageResolution () {
280 // Call the image class
281 $this->getImageInstance()->initResolution();
283 // Current main node is resolution
284 $this->currMainNode = 'resolution';
288 * "Setter" for base information. For more details see above method!
291 * @see ImageTemplateEngine::setImageResolution
293 private function setImageBase () {
294 // Call the image class
295 $this->getImageInstance()->initBase();
297 // Current main node is resolution
298 $this->currMainNode = 'base';
302 * "Setter" for background-color. For more details see above method!
305 * @see ImageTemplateEngine::setImageResolution
307 private function setImageBackgroundColor () {
308 // Call the image class
309 $this->getImageInstance()->initBackgroundColor();
311 // Current main node is background-color
312 $this->currMainNode = 'background-color';
316 * "Setter" for foreground-color. For more details see above method!
319 * @see ImageTemplateEngine::setImageResolution
321 private function setImageForegroundColor () {
322 // Call the image class
323 $this->getImageInstance()->initForegroundColor();
325 // Current main node is foreground-color
326 $this->currMainNode = 'foreground-color';
330 * "Setter" for image-string. For more details see above method!
332 * @param $groupable Whether this image string is groupable
334 * @see ImageTemplateEngine::setImageResolution
336 private function setImageImageString ($groupable = 'single') {
337 // Call the image class
338 $this->getImageInstance()->initImageString($groupable);
340 // Current main node is foreground-color
341 $this->currMainNode = 'image-string';
345 * Setter for image name
347 * @param $imageName Name of the image
350 private function setImagePropertyName ($imageName) {
351 // Call the image class
352 $this->getImageInstance()->setImageName($imageName);
356 * Setter for image width
358 * @param $width Width of the image or variable
361 private function setImagePropertyWidth ($width) {
362 // Call the image class
363 $this->getImageInstance()->setWidth($width);
367 * Setter for image height
369 * @param $height Height of the image or variable
372 private function setImagePropertyHeight ($height) {
373 // Call the image class
374 $this->getImageInstance()->setHeight($height);
378 * Setter for image red color
380 * @param $red Red color value
383 private function setImagePropertyRed ($red) {
384 // Call the image class
385 $this->getImageInstance()->setRed($red);
389 * Setter for image green color
391 * @param $green Green color value
394 private function setImagePropertyGreen ($green) {
395 // Call the image class
396 $this->getImageInstance()->setGreen($green);
400 * Setter for image blue color
402 * @param $blue Blue color value
405 private function setImagePropertyBlue ($blue) {
406 // Call the image class
407 $this->getImageInstance()->setBlue($blue);
411 * Setter for string name (identifier)
413 * @param $stringName String name (identifier)
416 private function setImagePropertyStringName ($stringName) {
417 // Call the image class
418 $this->getImageInstance()->setStringName($stringName);
422 * Setter for font size
424 * @param $fontSize Size of the font
427 private function setImagePropertyFontSize ($fontSize) {
428 // Call the image class
429 $this->getImageInstance()->setFontSize($fontSize);
433 * Setter for image string
435 * @param $imageString Image string to set
438 private function setImagePropertyText ($imageString) {
439 // Call the image class
440 $this->getImageInstance()->setString($imageString);
444 * Setter for X coordinate
446 * @param $x X coordinate
449 private function setImagePropertyX ($x) {
450 // Call the image class
451 $this->getImageInstance()->setX($x);
455 * Setter for Y coordinate
457 * @param $y Y coordinate
460 private function setImagePropertyY ($y) {
461 // Call the image class
462 $this->getImageInstance()->setY($y);
466 * Getter for image cache file (FQFN)
468 * @return $fqfn Full-qualified file name of the image cache
470 public function getImageCacheFqfn () {
471 // Get the FQFN ready
472 $fqfn = sprintf('%s%s%s/%s.%s',
473 $this->getConfigInstance()->getConfigEntry('base_path'),
474 $this->getGenericBasePath(),
477 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
479 $this->getImageInstance()->getImageType()
487 * Outputs the image to the world
489 * @param $responseInstance An instance of a Responseable class
492 public function transferToResponse (Responseable $responseInstance) {
493 // Set the image instance
494 $responseInstance->setImageInstance($this->getImageInstance());
498 * Load a specified image template into the engine
500 * @param $template The image template we shall load which is
501 * located in 'image' by default
504 public function loadImageTemplate ($template) {
506 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
508 // Load the special template
509 $this->loadTemplate($template);