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