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