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