3 namespace Org\Mxchange\CoreFramework\Command;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
8 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
9 use Org\Mxchange\CoreFramework\Request\Requestable;
10 use Org\Mxchange\CoreFramework\Resolver\Resolver;
11 use Org\Mxchange\CoreFramework\Response\Responseable;
14 * A general (base) command
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
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 abstract class BaseCommand extends BaseFrameworkSystem {
39 private $resolverInstance = NULL;
42 * Protected constructor
44 * @param $className Name of the class
47 protected function __construct (string $className) {
48 // Call parent constructor
49 parent::__construct($className);
53 * Setter for resolver instance
55 * @param $resolverInstance Instance of a command resolver class
58 protected final function setResolverInstance (Resolver $resolverInstance) {
59 $this->resolverInstance = $resolverInstance;
63 * Getter for resolver instance
65 * @return $resolverInstance Instance of a command resolver class
67 protected final function getResolverInstance () {
68 return $this->resolverInstance;
72 * Sends a generic HTTP response with header, menu, content and footer
74 * @param $requestInstance An instance of a class with an Requestable interface
75 * @param $responseInstance An instance of a class with an Responseable interface
76 * @param $suffix Optional template suffix, e.g. '_form' for forms
79 protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, $suffix = '') {
80 // This command doesn't handle any POST requests, so only handle get request
81 assert(!$requestInstance->isPostRequestMethod());
83 // Get the application instance
84 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
86 // Prepare a template instance
87 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
89 // Transfer application data
90 $templateInstance->assignApplicationData();
93 $templateInstance->assignConfigVariable('base_url');
95 // Load the master template
96 $masterTemplate = $applicationInstance->buildMasterTemplateName();
98 // Load header template
99 $templateInstance->loadCodeTemplate('header');
101 // Compile and assign it with a variable
102 $templateInstance->compileTemplate();
103 $templateInstance->assignTemplateWithVariable('header', 'header');
105 // Load footer template
106 $templateInstance->loadCodeTemplate('footer');
108 // Compile and assign it with a variable
109 $templateInstance->compileTemplate();
110 $templateInstance->assignTemplateWithVariable('footer', 'footer');
112 // Load the content template
113 $templateInstance->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
115 // Assign the content template with the master template as a content ... ;)
116 $templateInstance->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
118 // Load the master template
119 $templateInstance->loadCodeTemplate($masterTemplate);
122 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
124 // Construct the menu in every command. We could do this in BaseCommand class. But this means
125 // *every* command has a navigation system and that is want we don't want.
126 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
129 $menuInstance->renderMenu();
131 // Transfer it to the template engine instance
132 $menuInstance->transferContentToTemplateEngine($templateInstance);
135 * ... and all variables. This should be merged together in a pattern
136 * to make things easier. A cache mechanism should be added between
137 * these two calls to cache compiled templates.
139 $templateInstance->compileVariables();
141 // Get the content back from the template engine and put it in response class
142 $templateInstance->transferToResponse($responseInstance);