Also "import" here
[core.git] / framework / main / classes / template / console / class_ConsoleTemplateEngine.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Template\Engine;
4
5 // Import framework stuff
6 use CoreFramework\Filesystem\InvalidDirectoryException;
7 use CoreFramework\Registry\Registry;
8 use CoreFramework\Template\CompileableTemplate;
9
10 // Import SPL stuff
11 use \UnexpectedValueException;
12
13 /**
14  * A Console template engine class
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  * @todo                This template engine does not make use of setTemplateType()
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 class ConsoleTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
48          * Creates an instance of the class TemplateEngine and prepares it for usage
49          *
50          * @return      $templateInstance               An instance of TemplateEngine
51          * @throws      UnexpectedValueException                If the provided $templateBasePath is empty or no string
52          * @throws      InvalidDirectoryException       If $templateBasePath is no
53          *                                                                                      directory or not found
54          * @throws      BasePathReadProtectedException  If $templateBasePath is
55          *                                                                                      read-protected
56          */
57         public static final function createConsoleTemplateEngine () {
58                 // Get a new instance
59                 $templateInstance = new ConsoleTemplateEngine();
60
61                 // Get the application instance from registry
62                 $applicationInstance = Registry::getRegistry()->getInstance('app');
63
64                 // Determine base path
65                 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
66
67                 // Is the base path valid?
68                 if (empty($templateBasePath)) {
69                         // Base path is empty
70                         throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
71                 } elseif (!is_string($templateBasePath)) {
72                         // Is not a string
73                         throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_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($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
87                 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
88
89                 // Absolute output path for compiled templates
90                 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
91
92                 // Return the prepared instance
93                 return $templateInstance;
94         }
95
96 }