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