Some updates:
[core.git] / framework / main / classes / template / html / class_HtmlTemplateEngine.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Template\Engine;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\InvalidDirectoryException;
7 use Org\Mxchange\CoreFramework\Registry\Registry;
8 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
9 use Org\Mxchange\CoreFramework\Template\Engine\BaseTemplateEngine;
10
11 // Import SPL stuff
12 use \UnexpectedValueException;
13
14 /**
15  * The own template engine for loading caching and sending out the web pages
16  * and emails.
17  *
18  * @author              Roland Haeder <webmaster@shipsimu.org>
19  * @version             0.0.0
20 <<<<<<< HEAD:framework/main/classes/template/html/class_HtmlTemplateEngine.php
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 =======
23  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
24 >>>>>>> Some updates::inc/main/classes/template/html/class_HtmlTemplateEngine.php
25  * @license             GNU GPL 3.0 or any newer version
26  * @link                http://www.shipsimu.org
27  *
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.
32  *
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.
37  *
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/>.
40  */
41 class HtmlTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
42         /**
43          * Protected constructor
44          *
45          * @return      void
46          */
47         protected function __construct () {
48                 // Call parent constructor
49                 parent::__construct(__CLASS__);
50         }
51
52         /**
53          * Creates an instance of the class TemplateEngine and prepares it for usage
54          *
55          * @return      $templateInstance               An instance of TemplateEngine
56          * @throws      UnexpectedValueException                If the provided $templateBasePath is empty or no string
57          * @throws      InvalidDirectoryException       If $templateBasePath is no
58          *                                                                                      directory or not found
59          * @throws      BasePathReadProtectedException  If $templateBasePath is
60          *                                                                                      read-protected
61          */
62         public static final function createHtmlTemplateEngine () {
63                 // Get a new instance
64                 $templateInstance = new HtmlTemplateEngine();
65
66                 // Get the application instance from registry
67                 $applicationInstance = Registry::getRegistry()->getInstance('app');
68
69                 // Determine base path
70                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
71
72                 // Is the base path valid?
73                 if (empty($templateBasePath)) {
74                         // Base path is empty
75                         throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
76                 } elseif (!is_string($templateBasePath)) {
77                         // Is not a string
78                         throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
79                 } elseif (!is_dir($templateBasePath)) {
80                         // Is not a path
81                         throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
82                 } elseif (!is_readable($templateBasePath)) {
83                         // Is not readable
84                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
85                 }
86
87                 // Set the base path
88                 $templateInstance->setTemplateBasePath($templateBasePath);
89
90                 // Set template extensions
91                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
92                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
93
94                 // Absolute output path for compiled templates
95                 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
96                         $templateBasePath,
97                         $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
98                 ));
99
100                 // Return the prepared instance
101                 return $templateInstance;
102         }
103
104 }