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