3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Registry\Generic\Registry;
8 use CoreFramework\Template\CompileableTemplate;
11 * The own template engine for loading caching and sending out images
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class ImageTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
34 * Main nodes in the XML tree ('image' is ignored)
36 private $mainNodes = array(
46 * Sub nodes in the XML tree
48 private $subNodes = array(
65 private $currMainNode = '';
68 * Protected constructor
72 protected function __construct () {
73 // Call parent constructor
74 parent::__construct(__CLASS__);
78 * Creates an instance of the class TemplateEngine and prepares it for usage
80 * @return $templateInstance An instance of TemplateEngine
81 * @throws BasePathIsEmptyException If the provided $templateBasePath is empty
82 * @throws InvalidBasePathStringException If $templateBasePath is no string
83 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
84 * directory or not found
85 * @throws BasePathReadProtectedException If $templateBasePath is
88 public static final function createImageTemplateEngine () {
90 $templateInstance = new ImageTemplateEngine();
92 // Get the application instance from registry
93 $applicationInstance = Registry::getRegistry()->getInstance('app');
95 // Determine base path
96 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
98 // Is the base path valid?
99 if (empty($templateBasePath)) {
100 // Base path is empty
101 throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
102 } elseif (!is_string($templateBasePath)) {
104 throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
105 } elseif (!is_dir($templateBasePath)) {
107 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
108 } elseif (!is_readable($templateBasePath)) {
110 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
114 $templateInstance->setTemplateBasePath($templateBasePath);
116 // Set template extensions
117 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
118 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
120 // Absolute output path for compiled templates
121 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
123 // Return the prepared instance
124 return $templateInstance;
128 * Getter for current main node
130 * @return $currMainNode Current main node
132 public final function getCurrMainNode () {
133 return $this->currMainNode;
137 * Getter for main node array
139 * @return $mainNodes Array with valid main node names
141 public final function getMainNodes () {
142 return $this->mainNodes;
146 * Getter for sub node array
148 * @return $subNodes Array with valid sub node names
150 public final function getSubNodes () {
151 return $this->subNodes;
155 * Handles the start element of an XML resource
157 * @param $resource XML parser resource (currently ignored)
158 * @param $element The element we shall handle
159 * @param $attributes All attributes
161 * @throws InvalidXmlNodeException If an unknown/invalid XML node name was found
163 public function startElement ($resource, $element, array $attributes) {
164 // Initial method name which will never be called...
165 $methodName = 'initImage';
167 // Make the element name lower-case
168 $element = strtolower($element);
170 // Is the element a main node?
171 //* DEBUG: */ echo "START: >".$element."<<br />\n";
172 if (in_array($element, $this->mainNodes)) {
173 // Okay, main node found!
174 $methodName = 'setImage' . self::convertToClassName($element);
175 } elseif (in_array($element, $this->subNodes)) {
177 $methodName = 'setImageProperty' . self::convertToClassName($element);
178 } elseif ($element != 'image') {
179 // Invalid node name found
180 throw new InvalidXmlNodeException(array($this, $element, $attributes), XmlParser::EXCEPTION_XML_NODE_UNKNOWN);
184 //* DEBUG: */ echo "call: ".$methodName."<br />\n";
185 call_user_func_array(array($this, $methodName), $attributes);
189 * Ends the main or sub node by sending out the gathered data
191 * @param $resource An XML resource pointer (currently ignored)
192 * @param $nodeName Name of the node we want to finish
194 * @throws XmlNodeMismatchException If current main node mismatches the closing one
196 public function finishElement ($resource, $nodeName) {
197 // Make all lower-case
198 $nodeName = strtolower($nodeName);
200 // Does this match with current main node?
201 //* DEBUG: */ echo "END: >".$nodeName."<<br />\n";
202 if (($nodeName != $this->getCurrMainNode()) && (in_array($nodeName, $this->getMainNodes()))) {
204 throw new XmlNodeMismatchException (array($this, $nodeName, $this->getCurrMainNode()), XmlParser::EXCEPTION_XML_NODE_MISMATCH);
205 } elseif (in_array($nodeName, $this->getSubNodes())) {
206 // Silently ignore sub nodes
210 // Construct method name
211 $methodName = 'finish' . self::convertToClassName($nodeName);
213 // Call the corresponding method
214 call_user_func_array(array($this->getImageInstance(), $methodName), array());
220 * @param $resource XML parser resource (currently ignored)
221 * @param $characters Characters to handle
223 * @todo Find something usefull with this!
225 public function characterHandler ($resource, $characters) {
226 // Trim all spaces away
227 $characters = trim($characters);
229 // Is this string empty?
230 if (empty($characters)) {
231 // Then skip it silently
236 $this->partialStub('Handling extra characters is not yet supported!');
240 * Intializes the image
243 * @todo Add cache creation here
245 private function initImage () {
252 * @param $imageType Code fragment or direct value holding the image type
255 private function setImageType ($imageType) {
256 // Set group to general
257 $this->setVariableGroup('general');
259 // Try to compile it first to get the value from variable stack
260 $imageType = $this->compileRawCode($imageType);
262 // Now make a class name of it
263 $className = self::convertToClassName($imageType.'_image');
265 // And try to initiate it
266 $this->setImageInstance(ObjectFactory::createObjectByName($className, array($this)));
268 // Set current main node to type
269 $this->currMainNode = 'type';
273 * "Setter" for resolution, we first need to collect the resolution from the
274 * sub-nodes. So first, this method will prepare an array for it
278 private function setImageResolution () {
279 // Call the image class
280 $this->getImageInstance()->initResolution();
282 // Current main node is resolution
283 $this->currMainNode = 'resolution';
287 * "Setter" for base information. For more details see above method!
290 * @see ImageTemplateEngine::setImageResolution
292 private function setImageBase () {
293 // Call the image class
294 $this->getImageInstance()->initBase();
296 // Current main node is resolution
297 $this->currMainNode = 'base';
301 * "Setter" for background-color. For more details see above method!
304 * @see ImageTemplateEngine::setImageResolution
306 private function setImageBackgroundColor () {
307 // Call the image class
308 $this->getImageInstance()->initBackgroundColor();
310 // Current main node is background-color
311 $this->currMainNode = 'background-color';
315 * "Setter" for foreground-color. For more details see above method!
318 * @see ImageTemplateEngine::setImageResolution
320 private function setImageForegroundColor () {
321 // Call the image class
322 $this->getImageInstance()->initForegroundColor();
324 // Current main node is foreground-color
325 $this->currMainNode = 'foreground-color';
329 * "Setter" for image-string. For more details see above method!
331 * @param $groupable Whether this image string is groupable
333 * @see ImageTemplateEngine::setImageResolution
335 private function setImageImageString ($groupable = 'single') {
336 // Call the image class
337 $this->getImageInstance()->initImageString($groupable);
339 // Current main node is foreground-color
340 $this->currMainNode = 'image-string';
344 * Setter for image name
346 * @param $imageName Name of the image
349 private function setImagePropertyName ($imageName) {
350 // Call the image class
351 $this->getImageInstance()->setImageName($imageName);
355 * Setter for image width
357 * @param $width Width of the image or variable
360 private function setImagePropertyWidth ($width) {
361 // Call the image class
362 $this->getImageInstance()->setWidth($width);
366 * Setter for image height
368 * @param $height Height of the image or variable
371 private function setImagePropertyHeight ($height) {
372 // Call the image class
373 $this->getImageInstance()->setHeight($height);
377 * Setter for image red color
379 * @param $red Red color value
382 private function setImagePropertyRed ($red) {
383 // Call the image class
384 $this->getImageInstance()->setRed($red);
388 * Setter for image green color
390 * @param $green Green color value
393 private function setImagePropertyGreen ($green) {
394 // Call the image class
395 $this->getImageInstance()->setGreen($green);
399 * Setter for image blue color
401 * @param $blue Blue color value
404 private function setImagePropertyBlue ($blue) {
405 // Call the image class
406 $this->getImageInstance()->setBlue($blue);
410 * Setter for string name (identifier)
412 * @param $stringName String name (identifier)
415 private function setImagePropertyStringName ($stringName) {
416 // Call the image class
417 $this->getImageInstance()->setStringName($stringName);
421 * Setter for font size
423 * @param $fontSize Size of the font
426 private function setImagePropertyFontSize ($fontSize) {
427 // Call the image class
428 $this->getImageInstance()->setFontSize($fontSize);
432 * Setter for image string
434 * @param $imageString Image string to set
437 private function setImagePropertyText ($imageString) {
438 // Call the image class
439 $this->getImageInstance()->setString($imageString);
443 * Setter for X coordinate
445 * @param $x X coordinate
448 private function setImagePropertyX ($x) {
449 // Call the image class
450 $this->getImageInstance()->setX($x);
454 * Setter for Y coordinate
456 * @param $y Y coordinate
459 private function setImagePropertyY ($y) {
460 // Call the image class
461 $this->getImageInstance()->setY($y);
465 * Getter for image cache file (FQFN)
467 * @return $fqfn Full-qualified file name of the image cache
469 public function getImageCacheFqfn () {
470 // Get the FQFN ready
471 $fqfn = sprintf('%s%s%s/%s.%s',
472 $this->getConfigInstance()->getConfigEntry('base_path'),
473 $this->getGenericBasePath(),
476 $this->getImageInstance()->getImageName() . ':' . $this->__toString() . ':' . $this->getImageInstance()->__toString()
478 $this->getImageInstance()->getImageType()
486 * Outputs the image to the world
488 * @param $responseInstance An instance of a Responseable class
491 public function transferToResponse (Responseable $responseInstance) {
492 // Set the image instance
493 $responseInstance->setImageInstance($this->getImageInstance());
497 * Load a specified image template into the engine
499 * @param $template The image template we shall load which is
500 * located in 'image' by default
503 public function loadImageTemplate ($template) {
505 $this->setTemplateType($this->getConfigInstance()->getConfigEntry('image_template_type'));
507 // Load the special template
508 $this->loadTemplate($template);