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