]> 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\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\Resolver\Resolver;
12 use Org\Mxchange\CoreFramework\Response\Responseable;
13 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
14 /**
15  * A general (base) command
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
19  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
20  * @license             GNU GPL 3.0 or any newer version
21  * @link                http://www.shipsimu.org
22  *
23  * This program is free software: you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation, either version 3 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program. If not, see <http://www.gnu.org/licenses/>.
35  */
36 abstract class BaseCommand extends BaseFrameworkSystem {
37         /**
38          * Resolver instance
39          */
40         private $resolverInstance = NULL;
41
42         /**
43          * Template engine instance
44          */
45         private $templateInstance = NULL;
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          * Setter for resolver instance
60          *
61          * @param       $resolverInstance       Instance of a command resolver class
62          * @return      void
63          */
64         protected final function setResolverInstance (Resolver $resolverInstance) {
65                 $this->resolverInstance = $resolverInstance;
66         }
67
68         /**
69          * Getter for resolver instance
70          *
71          * @return      $resolverInstance       Instance of a command resolver class
72          */
73         protected final function getResolverInstance () {
74                 return $this->resolverInstance;
75         }
76
77         /**
78          * Setter for template engine instances
79          *
80          * @param       $templateInstance       An instance of a CompileableTemplate class
81          * @return      void
82          */
83         protected final function setTemplateInstance (CompileableTemplate $templateInstance) {
84                 $this->templateInstance = $templateInstance;
85         }
86
87         /**
88          * Getter for template engine instances
89          *
90          * @return      $templateInstance       An instance of a CompileableTemplate class
91          */
92         public final function getTemplateInstance () {
93                 return $this->templateInstance;
94         }
95
96         /**
97          * Initializes the template engine
98          *
99          * @param       $templateType   Type of template, e.g. 'html', 'image', 'console' ...
100          * @return      void
101          */
102         public final function initTemplateEngine (string $templateType) {
103                 // Prepare a template instance
104                 $templateInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_template_class', $templateType));
105
106                 // Set it here
107                 $this->setTemplateInstance($templateInstance);
108         }
109
110         /**
111          * Sends a generic HTTP response with header, menu, content and footer
112          *
113          * @param       $requestInstance        An instance of a class with an Requestable interface
114          * @param       $responseInstance       An instance of a class with an Responseable interface
115          * @param       $suffix                         Optional template suffix, e.g. '_form' for forms
116          * @return      void
117          */
118         protected function sendGenericGetResponse (Requestable $requestInstance, Responseable $responseInstance, string $suffix = '') {
119                 // This command doesn't handle any POST requests, so only handle get request
120                 assert(!$requestInstance->isPostRequestMethod());
121
122                 // Get the application instance
123                 $applicationInstance = GenericRegistry::getRegistry()->getInstance('application');
124
125                 // Transfer application data
126                 $this->getTemplateInstance()->assignApplicationData();
127
128                 // Assign base URL
129                 $this->getTemplateInstance()->assignConfigVariable('base_url');
130
131                 // Load the master template
132                 $masterTemplate = $applicationInstance->buildMasterTemplateName();
133
134                 // Load header template
135                 $this->getTemplateInstance()->loadCodeTemplate('header');
136
137                 // Compile and assign it with a variable
138                 $this->getTemplateInstance()->compileTemplate();
139                 $this->getTemplateInstance()->assignTemplateWithVariable('header', 'header');
140
141                 // Load footer template
142                 $this->getTemplateInstance()->loadCodeTemplate('footer');
143
144                 // Compile and assign it with a variable
145                 $this->getTemplateInstance()->compileTemplate();
146                 $this->getTemplateInstance()->assignTemplateWithVariable('footer', 'footer');
147
148                 // Load the content template
149                 $this->getTemplateInstance()->loadCodeTemplate($this->getResolverInstance()->getCommandName() . $suffix);
150
151                 // Assign the content template with the master template as a content ... ;)
152                 $this->getTemplateInstance()->assignTemplateWithVariable($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName(), 'main_content');
153
154                 // Load the master template
155                 $this->getTemplateInstance()->loadCodeTemplate($masterTemplate);
156
157                 // Set title
158                 $this->getTemplateInstance()->assignVariable('title', FrameworkBootstrap::getLanguageInstance()->getMessage('page_' . $applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_title'));
159
160                 // Construct the menu in every command. We could do this in BaseCommand class. But this means
161                 // *every* command has a navigation system and that is want we don't want.
162                 $menuInstance = ObjectFactory::createObjectByConfiguredName($applicationInstance->getAppShortName() . '_' . $this->getResolverInstance()->getCommandName() . '_menu_class', array($applicationInstance));
163
164                 // Render the menu
165                 $menuInstance->renderMenu();
166
167                 // Transfer it to the template engine instance
168                 $menuInstance->transferContentToTemplateEngine($this->getTemplateInstance());
169
170                 /*
171                  * ... and all variables. This should be merged together in a pattern
172                  * to make things easier. A cache mechanism should be added between
173                  * these two calls to cache compiled templates.
174                  */
175                 $this->getTemplateInstance()->compileVariables();
176
177                 // Get the content back from the template engine and put it in response class
178                 $this->getTemplateInstance()->transferToResponse($responseInstance);
179         }
180
181 }