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