]> git.mxchange.org Git - core.git/blob - framework/main/classes/commands/class_BaseCommand.php
b66949885f9e03039bef9caeb95ea14b176bb0c8
[core.git] / framework / main / classes / commands / class_BaseCommand.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Command;
4
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;
11
12 /**
13  * A general (base) command
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
17 <<<<<<< HEAD:framework/main/classes/commands/class_BaseCommand.php
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19 =======
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
21 >>>>>>> Some updates::inc/main/classes/commands/class_BaseCommand.php
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.shipsimu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program. If not, see <http://www.gnu.org/licenses/>.
37  */
38 abstract class BaseCommand extends BaseFrameworkSystem {
39         /**
40          * Protected constructor
41          *
42          * @param       $className      Name of the class
43          * @return      void
44          */
45         protected function __construct ($className) {
46                 // Call parent constructor
47                 parent::__construct($className);
48         }
49
50         /**
51          * Sends a generic HTTP response with header, menu, content and footer
52          *
53          * @param       $requestInstance        An instance of a class with an Requestable interface
54          * @param       $responseInstance       An instance of a class with an Responseable interface
55          * @param       $suffix                         Optional template suffix, e.g. '_form' for forms
56          * @return      void
57          */
58         protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, $suffix = '') {
59                 // This command doesn't handle any POST requests, so only handle get request
60                 assert(!$requestInstance->isPostRequestMethod());
61
62                 // Get the application instance
63                 $applicationInstance = Registry::getRegistry()->getInstance('app');
64
65                 // Prepare a template instance
66                 $templateInstance = $this->prepareTemplateInstance($applicationInstance);
67
68                 // Transfer application data
69                 $templateInstance->assignApplicationData($applicationInstance);
70
71                 // Assign base URL
72                 $templateInstance->assignConfigVariable('base_url');
73
74                 // Load the master template
75                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
76
77                 // Load header template
78                 $templateInstance->loadCodeTemplate('header');
79
80                 // Compile and assign it with a variable
81                 $templateInstance->compileTemplate();
82                 $templateInstance->assignTemplateWithVariable('header', 'header');
83
84                 // Load footer template
85                 $templateInstance->loadCodeTemplate('footer');
86
87                 // Compile and assign it with a variable
88                 $templateInstance->compileTemplate();
89                 $templateInstance->assignTemplateWithVariable('footer', 'footer');
90
91                 // Load the content template
92                 $templateInstance->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
93
94                 // Assign the content template with the master template as a content ... ;)
95                 $templateInstance->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
96
97                 // Load the master template
98                 $templateInstance->loadCodeTemplate($masterTemplate);
99
100                 // Set title
101                 $templateInstance->assignVariable('title', $this->getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
102
103                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
104                 // *every* command has a navigation system and that is want we don't want.
105                 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
106
107                 // Render the menu
108                 $menuInstance->renderMenu();
109
110                 // Transfer it to the template engine instance
111                 $menuInstance->transferContentToTemplateEngine($templateInstance);
112
113                 /*
114                  * ... and all variables. This should be merged together in a pattern
115                  * to make things easier. A cache mechanism should be added between
116                  * these two calls to cache compiled templates.
117                  */
118                 $templateInstance->compileVariables();
119
120                 // Get the content back from the template engine and put it in response class
121                 $templateInstance->transferToResponse($responseInstance);
122         }
123
124 }