]> 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\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 \BadMethodCallException;
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 - 2023 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          * @throws      InvalidArgumentException        If a parameter has an invalid value
64          */
65         public final function initTemplateEngine (string $templateType) {
66                 // Check paramter
67                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-COMMAND: templateType=%s - CALLED!', $templateType));
68                 if (empty($templateType)) {
69                         // Throw IAE
70                         throw new InvalidArgumentException('Parameter "templateType" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
71                 }
72
73                 // Prepare a template instance
74                 $templateInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_template_class', $templateType));
75
76                 // Set it here
77                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-COMMAND: templateInstance=%s', $templateInstance->__toString()));
78                 $this->setTemplateInstance($templateInstance);
79
80                 // Trace message
81                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: EXIT!');
82         }
83
84         /**
85          * Sends a generic HTTP response with header, menu, content and footer
86          *
87          * @param       $requestInstance        An instance of a class with an Requestable interface
88          * @param       $responseInstance       An instance of a class with an Responseable interface
89          * @param       $suffix                         Optional template suffix, e.g. '_form' for forms
90          * @return      void
91          * @throws      BadMethodCallException  If a POST request ended here
92          */
93         protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, string $suffix = '') {
94                 // Check conditions
95                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-COMMAND: requestInstance=%s,responseInstance=%s,suffix=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString(), $suffix));
96                 if ($requestInstance->isPostRequestMethod()) {
97                         // POST request isn't handled here
98                         throw new BadMethodCallException('This method does only handle GET/HEAD requests', FrameworkInterface::EXCEPTION_BAD_METHOD_CALL);
99                 }
100
101                 // Transfer application data
102                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: Invoking this->templateInstance->assignApplicationData() ...');
103                 $this->getTemplateInstance()->assignApplicationData();
104
105                 // Assign base URL
106                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: Invoking this->templateInstance->assignConfigVariable(base_url) ...');
107                 $this->getTemplateInstance()->assignConfigVariable('base_url');
108
109                 // Get the application instance
110                 $applicationInstance = ApplicationHelper::getSelfInstance();
111
112                 // Load the master template
113                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-COMMAND: applicationInstance=%s', $applicationInstance->__toString()));
114                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
115
116                 // Load header template
117                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('BASE-COMMAND: masterTemplate=%s', $masterTemplate));
118                 $this->getTemplateInstance()->loadCodeTemplate('header');
119
120                 // Compile and assign it with a variable
121                 $this->getTemplateInstance()->compileTemplate();
122                 $this->getTemplateInstance()->assignTemplateWithVariable('header', 'header');
123
124                 // Load footer template
125                 $this->getTemplateInstance()->loadCodeTemplate('footer');
126
127                 // Compile and assign it with a variable
128                 $this->getTemplateInstance()->compileTemplate();
129                 $this->getTemplateInstance()->assignTemplateWithVariable('footer', 'footer');
130
131                 // Load the content template
132                 $this->getTemplateInstance()->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
133
134                 // Assign the content template with the master template as a content ... ;)
135                 $this->getTemplateInstance()->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
136
137                 // Load the master template
138                 $this->getTemplateInstance()->loadCodeTemplate($masterTemplate);
139
140                 // Set title
141                 $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
142
143                 /*
144                  * Construct the menu in every command. We could do this in BaseCommand
145                  * class. But this means *every* command has a navigation system and
146                  * that is want we don't want.
147                  */
148                 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class');
149
150                 // Render the menu
151                 $menuInstance->renderMenu();
152
153                 // Transfer it to the template engine instance
154                 $menuInstance->transferContentToTemplateEngine($this->getTemplateInstance());
155
156                 /*
157                  * ... and all variables. This should be merged together in a pattern
158                  * to make things easier. A cache mechanism should be added between
159                  * these two calls to cache compiled templates.
160                  */
161                 $this->getTemplateInstance()->compileVariables();
162
163                 // Get the content back from the template engine and put it in response class
164                 $this->getTemplateInstance()->transferToResponse($responseInstance);
165
166                 // Trace message
167                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-COMMAND: EXIT!');
168         }
169
170 }