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\Registry;
9 use Org\Mxchange\CoreFramework\Request\Requestable;
10 use Org\Mxchange\CoreFramework\Response\Responseable;
13 * A general (base) command
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class BaseCommand extends BaseFrameworkSystem {
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * Sends a generic HTTP response with header, menu, content and footer
49 * @param $requestInstance An instance of a class with an Requestable interface
50 * @param $responseInstance An instance of a class with an Responseable interface
51 * @param $suffix Optional template suffix, e.g. '_form' for forms
54 protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, $suffix = '') {
55 // This command doesn't handle any POST requests, so only handle get request
56 assert(!$requestInstance->isPostRequestMethod());
58 // Get the application instance
59 $applicationInstance = Registry::getRegistry()->getInstance('app');
61 // Prepare a template instance
62 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
64 // Transfer application data
65 $templateInstance->assignApplicationData($applicationInstance);
68 $templateInstance->assignConfigVariable('base_url');
70 // Load the master template
71 $masterTemplate = $applicationInstance->buildMasterTemplateName();
73 // Load header template
74 $templateInstance->loadCodeTemplate('header');
76 // Compile and assign it with a variable
77 $templateInstance->compileTemplate();
78 $templateInstance->assignTemplateWithVariable('header', 'header');
80 // Load footer template
81 $templateInstance->loadCodeTemplate('footer');
83 // Compile and assign it with a variable
84 $templateInstance->compileTemplate();
85 $templateInstance->assignTemplateWithVariable('footer', 'footer');
87 // Load the content template
88 $templateInstance->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
90 // Assign the content template with the master template as a content ... ;)
91 $templateInstance->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
93 // Load the master template
94 $templateInstance->loadCodeTemplate($masterTemplate);
97 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
99 // Construct the menu in every command. We could do this in BaseCommand class. But this means
100 // *every* command has a navigation system and that is want we don't want.
101 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
104 $menuInstance->renderMenu();
106 // Transfer it to the template engine instance
107 $menuInstance->transferContentToTemplateEngine($templateInstance);
110 * ... and all variables. This should be merged together in a pattern
111 * to make things easier. A cache mechanism should be added between
112 * these two calls to cache compiled templates.
114 $templateInstance->compileVariables();
116 // Get the content back from the template engine and put it in response class
117 $templateInstance->transferToResponse($responseInstance);