]> git.mxchange.org Git - core.git/blob - framework/main/classes/resolver/command/class_BaseCommandResolver.php
ed9fbdf6435f4a56bd224e844f4e59503ef4b3d9
[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\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 - 2020 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         protected 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 ($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                 } // END - if
104
105                 // Initiate the command
106                 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($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 = '';
124                 $commandInstance = NULL;
125
126                 // This goes fine so let's resolve the command
127                 $commandName = $requestInstance->getRequestElement('command');
128
129                 // Is the command empty? Then fall back to default command
130                 if (empty($commandName)) {
131                         $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
132                 } // END - if
133
134                 // Check if command is valid
135                 if ($this->isCommandValid($this->getNamespace(), $commandName) === false) {
136                         // This command is invalid!
137                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
138                 } // END - if
139
140                 // Get the command
141                 $commandInstance = $this->loadCommand($commandName);
142
143                 // And validate it
144                 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
145                         // This command has an invalid instance!
146                         throw new UnexpectedValueException(sprintf('commandInstance for commandName=%s is not object (%s) or does not implement Commandable.', $commandName, gettype($commandInstance)), self::EXCEPTION_INVALID_COMMAND);
147                 } // END - if
148
149                 // Set last command
150                 $this->setResolvedInstance($commandInstance);
151
152                 // Return the resolved command instance
153                 return $commandInstance;
154         }
155
156         /**
157          * Resolves the command by its direct name and returns an instance of its class
158          *
159          * @param       $namespace                      Namespace to look in
160          * @param       $commandName            The direct command name we shall resolve
161          * @return      $commandInstance        An instance of the command class
162          * @throws      InvalidCommandException         Thrown if $commandName is invalid
163          */
164         public function resolveCommand ($namespace, $commandName) {
165                 // Is a action set?
166                 if (empty($namespace)) {
167                         // Then thrown an exception here
168                         throw new InvalidArgumentException('Parameter "namespace" is empty');
169                 } elseif (empty($commandName)) {
170                         // Then thrown an exception here
171                         throw new InvalidArgumentException('Parameter "commandName" is empty');
172                 }
173
174                 // Initiate the instance variable
175                 $commandInstance = NULL;
176
177                 // Is the command empty? Then fall back to default command
178                 if (empty($commandName)) {
179                         // Init default command
180                         $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
181                 } // END - if
182
183                 // Check if command is valid
184                 if ($this->isCommandValid($namespace, $commandName) === false) {
185                         // This command is invalid!
186                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
187                 } // END - if
188
189                 // Get the command
190                 $commandInstance = $this->loadCommand($commandName);
191
192                 // Return the instance
193                 return $commandInstance;
194         }
195
196         /**
197          * Checks whether the given command is valid
198          *
199          * @param       $namespace              Namespace to look in
200          * @param       $commandName    The default command we shall execute
201          * @return      $isValid                Whether the given command is valid
202          * @throws      InvalidArgumentException        Thrown if given command is not set
203          */
204         protected function isCommandValid ($namespace, $commandName) {
205                 // Is namespace and command name set?
206                 if (empty($namespace)) {
207                         // Then thrown an exception here
208                         throw new InvalidArgumentException('Parameter "namespace" is empty');
209                 } elseif (empty($commandName)) {
210                         // Then thrown an exception here
211                         throw new InvalidArgumentException('Parameter "commandName" is empty');
212                 }
213
214                 // By default nothing shall be valid
215                 $isValid = false;
216
217                 // Create the full class name
218                 $className = sprintf(
219                         '%s\%s%sCommand',
220                         $namespace,
221                         $this->getCapitalizedClassPrefix(),
222                         StringUtils::convertToClassName($commandName)
223                 );
224
225                 // Now, let us create the full name of the command class
226                 $this->setClassName($className);
227
228                 // Is this class already loaded?
229                 if (class_exists($this->getClassName())) {
230                         // This class does exist. :-)
231                         $isValid = true;
232                 } // END - if
233
234                 // Set command name
235                 $this->setCommandName($commandName);
236
237                 // Return the result
238                 return $isValid;
239         }
240
241 }