]> git.mxchange.org Git - core.git/blob - framework/main/classes/resolver/command/class_BaseCommandResolver.php
Continued:
[core.git] / framework / main / classes / resolver / command / class_BaseCommandResolver.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Resolver\Command;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Command\Commandable;
8 use Org\Mxchange\CoreFramework\Command\InvalidCommandException;
9 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
12 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
13
14 // Import SPL stuff
15 use \InvalidArgumentException;
16 use \UnexpectedValueException;
17
18 /**
19  * A generic command resolver class
20  *
21  * @author              Roland Haeder <webmaster@shipsimu.org>
22  * @version             0.0.0
23  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
24  * @license             GNU GPL 3.0 or any newer version
25  * @link                http://www.shipsimu.org
26  *
27  * This program is free software: you can redistribute it and/or modify
28  * it under the terms of the GNU General Public License as published by
29  * the Free Software Foundation, either version 3 of the License, or
30  * (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program. If not, see <http://www.gnu.org/licenses/>.
39  */
40 abstract class BaseCommandResolver extends BaseResolver {
41         /**
42          * Command name
43          */
44         private $commandName = '';
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 command name
59          *
60          * @param       $commandName    Last validated command name
61          * @return      void
62          */
63         protected final function setCommandName (string $commandName) {
64                 $this->commandName = $commandName;
65         }
66
67         /**
68          * Getter for command name
69          *
70          * @return      $commandName    Last validated command name
71          */
72         public final function getCommandName () {
73                 return $this->commandName;
74         }
75
76         /**
77          * "Loads" a given command and instances it if not yet cached
78          *
79          * @param       $commandName                            A command name we shall look for
80          * @return      $commandInstance                        A loaded command instance
81          * @throws      InvalidCommandException         Thrown if even the default
82          *                                                                              command class is missing (bad!)
83          */
84         protected function loadCommand (string $commandName) {
85                 // Init command instance
86                 $commandInstance = NULL;
87
88                 // Create class name
89                 $className = sprintf(
90                         '%s\%s%sCommand',
91                         $this->getNamespace(),
92                         $this->getCapitalizedClassPrefix(),
93                         StringUtils::convertToClassName($commandName)
94                 );
95
96                 // Create command class name
97                 $this->setClassName($className);
98
99                 // Is this class loaded?
100                 if (!class_exists($this->getClassName())) {
101                         // Class not found, so throw an exception
102                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
103                 }
104
105                 // Initiate the command
106                 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), [$this]);
107
108                 // Return the result
109                 return $commandInstance;
110         }
111
112         /**
113          * Returns an command instance for a given request class or null if
114          * it was not found
115          *
116          * @param       $requestInstance        An instance of a Requestable class
117          * @return      $commandInstance        An instance of the resolved command
118          * @throws      InvalidCommandException         Thrown if $commandName is invalid
119          * @throws      UnexpectedValueException        Thrown if $commandInstance is an invalid instance
120          */
121         public function resolveCommandByRequest (Requestable $requestInstance) {
122                 // Init variables
123                 $commandName = $requestInstance->getRequestElement('command');
124                 $commandType = FrameworkBootstrap::getRequestTypeFromSystem();
125                 $commandInstance = NULL;
126
127                 // Is the command empty? Then fall back to default command
128                 if (empty($commandName)) {
129                         // Fall back to default command
130                         $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('default_%s_command', $commandType));
131                 }
132
133                 // Check if command is valid
134                 if ($this->isCommandValid($this->getNamespace(), $commandName) === false) {
135                         // This command is invalid!
136                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
137                 }
138
139                 // Get the command
140                 $commandInstance = $this->loadCommand($commandName);
141
142                 // And validate it
143                 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
144                         // This command has an invalid instance!
145                         throw new UnexpectedValueException(sprintf('commandInstance for commandName=%s is not object (%s) or does not implement Commandable.', $commandName, gettype($commandInstance)), self::EXCEPTION_INVALID_COMMAND);
146                 }
147
148                 // Set last command
149                 $this->setResolvedInstance($commandInstance);
150
151                 // Init template engine
152                 $commandInstance->initTemplateEngine($commandType);
153
154                 // Return the resolved command instance
155                 return $commandInstance;
156         }
157
158         /**
159          * Resolves the command by its direct name and returns an instance of its class
160          *
161          * @param       $namespace                      Namespace to look in
162          * @param       $commandName            The direct command name we shall resolve
163          * @return      $commandInstance        An instance of the command class
164          * @throws      InvalidCommandException         Thrown if $commandName is invalid
165          */
166         public function resolveCommand (string $namespace, string $commandName) {
167                 // Is a action set?
168                 if (empty($namespace)) {
169                         // Then thrown an exception here
170                         throw new InvalidArgumentException('Parameter "namespace" is empty');
171                 } elseif (empty($commandName)) {
172                         // Then thrown an exception here
173                         throw new InvalidArgumentException('Parameter "commandName" is empty');
174                 }
175
176                 // Initiate the instance variable
177                 $commandInstance = NULL;
178                 $commandType = FrameworkBootstrap::getRequestTypeFromSystem();
179
180                 // Is the command empty? Then fall back to default command
181                 if (empty($commandName)) {
182                         // Init default command
183                         $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('default_%s_command', $commandType));
184                 }
185
186                 // Check if command is valid
187                 if ($this->isCommandValid($namespace, $commandName) === false) {
188                         // This command is invalid!
189                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
190                 }
191
192                 // Get the command
193                 $commandInstance = $this->loadCommand($commandName);
194
195                 // Init template engine
196                 $commandInstance->initTemplateEngine($commandType);
197
198                 // Return the instance
199                 return $commandInstance;
200         }
201
202         /**
203          * Checks whether the given command is valid
204          *
205          * @param       $namespace              Namespace to look in
206          * @param       $commandName    The default command we shall execute
207          * @return      $isValid                Whether the given command is valid
208          * @throws      InvalidArgumentException        Thrown if given command is not set
209          */
210         protected function isCommandValid (string $namespace, string $commandName) {
211                 // Is namespace and command name set?
212                 if (empty($namespace)) {
213                         // Then thrown an exception here
214                         throw new InvalidArgumentException('Parameter "namespace" is empty');
215                 } elseif (empty($commandName)) {
216                         // Then thrown an exception here
217                         throw new InvalidArgumentException('Parameter "commandName" is empty');
218                 }
219
220                 // By default nothing shall be valid
221                 $isValid = false;
222
223                 // Create the full class name
224                 $className = sprintf(
225                         '%s\%s%sCommand',
226                         $namespace,
227                         $this->getCapitalizedClassPrefix(),
228                         StringUtils::convertToClassName($commandName)
229                 );
230
231                 // Now, let us create the full name of the command class
232                 $this->setClassName($className);
233
234                 // Is this class already loaded?
235                 if (class_exists($this->getClassName())) {
236                         // This class does exist. :-)
237                         $isValid = true;
238                 }
239
240                 // Set command name
241                 $this->setCommandName($commandName);
242
243                 // Return the result
244                 return $isValid;
245         }
246
247 }