]> git.mxchange.org Git - core.git/blob - inc/main/classes/commands/class_BaseCommand.php
Continued:
[core.git] / inc / main / classes / commands / class_BaseCommand.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Command;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7
8 /**
9  * A general (base) command
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
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
16  *
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.
21  *
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.
26  *
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/>.
29  */
30 class BaseCommand extends BaseFrameworkSystem {
31         /**
32          * Protected constructor
33          *
34          * @param       $className      Name of the class
35          * @return      void
36          */
37         protected function __construct ($className) {
38                 // Call parent constructor
39                 parent::__construct($className);
40         }
41
42         /**
43          * Sends a generic HTTP response with header, menu, content and footer
44          *
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
48          * @return      void
49          */
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());
53
54                 // Get the application instance
55                 $applicationInstance = $this->getResolverInstance()->getApplicationInstance();
56
57                 // Prepare a template instance
58                 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
59
60                 // Transfer application data
61                 $templateInstance->assignApplicationData($applicationInstance);
62
63                 // Assign base URL
64                 $templateInstance->assignConfigVariable('base_url');
65
66                 // Load the master template
67                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
68
69                 // Load header template
70                 $templateInstance->loadCodeTemplate('header');
71
72                 // Compile and assign it with a variable
73                 $templateInstance->compileTemplate();
74                 $templateInstance->assignTemplateWithVariable('header', 'header');
75
76                 // Load footer template
77                 $templateInstance->loadCodeTemplate('footer');
78
79                 // Compile and assign it with a variable
80                 $templateInstance->compileTemplate();
81                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
82
83                 // Load the content template
84                 $templateInstance->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
85
86                 // Assign the content template with the master template as a content ... ;)
87                 $templateInstance->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
88
89                 // Load the master template
90                 $templateInstance->loadCodeTemplate($masterTemplate);
91
92                 // Set title
93                 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
94
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));
98
99                 // Render the menu
100                 $menuInstance->renderMenu();
101
102                 // Transfer it to the template engine instance
103                 $menuInstance->transferContentToTemplateEngine($templateInstance);
104
105                 /*
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.
109                  */
110                 $templateInstance->compileVariables();
111
112                 // Get the content back from the template engine and put it in response class
113                 $templateInstance->transferToResponse($responseInstance);
114         }
115
116 }