9d1bb119ccd9ec0cfe22fe9b1e7b5da2ca3ae96c
[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\GenericRegistry;
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  * @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
23  *
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.
28  *
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.
33  *
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/>.
36  */
37 class HtmlTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of the class TemplateEngine and prepares it for usage
50          *
51          * @return      $templateInstance               An instance of TemplateEngine
52          * @throws      UnexpectedValueException                If the provided $templateBasePath is empty or no string
53          * @throws      InvalidDirectoryException       If $templateBasePath is no
54          *                                                                                      directory or not found
55          * @throws      BasePathReadProtectedException  If $templateBasePath is
56          *                                                                                      read-protected
57          */
58         public static final function createHtmlTemplateEngine () {
59                 // Get a new instance
60                 $templateInstance = new HtmlTemplateEngine();
61
62                 // Get the application instance from registry
63                 $applicationInstance = GenericRegistry::getRegistry()->getInstance('app');
64
65                 // Determine base path
66                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getAppShortName(). '/';
67
68                 // Is the base path valid?
69                 if (empty($templateBasePath)) {
70                         // Base path is empty
71                         throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
72                 } elseif (!is_string($templateBasePath)) {
73                         // Is not a string
74                         throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
75                 } elseif (!is_dir($templateBasePath)) {
76                         // Is not a path
77                         throw new InvalidDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
78                 } elseif (!is_readable($templateBasePath)) {
79                         // Is not readable
80                         throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
81                 }
82
83                 // Set the base path
84                 $templateInstance->setTemplateBasePath($templateBasePath);
85
86                 // Set template extensions
87                 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
88                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
89
90                 // Absolute output path for compiled templates
91                 $templateInstance->setCompileOutputPath(sprintf('%s%s/',
92                         $templateBasePath,
93                         $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path')
94                 ));
95
96                 // Return the prepared instance
97                 return $templateInstance;
98         }
99
100 }