0ecfa8625083f632e36bef21dabcf57364b7f7c2
[core.git] / framework / 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\Registry;
7 use CoreFramework\Template\CompileableTemplate;
8
9 // Import SPL stuff
10 use \UnexpectedValueException;
11
12 /**
13  * The own template engine for loading caching and sending out the web pages
14  * and emails.
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 class HtmlTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
36         /**
37          * Protected constructor
38          *
39          * @return      void
40          */
41         protected function __construct () {
42                 // Call parent constructor
43                 parent::__construct(__CLASS__);
44         }
45
46         /**
47          * Creates an instance of the class TemplateEngine and prepares it for usage
48          *
49          * @return      $templateInstance               An instance of TemplateEngine
50          * @throws      UnexpectedValueException                If the provided $templateBasePath is empty or no string
51          * @throws      InvalidDirectoryException       If $templateBasePath is no
52          *                                                                                      directory or not found
53          * @throws      BasePathReadProtectedException  If $templateBasePath is
54          *                                                                                      read-protected
55          */
56         public static final function createHtmlTemplateEngine () {
57                 // Get a new instance
58                 $templateInstance = new HtmlTemplateEngine();
59
60                 // Get the application instance from registry
61                 $applicationInstance = Registry::getRegistry()->getInstance('app');
62
63                 // Determine base path
64                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
65
66                 // Is the base path valid?
67                 if (empty($templateBasePath)) {
68                         // Base path is empty
69                         throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
70                 } elseif (!is_string($templateBasePath)) {
71                         // Is not a string
72                         throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
73                 } elseif (!is_dir($templateBasePath)) {
74                         // Is not a path
75                         throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
76                 } elseif (!is_readable($templateBasePath)) {
77                         // Is not readable
78                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
79                 }
80
81                 // Set the base path
82                 $templateInstance->setTemplateBasePath($templateBasePath);
83
84                 // Set template extensions
85                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
86                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
87
88                 // Absolute output path for compiled templates
89                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
90
91                 // Return the prepared instance
92                 return $templateInstance;
93         }
94
95 }