Continued:
[core.git] / inc / main / classes / template / html / class_HtmlTemplateEngine.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Template\Engine;
4
5 // Import framework stuff
6 use CoreFramework\Registry\Generic\Registry;
7 use CoreFramework\Template\CompileableTemplate;
8
9 /**
10  * The own template engine for loading caching and sending out the web pages
11  * and emails.
12  *
13  * @author              Roland Haeder <webmaster@shipsimu.org>
14  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class HtmlTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
33         /**
34          * Protected constructor
35          *
36          * @return      void
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41         }
42
43         /**
44          * Creates an instance of the class TemplateEngine and prepares it for usage
45          *
46          * @return      $templateInstance               An instance of TemplateEngine
47          * @throws      BasePathIsEmptyException                If the provided $templateBasePath is empty
48          * @throws      InvalidBasePathStringException  If $templateBasePath is no string
49          * @throws      BasePathIsNoDirectoryException  If $templateBasePath is no
50          *                                                                                      directory or not found
51          * @throws      BasePathReadProtectedException  If $templateBasePath is
52          *                                                                                      read-protected
53          */
54         public static final function createHtmlTemplateEngine () {
55                 // Get a new instance
56                 $templateInstance = new HtmlTemplateEngine();
57
58                 // Get the application instance from registry
59                 $applicationInstance = Registry::getRegistry()->getInstance('app');
60
61                 // Determine base path
62                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
63
64                 // Is the base path valid?
65                 if (empty($templateBasePath)) {
66                         // Base path is empty
67                         throw new BasePathIsEmptyException($templateInstance, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
68                 } elseif (!is_string($templateBasePath)) {
69                         // Is not a string
70                         throw new InvalidBasePathStringException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_STRING);
71                 } elseif (!is_dir($templateBasePath)) {
72                         // Is not a path
73                         throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
74                 } elseif (!is_readable($templateBasePath)) {
75                         // Is not readable
76                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
77                 }
78
79                 // Set the base path
80                 $templateInstance->setTemplateBasePath($templateBasePath);
81
82                 // Set template extensions
83                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
84                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
85
86                 // Absolute output path for compiled templates
87                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
88
89                 // Return the prepared instance
90                 return $templateInstance;
91         }
92
93 }