Files merged from ship-simu project
[mailer.git] / inc / classes / main / resolver / web / class_WebControllerResolver.php
1 <?php
2 /**
3  * A resolver for resolving controllers locally
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 WebControllerResolver extends BaseResolver implements ControllerResolver {
25         /**
26          * Last successfull resolved controller
27          */
28         private $lastControllerName = "";
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("");
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 resolver class with a given command
55          *
56          * @param       $commandName                            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 createWebControllerResolver ($commandName, ManageableApplication $appInstance) {
63                 // Create the new instance
64                 $resolverInstance = new WebControllerResolver();
65
66                 // Is the variable $commandName set and the command is valid?
67                 if (empty($commandName)) {
68                         // Then thrown an exception here
69                         throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
70                 } elseif (!$resolverInstance->isCommandValid($commandName)) {
71                         // Invalid command found
72                         throw new InvalidCommandException(array($resolverInstance, $commandName), 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          * Resolves the default controller of the given command
84          *
85          * @return      $controllerInstance             A controller instance for the default
86          *                                                                      command
87          * @throws      InvalidCommandException                         Thrown if $commandName is
88          *                                                                                              invalid
89          * @throws      InvalidControllerInstanceException      Thrown if $commandInstance
90          *                                                                                              is invalid
91          */
92         public function resolveDefaultController () {
93                 // Init variables
94                 $commandName = "";
95                 $controllerInstance = null;
96
97                 // Try to resolv the command
98                 try {
99                         // Get the command name 
100                         $commandName = $this->getCommandName();
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                         $controllerInstance = $this->loadController($commandName);
110
111                         // And validate it
112                         if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
113                                 // This command has an invalid instance!
114                                 throw new InvalidControllerInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_CONTROLLER);
115                         }
116
117                         // Set last command
118                         $this->lastCommandInstance = $controllerInstance;
119                 } catch (MissingArrayElementsException $e) {
120                         // Just catch it here...
121                 }
122
123                 // Return the maybe resolved instance
124                 return $controllerInstance;
125         }
126
127         /**
128          * "Loads" a given controller and instances it if not yet cached
129          *
130          * @param       $commandName                    A controller name we shall look for
131          * @return      $controllerInstance             A loaded controller instance
132          * @throws      DefaultControllerException      Thrown if even the default
133          *                                                                              controller class is missing (bad!)
134          */
135         private function loadController ($commandName) {
136                 // Cache default command
137                 $defaultCommand = $this->getConfigInstance()->readConfig("default_command");
138
139                 // Init controller instance
140                 $controllerInstance = null;
141
142                 // Default controller
143                 $class = "WebDefaultController";
144
145                 // Generate the class name
146                 if ($commandName != $defaultCommand) {
147                         // Create controller class name
148                         $class = sprintf("Web%sController",
149                                 ucfirst(strtolower($commandName))
150                         );
151                 } elseif ($this->getConfigInstance()->readConfig("home_with_news") == "Y") {
152                         // Yes, display news in home then set default controller with news
153                         $class = "WebDefaultNewsController";
154                 }
155
156                 // Is this class loaded?
157                 if (!class_exists($class)) {
158                         // Class not found, so try the default one or throw exception
159                         if ($commandName != $defaultCommand) {
160                                 // Try the default controller
161                                 return $this->loadController($defaultCommand);
162                         } else {
163                                 // Still not found?
164                                 throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE);
165                         }
166                 }
167
168                 // Initiate the controller
169                 $eval = sprintf("\$controllerInstance = %s::create%s(WebCommandResolver::createWebCommandResolver(\$commandName, \$this->getApplicationInstance()));",
170                         $class,
171                         $class
172                 );
173
174                 // Run the command
175                 eval($eval);
176
177                 // Is the instance valid?
178                 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
179                         // Something is wrong
180                         $controllerInstance = null;
181                 }
182
183                 // Return the result
184                 return $controllerInstance;
185         }
186 }
187
188 // [EOF]
189 ?>