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