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