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