3 namespace Org\Mxchange\CoreFramework\Command;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Resolver\Resolver;
12 use Org\Mxchange\CoreFramework\Response\Responseable;
13 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
15 * A general (base) command
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
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.
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.
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/>.
36 abstract class BaseCommand extends BaseFrameworkSystem {
40 private $resolverInstance = NULL;
43 * Template engine instance
45 private $templateInstance = NULL;
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 * Setter for resolver instance
61 * @param $resolverInstance Instance of a command resolver class
64 protected final function setResolverInstance (Resolver $resolverInstance) {
65 $this->resolverInstance = $resolverInstance;
69 * Getter for resolver instance
71 * @return $resolverInstance Instance of a command resolver class
73 protected final function getResolverInstance () {
74 return $this->resolverInstance;
78 * Setter for template engine instances
80 * @param $templateInstance An instance of a CompileableTemplate class
83 protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
84 $this->templateInstance = $templateInstance;
88 * Getter for template engine instances
90 * @return $templateInstance An instance of a CompileableTemplate class
92 public final function getTemplateInstance () {
93 return $this->templateInstance;
97 * Initializes the template engine
99 * @param $templateType Type of template, e.g. 'html', 'image', 'console' ...
102 public final function initTemplateEngine (string $templateType) {
103 // Prepare a template instance
104 $templateInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_template_class', $templateType));
107 $this->setTemplateInstance($templateInstance);
111 * Sends a generic HTTP response with header, menu, content and footer
113 * @param $requestInstance An instance of a class with an Requestable interface
114 * @param $responseInstance An instance of a class with an Responseable interface
115 * @param $suffix Optional template suffix, e.g. '_form' for forms
118 protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, string $suffix = '') {
119 // This command doesn't handle any POST requests, so only handle get request
120 assert(!$requestInstance->isPostRequestMethod());
122 // Get the application instance
123 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
125 // Transfer application data
126 $this->getTemplateInstance()->assignApplicationData();
129 $this->getTemplateInstance()->assignConfigVariable('base_url');
131 // Load the master template
132 $masterTemplate = $applicationInstance->buildMasterTemplateName();
134 // Load header template
135 $this->getTemplateInstance()->loadCodeTemplate('header');
137 // Compile and assign it with a variable
138 $this->getTemplateInstance()->compileTemplate();
139 $this->getTemplateInstance()->assignTemplateWithVariable('header', 'header');
141 // Load footer template
142 $this->getTemplateInstance()->loadCodeTemplate('footer');
144 // Compile and assign it with a variable
145 $this->getTemplateInstance()->compileTemplate();
146 $this->getTemplateInstance()->assignTemplateWithVariable('footer', 'footer');
148 // Load the content template
149 $this->getTemplateInstance()->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
151 // Assign the content template with the master template as a content ... ;)
152 $this->getTemplateInstance()->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
154 // Load the master template
155 $this->getTemplateInstance()->loadCodeTemplate($masterTemplate);
158 $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
160 // Construct the menu in every command. We could do this in BaseCommand class. But this means
161 // *every* command has a navigation system and that is want we don't want.
162 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
165 $menuInstance->renderMenu();
167 // Transfer it to the template engine instance
168 $menuInstance->transferContentToTemplateEngine($this->getTemplateInstance());
171 * ... and all variables. This should be merged together in a pattern
172 * to make things easier. A cache mechanism should be added between
173 * these two calls to cache compiled templates.
175 $this->getTemplateInstance()->compileVariables();
177 // Get the content back from the template engine and put it in response class
178 $this->getTemplateInstance()->transferToResponse($responseInstance);