3 namespace Org\Mxchange\CoreFramework\Command;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
9 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\Request\Requestable;
12 use Org\Mxchange\CoreFramework\Response\Responseable;
13 use Org\Mxchange\CoreFramework\Traits\Resolver\ResolverTrait;
14 use Org\Mxchange\CoreFramework\Traits\Template\CompileableTemplateTrait;
17 use \InvalidArgumentException;
20 * A general (base) command
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 abstract class BaseCommand extends BaseFrameworkSystem {
43 use CompileableTemplateTrait;
47 * Protected constructor
49 * @param $className Name of the class
52 protected function __construct (string $className) {
53 // Call parent constructor
54 parent::__construct($className);
58 * Initializes the template engine
60 * @param $templateType Type of template, e.g. 'html', 'image', 'console' ...
63 public final function initTemplateEngine (string $templateType) {
65 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-COMMAND: templateType=%s - CALLED!', $templateType));
66 if (empty($templateType)) {
68 throw new InvalidArgumentException('Parameter "templateType" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
71 // Prepare a template instance
72 $templateInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_template_class', $templateType));
75 $this->setTemplateInstance($templateInstance);
78 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-COMMAND: EXIT!');
82 * Sends a generic HTTP response with header, menu, content and footer
84 * @param $requestInstance An instance of a class with an Requestable interface
85 * @param $responseInstance An instance of a class with an Responseable interface
86 * @param $suffix Optional template suffix, e.g. '_form' for forms
89 protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, string $suffix = '') {
90 // This command doesn't handle any POST requests, so only handle get request
91 assert(!$requestInstance->isPostRequestMethod());
93 // Get the application instance
94 $applicationInstance = ApplicationHelper::getSelfInstance();
96 // Transfer application data
97 $this->getTemplateInstance()->assignApplicationData();
100 $this->getTemplateInstance()->assignConfigVariable('base_url');
102 // Load the master template
103 $masterTemplate = $applicationInstance->buildMasterTemplateName();
105 // Load header template
106 $this->getTemplateInstance()->loadCodeTemplate('header');
108 // Compile and assign it with a variable
109 $this->getTemplateInstance()->compileTemplate();
110 $this->getTemplateInstance()->assignTemplateWithVariable('header', 'header');
112 // Load footer template
113 $this->getTemplateInstance()->loadCodeTemplate('footer');
115 // Compile and assign it with a variable
116 $this->getTemplateInstance()->compileTemplate();
117 $this->getTemplateInstance()->assignTemplateWithVariable('footer', 'footer');
119 // Load the content template
120 $this->getTemplateInstance()->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
122 // Assign the content template with the master template as a content ... ;)
123 $this->getTemplateInstance()->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
125 // Load the master template
126 $this->getTemplateInstance()->loadCodeTemplate($masterTemplate);
129 $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
131 // Construct the menu in every command. We could do this in BaseCommand class. But this means
132 // *every* command has a navigation system and that is want we don't want.
133 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class');
136 $menuInstance->renderMenu();
138 // Transfer it to the template engine instance
139 $menuInstance->transferContentToTemplateEngine($this->getTemplateInstance());
142 * ... and all variables. This should be merged together in a pattern
143 * to make things easier. A cache mechanism should be added between
144 * these two calls to cache compiled templates.
146 $this->getTemplateInstance()->compileVariables();
148 // Get the content back from the template engine and put it in response class
149 $this->getTemplateInstance()->transferToResponse($responseInstance);