3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Parser\Xml\XmlParser;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Response\Responseable;
10 use CoreFramework\Template\CompileableTemplate;
13 use \UnexpectedValueException;
16 * The own template engine for loading caching and sending out images
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
39 * Main nodes in the XML tree ('image' is ignored)
41 private $mainNodes = array(
51 * Sub nodes in the XML tree
53 private $subNodes = array(
70 private $currMainNode = '';
73 * Protected constructor
77 protected function __construct () {
78 // Call parent constructor
79 parent::__construct(__CLASS__);
83 * Creates an instance of the class TemplateEngine and prepares it for usage
85 * @return $templateInstance An instance of TemplateEngine
86 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
87 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
88 * directory or not found
89 * @throws BasePathReadProtectedException If $templateBasePath is
92 public static final function createImageTemplateEngine () {
94 $templateInstance = new ImageTemplateEngine();
96 // Get the application instance from registry
97 $applicationInstance = Registry::getRegistry()->getInstance('app');
99 // Determine base path
100 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
102 // Is the base path valid?
103 if (empty($templateBasePath)) {
104 // Base path is empty
105 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
106 } elseif (!is_string($templateBasePath)) {
108 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
109 } elseif (!is_dir($templateBasePath)) {
111 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
112 } elseif (!is_readable($templateBasePath)) {
114 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
118 $templateInstance->setTemplateBasePath($templateBasePath);
120 // Set template extensions
121 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
122 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
124 // Absolute output path for compiled templates
125 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
127 // Return the prepared instance
128 return $templateInstance;
132 * Getter for current main node
134 * @return $currMainNode Current main node
136 public final function getCurrMainNode () {
137 return $this->currMainNode;
141 * Getter for main node array
143 * @return $mainNodes Array with valid main node names
145 public final function getMainNodes () {
146 return $this->mainNodes;
150 * Getter for sub node array
152 * @return $subNodes Array with valid sub node names
154 public final function getSubNodes () {
155 return $this->subNodes;
159 * Handles the start element of an XML resource
161 * @param $resource XML parser resource (currently ignored)
162 * @param $element The element we shall handle
163 * @param $attributes All attributes
165 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
167 public function startElement ($resource, $element, array $attributes) {
168 // Initial method name which will never be called...
169 $methodName = 'initImage';
171 // Make the element name lower-case
172 $element = strtolower($element);
174 // Is the element a main node?
175 //* DEBUG: */ echo "START: >".$element."<<br />\n";
176 if (in_array($element, $this->mainNodes)) {
177 // Okay, main node found!
178 $methodName = 'setImage' . self::convertToClassName($element);
179 } elseif (in_array($element, $this->subNodes)) {
181 $methodName = 'setImageProperty' . self::convertToClassName($element);
182 } elseif ($element != 'image') {
183 // Invalid node name found
184 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
188 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
189 call_user_func_array(array($this, $methodName), $attributes);
193 * Ends the main or sub node by sending out the gathered data
195 * @param $resource An XML resource pointer (currently ignored)
196 * @param $nodeName Name of the node we want to finish
198 * @throws XmlNodeMismatchException If current main node mismatches the closing one
200 public function finishElement ($resource, $nodeName) {
201 // Make all lower-case
202 $nodeName = strtolower($nodeName);
204 // Does this match with current main node?
205 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
206 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
208 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
209 } elseif (in_array($nodeName, $this->getSubNodes())) {
210 // Silently ignore sub nodes
214 // Construct method name
215 $methodName = 'finish' . self::convertToClassName($nodeName);
217 // Call the corresponding method
218 call_user_func_array(array($this->getImageInstance(), $methodName), array());
224 * @param $resource XML parser resource (currently ignored)
225 * @param $characters Characters to handle
227 * @todo Find something usefull with this!
229 public function characterHandler ($resource, $characters) {
230 // Trim all spaces away
231 $characters = trim($characters);
233 // Is this string empty?
234 if (empty($characters)) {
235 // Then skip it silently
240 $this->partialStub('Handling extra characters is not yet supported!');
244 * Intializes the image
247 * @todo Add cache creation here
249 private function initImage () {
256 * @param $imageType Code fragment or direct value holding the image type
259 private function setImageType ($imageType) {
260 // Set group to general
261 $this->setVariableGroup('general');
263 // Try to compile it first to get the value from variable stack
264 $imageType = $this->compileRawCode($imageType);
266 // Now make a class name of it
267 $className = self::convertToClassName($imageType.'_image');
269 // And try to initiate it
270 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
272 // Set current main node to type
273 $this->currMainNode = 'type';
277 * "Setter" for resolution, we first need to collect the resolution from the
278 * sub-nodes. So first, this method will prepare an array for it
282 private function setImageResolution () {
283 // Call the image class
284 $this->getImageInstance()->initResolution();
286 // Current main node is resolution
287 $this->currMainNode = 'resolution';
291 * "Setter" for base information. For more details see above method!
294 * @see ImageTemplateEngine::setImageResolution
296 private function setImageBase () {
297 // Call the image class
298 $this->getImageInstance()->initBase();
300 // Current main node is resolution
301 $this->currMainNode = 'base';
305 * "Setter" for background-color. For more details see above method!
308 * @see ImageTemplateEngine::setImageResolution
310 private function setImageBackgroundColor () {
311 // Call the image class
312 $this->getImageInstance()->initBackgroundColor();
314 // Current main node is background-color
315 $this->currMainNode = 'background-color';
319 * "Setter" for foreground-color. For more details see above method!
322 * @see ImageTemplateEngine::setImageResolution
324 private function setImageForegroundColor () {
325 // Call the image class
326 $this->getImageInstance()->initForegroundColor();
328 // Current main node is foreground-color
329 $this->currMainNode = 'foreground-color';
333 * "Setter" for image-string. For more details see above method!
335 * @param $groupable Whether this image string is groupable
337 * @see ImageTemplateEngine::setImageResolution
339 private function setImageImageString ($groupable = 'single') {
340 // Call the image class
341 $this->getImageInstance()->initImageString($groupable);
343 // Current main node is foreground-color
344 $this->currMainNode = 'image-string';
348 * Setter for image name
350 * @param $imageName Name of the image
353 private function setImagePropertyName ($imageName) {
354 // Call the image class
355 $this->getImageInstance()->setImageName($imageName);
359 * Setter for image width
361 * @param $width Width of the image or variable
364 private function setImagePropertyWidth ($width) {
365 // Call the image class
366 $this->getImageInstance()->setWidth($width);
370 * Setter for image height
372 * @param $height Height of the image or variable
375 private function setImagePropertyHeight ($height) {
376 // Call the image class
377 $this->getImageInstance()->setHeight($height);
381 * Setter for image red color
383 * @param $red Red color value
386 private function setImagePropertyRed ($red) {
387 // Call the image class
388 $this->getImageInstance()->setRed($red);
392 * Setter for image green color
394 * @param $green Green color value
397 private function setImagePropertyGreen ($green) {
398 // Call the image class
399 $this->getImageInstance()->setGreen($green);
403 * Setter for image blue color
405 * @param $blue Blue color value
408 private function setImagePropertyBlue ($blue) {
409 // Call the image class
410 $this->getImageInstance()->setBlue($blue);
414 * Setter for string name (identifier)
416 * @param $stringName String name (identifier)
419 private function setImagePropertyStringName ($stringName) {
420 // Call the image class
421 $this->getImageInstance()->setStringName($stringName);
425 * Setter for font size
427 * @param $fontSize Size of the font
430 private function setImagePropertyFontSize ($fontSize) {
431 // Call the image class
432 $this->getImageInstance()->setFontSize($fontSize);
436 * Setter for image string
438 * @param $imageString Image string to set
441 private function setImagePropertyText ($imageString) {
442 // Call the image class
443 $this->getImageInstance()->setString($imageString);
447 * Setter for X coordinate
449 * @param $x X coordinate
452 private function setImagePropertyX ($x) {
453 // Call the image class
454 $this->getImageInstance()->setX($x);
458 * Setter for Y coordinate
460 * @param $y Y coordinate
463 private function setImagePropertyY ($y) {
464 // Call the image class
465 $this->getImageInstance()->setY($y);
469 * Getter for image cache file (FQFN)
471 * @return $fqfn Full-qualified file name of the image cache
473 public function getImageCacheFqfn () {
474 // Get the FQFN ready
475 $fqfn = sprintf('%s%s%s/%s.%s',
476 $this->getConfigInstance()->getConfigEntry('base_path'),
477 $this->getGenericBasePath(),
480 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
482 $this->getImageInstance()->getImageType()
490 * Outputs the image to the world
492 * @param $responseInstance An instance of a Responseable class
495 public function transferToResponse (Responseable $responseInstance) {
496 // Set the image instance
497 $responseInstance->setImageInstance($this->getImageInstance());
501 * Load a specified image template into the engine
503 * @param $template The image template we shall load which is
504 * located in 'image' by default
507 public function loadImageTemplate ($template) {
509 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
511 // Load the special template
512 $this->loadTemplate($template);