3 namespace CoreFramework\Template\Engine;
5 // Import framework stuff
6 use CoreFramework\Registry\Registry;
7 use CoreFramework\Template\CompileableTemplate;
10 use \UnexpectedValueException;
13 * A Console template engine class
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
20 * @todo This template engine does not make use of setTemplateType()
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class ConsoleTemplateEngine extends BaseTemplateEngine implements CompileableTemplate {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of the class TemplateEngine and prepares it for usage
49 * @return $templateInstance An instance of TemplateEngine
50 * @throws UnexpectedValueException If the provided $templateBasePath is empty or no string
51 * @throws BasePathIsNoDirectoryException If $templateBasePath is no
52 * directory or not found
53 * @throws BasePathReadProtectedException If $templateBasePath is
56 public static final function createConsoleTemplateEngine () {
58 $templateInstance = new ConsoleTemplateEngine();
60 // Get the application instance from registry
61 $applicationInstance = Registry::getRegistry()->getInstance('app');
63 // Determine base path
64 $templateBasePath = $templateInstance->getConfigInstance()->getConfigEntry('application_base_path') . $applicationInstance->getRequestInstance()->getRequestElement('app') . '/';
66 // Is the base path valid?
67 if (empty($templateBasePath)) {
69 throw new UnexpectedValueException(sprintf('[%s:%d] Variable templateBasePath is empty.', $templateInstance->__toString(), __LINE__), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
70 } elseif (!is_string($templateBasePath)) {
72 throw new UnexpectedValueException(sprintf('[%s:%d] %s is not a string with a base path.', $templateInstance->__toString(), __LINE__, $templateBasePath), self::EXCEPTION_INVALID_STRING);
73 } elseif (!is_dir($templateBasePath)) {
75 throw new BasePathIsNoDirectoryException(array($templateInstance, $templateBasePath), self::EXCEPTION_INVALID_PATH_NAME);
76 } elseif (!is_readable($templateBasePath)) {
78 throw new BasePathReadProtectedException(array($templateInstance, $templateBasePath), self::EXCEPTION_READ_PROTECED_PATH);
82 $templateInstance->setTemplateBasePath($templateBasePath);
84 // Set template extensions
85 $templateInstance->setRawTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('raw_template_extension'));
86 $templateInstance->setCodeTemplateExtension($templateInstance->getConfigInstance()->getConfigEntry('code_template_extension'));
88 // Absolute output path for compiled templates
89 $templateInstance->setCompileOutputPath($templateInstance->getConfigInstance()->getConfigEntry('base_path') . $templateInstance->getConfigInstance()->getConfigEntry('compile_output_path'));
91 // Return the prepared instance
92 return $templateInstance;