]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/resolver/web/class_WebCommandResolver.php
NullFilter class added, partial filter for accepting rules added
[shipsimu.git] / inc / classes / main / resolver / web / class_WebCommandResolver.php
1 <?php
2 /**
3  * A command resolver for local (non-hubbed) commands
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class WebCommandResolver extends BaseResolver implements CommandResolver {
25         /**
26          * Last successfull resolved command
27          */
28         private $lastCommandInstance = "";
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38
39                 // Set part description
40                 $this->setObjectDescription("Resolver for local web commands");
41
42                 // Create unique ID number
43                 $this->createUniqueID();
44
45                 // Set prefix to "Web"
46                 $this->setCommandPrefix("Web");
47         }
48
49         /**
50          * Creates an instance of a Web command resolver with a given default command
51          *
52          * @param       $commandName                            The default command we shall execute
53          * @param       $appInstance                            An instance of a manageable application helper class
54          * @return      $resolverInstance                       The prepared command resolver instance
55          * @throws      EmptyVariableException          Thrown if the default command is not set
56          * @throws      InvalidCommandException         Thrown if the default command is invalid
57          */
58         public final static function createWebCommandResolver ($commandName, ManageableApplication $appInstance) {
59                 // Create the new instance
60                 $resolverInstance = new WebCommandResolver();
61
62                 // Is the variable $commandName set and the command is valid?
63                 if (empty($commandName)) {
64                         // Then thrown an exception here
65                         throw new EmptyVariableException(array($resolverInstance, 'defaultCommand'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
66                 } elseif (!$resolverInstance->isCommandValid($commandName)) {
67                         // Invalid command found
68                         throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND);
69                 }
70
71                 // Set the application instance
72                 $resolverInstance->setApplicationInstance($appInstance);
73
74                 // Return the prepared instance
75                 return $resolverInstance;
76         }
77
78         /**
79          * Returns an command instance for a given request class or null if
80          * MissingArrayElementsException was thrown
81          *
82          * @param       $requestInstance        An instance of a request class
83          * @return      $commandInstance        An instance of the resolved command
84          * @throws      InvalidCommandException                         Thrown if $commandName is
85          *                                                                                              invalid
86          * @throws      InvalidCommandInstanceException         Thrown if $commandInstance
87          *                                                                                              is an invalid instance
88          */
89         public function resolvCommandByRequest (Requestable $requestInstance) {
90                 // Init variables
91                 $commandName = "";
92                 $commandInstance = null;
93
94                 // Test if the required parameter is set
95                 try {
96                         // This goes fine so let's resolv the command
97                         $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig('command_parameter'));
98
99                         // Is the command empty? Then fall back to default command
100                         if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig('default_command');
101
102                         // Check if the command is valid
103                         if (!$this->isCommandValid($commandName)) {
104                                 // This command is invalid!
105                                 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
106                         }
107
108                         // Get the command
109                         $commandInstance = $this->loadCommand($commandName);
110
111                         // And validate it
112                         if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
113                                 // This command has an invalid instance!
114                                 throw new InvalidCommandInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
115                         }
116
117                         // Set last command
118                         $this->lastCommandInstance = $commandInstance;
119                 } catch (MissingArrayElementsException $e) {
120                         // Just catch it here...
121                 }
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)) $commandName = $this->getConfigInstance()->readConfig('default_command');
140
141                 // Check if the command is valid
142                 if (!$this->isCommandValid($commandName)) {
143                         // This command is invalid!
144                         throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
145                 }
146
147                 // Get the command
148                 $commandInstance = $this->loadCommand($commandName);
149
150                 // Return the instance
151                 return $commandInstance;
152         }
153
154         /**
155          * "Loads" a given command and instances it if not yet cached
156          *
157          * @param       $commandName                            A command name we shall look for
158          * @return      $commandInstance                        A loaded command instance
159          * @throws      InvalidCommandException         Thrown if even the default
160          *                                                                              command class is missing (bad!)
161          */
162         private function loadCommand ($commandName) {
163                 // Cache default command
164                 $defaultCommand = $this->getConfigInstance()->readConfig('default_command');
165
166                 // Init command instance
167                 $commandInstance = null;
168
169                 // Create command class name
170                 $className = sprintf("Web%sCommand",
171                         $this->convertToClassName($commandName)
172                 );
173
174                 // Is this class loaded?
175                 if (!class_exists($className)) {
176                         // Class not found, so try the default one or throw exception
177                         if ($commandName != $defaultCommand) {
178                                 // Try the default command
179                                 return $this->loadCommand($defaultCommand);
180                         } else {
181                                 // Still not found?
182                                 throw new InvalidCommandException(array($this, $defaultCommand), self::EXCEPTION_INVALID_COMMAND);
183                         }
184                 }
185
186                 // Initiate the command
187                 $eval = sprintf("\$commandInstance = %s::create%s(\$this);",
188                         $className,
189                         $className
190                 );
191
192                 // Run the command
193                 eval($eval);
194
195                 // Is the instance valid?
196                 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
197                         // Something is wrong
198                         $commandInstance = null;
199                 }
200
201                 // Return the result
202                 return $commandInstance;
203         }
204 }
205
206 // [EOF]
207 ?>