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