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 \BadMethodCallException;
18 use \InvalidArgumentException;
21 * A general (base) command
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 abstract class BaseCommand extends BaseFrameworkSystem {
44 use CompileableTemplateTrait;
48 * Protected constructor
50 * @param $className Name of the class
53 protected function __construct (string $className) {
54 // Call parent constructor
55 parent::__construct($className);
59 * Initializes the template engine
61 * @param $templateType Type of template, e.g. 'html', 'image', 'console' ...
63 * @throws InvalidArgumentException If a parameter has an invalid value
65 public final function initTemplateEngine (string $templateType) {
67 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-COMMAND: templateType=%s - CALLED!', $templateType));
68 if (empty($templateType)) {
70 throw new InvalidArgumentException('Parameter "templateType" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
73 // Prepare a template instance
74 $templateInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_template_class', $templateType));
77 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-COMMAND: templateInstance=%s', $templateInstance->__toString()));
78 $this->setTemplateInstance($templateInstance);
81 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: EXIT!');
85 * Sends a generic HTTP response with header, menu, content and footer
87 * @param $requestInstance An instance of a class with an Requestable interface
88 * @param $responseInstance An instance of a class with an Responseable interface
89 * @param $suffix Optional template suffix, e.g. '_form' for forms
91 * @throws BadMethodCallException If a POST request ended here
93 protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, string $suffix = '') {
95 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-COMMAND: requestInstance=%s,responseInstance=%s,suffix=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString(), $suffix));
96 if ($requestInstance->isPostRequestMethod()) {
97 // POST request isn't handled here
98 throw new BadMethodCallException('This method does only handle GET/HEAD requests', FrameworkInterface::EXCEPTION_BAD_METHOD_CALL);
101 // Transfer application data
102 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: Invoking this->templateInstance->assignApplicationData() ...');
103 $this->getTemplateInstance()->assignApplicationData();
106 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: Invoking this->templateInstance->assignConfigVariable(base_url) ...');
107 $this->getTemplateInstance()->assignConfigVariable('base_url');
109 // Get the application instance
110 $applicationInstance = ApplicationHelper::getSelfInstance();
112 // Load the master template
113 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-COMMAND: applicationInstance=%s', $applicationInstance->__toString()));
114 $masterTemplate = $applicationInstance->buildMasterTemplateName();
116 // Load header template
117 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-COMMAND: masterTemplate=%s', $masterTemplate));
118 $this->getTemplateInstance()->loadCodeTemplate('header');
120 // Compile and assign it with a variable
121 $this->getTemplateInstance()->compileTemplate();
122 $this->getTemplateInstance()->assignTemplateWithVariable('header', 'header');
124 // Load footer template
125 $this->getTemplateInstance()->loadCodeTemplate('footer');
127 // Compile and assign it with a variable
128 $this->getTemplateInstance()->compileTemplate();
129 $this->getTemplateInstance()->assignTemplateWithVariable('footer', 'footer');
131 // Load the content template
132 $this->getTemplateInstance()->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
134 // Assign the content template with the master template as a content ... ;)
135 $this->getTemplateInstance()->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
137 // Load the master template
138 $this->getTemplateInstance()->loadCodeTemplate($masterTemplate);
141 $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
144 * Construct the menu in every command. We could do this in BaseCommand
145 * class. But this means *every* command has a navigation system and
146 * that is want we don't want.
148 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class');
151 $menuInstance->renderMenu();
153 // Transfer it to the template engine instance
154 $menuInstance->transferContentToTemplateEngine($this->getTemplateInstance());
157 * ... and all variables. This should be merged together in a pattern
158 * to make things easier. A cache mechanism should be added between
159 * these two calls to cache compiled templates.
161 $this->getTemplateInstance()->compileVariables();
163 // Get the content back from the template engine and put it in response class
164 $this->getTemplateInstance()->transferToResponse($responseInstance);
167 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: EXIT!');