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