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