Files merged from ship-simu project
[mailer.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          * Private 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("Lokaler Kommandoaufl&ouml;ser");
41
42                 // Create unique ID number
43                 $this->createUniqueID();
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48
49                 // Set prefix to "Web"
50                 $this->setCommandPrefix("Web");
51         }
52
53         /**
54          * Creates an instance of a Web command resolver with a given default command
55          *
56          * @param       $defaultCommand                         The default command we shall execute
57          * @param       $appInstance                            An instance of a manageable application helper class
58          * @return      $resolverInstance                       The prepared command resolver instance
59          * @throws      EmptyVariableException          Thrown if the default command is not set
60          * @throws      InvalidCommandException         Thrown if the default command is invalid
61          */
62         public final static function createWebCommandResolver ($defaultCommand, ManageableApplication $appInstance) {
63                 // Create the new instance
64                 $resolverInstance = new WebCommandResolver();
65
66                 // Is the variable $defaultCommand set and the command is valid?
67                 if (empty($defaultCommand)) {
68                         // Then thrown an exception here
69                         throw new EmptyVariableException(array($resolverInstance, 'defaultCommand'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
70                 } elseif (!$resolverInstance->isCommandValid($defaultCommand)) {
71                         // Invalid command found
72                         throw new InvalidCommandException(array($resolverInstance, $defaultCommand), self::EXCEPTION_INVALID_COMMAND);
73                 }
74
75                 // Set the application instance
76                 $resolverInstance->setApplicationInstance($appInstance);
77
78                 // Return the prepared instance
79                 return $resolverInstance;
80         }
81
82         /**
83          * Returns an command instance for a given request class or null if
84          * MissingArrayElementsException was thrown
85          *
86          * @param       $requestInstance        An instance of a request 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 resolvCommandByRequest (Requestable $requestInstance) {
94                 // Init variables
95                 $commandName = "";
96                 $commandInstance = null;
97
98                 // Test if the required parameter is set
99                 try {
100                         // This goes fine so let's resolv the command
101                         $commandName = $requestInstance->getRequestElement($this->getConfigInstance()->readConfig("command_parameter"));
102
103                         // Is the command empty? Then fall back to default command
104                         if (empty($commandName)) $commandName = $this->getConfigInstance()->readConfig("default_command");
105
106                         // Check if the command is valid
107                         if (!$this->isCommandValid($commandName)) {
108                                 // This command is invalid!
109                                 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
110                         }
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                         }
120
121                         // Set last command
122                         $this->lastCommandInstance = $commandInstance;
123                 } catch (MissingArrayElementsException $e) {
124                         // Just catch it here...
125                 }
126
127                 // Return the resolved command instance
128                 return $commandInstance;
129         }
130
131         /**
132          * "Loads" a given command and instances it if not yet cached
133          *
134          * @param       $commandName                            A command name we shall look for
135          * @return      $commandInstance                        A loaded command instance
136          * @throws      InvalidCommandException         Thrown if even the default
137          *                                                                              command class is missing (bad!)
138          */
139         private function loadCommand ($commandName) {
140                 // Cache default command
141                 $defaultCommand = $this->getConfigInstance()->readConfig("default_command");
142
143                 // Init command instance
144                 $commandInstance = null;
145
146                 // Create command class name
147                 $class = sprintf("Web%sCommand",
148                         ucfirst(strtolower($commandName))
149                 );
150
151                 // Is this class loaded?
152                 if (!class_exists($class)) {
153                         // Class not found, so try the default one or throw exception
154                         if ($commandName != $defaultCommand) {
155                                 // Try the default command
156                                 return $this->loadCommand($defaultCommand);
157                         } else {
158                                 // Still not found?
159                                 throw new InvalidCommandException(array($this, $defaultCommand), self::EXCEPTION_INVALID_COMMAND);
160                         }
161                 }
162
163                 // Initiate the command
164                 $eval = sprintf("\$commandInstance = %s::create%s(\$this);",
165                         $class,
166                         $class
167                 );
168
169                 // Run the command
170                 eval($eval);
171
172                 // Is the instance valid?
173                 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
174                         // Something is wrong
175                         $commandInstance = null;
176                 }
177
178                 // Return the result
179                 return $commandInstance;
180         }
181 }
182
183 // [EOF]
184 ?>