3 namespace CoreFramework\Command;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A general (base) command
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseCommand extends BaseFrameworkSystem {
32 * Protected constructor
34 * @param $className Name of the class
37 protected function __construct ($className) {
38 // Call parent constructor
39 parent::__construct($className);
43 * Sends a generic HTTP response with header, menu, content and footer
45 * @param $requestInstance An instance of a class with an Requestable interface
46 * @param $responseInstance An instance of a class with an Responseable interface
47 * @param $suffix Optional template suffix, e.g. '_form' for forms
50 protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, $suffix = '') {
51 // This command doesn't handle any POST requests, so only handle get request
52 assert(!$requestInstance->isPostRequestMethod());
54 // Get the application instance
55 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
57 // Prepare a template instance
58 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
60 // Transfer application data
61 $templateInstance->assignApplicationData($applicationInstance);
64 $templateInstance->assignConfigVariable('base_url');
66 // Load the master template
67 $masterTemplate = $applicationInstance->buildMasterTemplateName();
69 // Load header template
70 $templateInstance->loadCodeTemplate('header');
72 // Compile and assign it with a variable
73 $templateInstance->compileTemplate();
74 $templateInstance->assignTemplateWithVariable('header', 'header');
76 // Load footer template
77 $templateInstance->loadCodeTemplate('footer');
79 // Compile and assign it with a variable
80 $templateInstance->compileTemplate();
81 $templateInstance->assignTemplateWithVariable('footer', 'footer');
83 // Load the content template
84 $templateInstance->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
86 // Assign the content template with the master template as a content ... ;)
87 $templateInstance->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
89 // Load the master template
90 $templateInstance->loadCodeTemplate($masterTemplate);
93 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
95 // Construct the menu in every command. We could do this in BaseCommand class. But this means
96 // *every* command has a navigation system and that is want we don't want.
97 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
100 $menuInstance->renderMenu();
102 // Transfer it to the template engine instance
103 $menuInstance->transferContentToTemplateEngine($templateInstance);
106 * ... and all variables. This should be merged together in a pattern
107 * to make things easier. A cache mechanism should be added between
108 * these two calls to cache compiled templates.
110 $templateInstance->compileVariables();
112 // Get the content back from the template engine and put it in response class
113 $templateInstance->transferToResponse($responseInstance);