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