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