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