Launcher scripts updated
[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 (name)
27          */
28         private $lastControllerName = "";
29
30         /**
31          * Last successfull resolved controller (instance)
32          */
33         private $lastControllerInstance = null;
34
35         /**
36          * Protected constructor
37          *
38          * @return      void
39          */
40         protected function __construct () {
41                 // Call parent constructor
42                 parent::__construct(__CLASS__);
43
44                 // Set prefix to "Web"
45                 $this->setCommandPrefix("Web");
46         }
47
48         /**
49          * Creates an instance of a resolver class with a given command
50          *
51          * @param       $commandName                            The default command we shall execute
52          * @param       $appInstance                            An instance of a manageable application helper class
53          * @return      $resolverInstance                       The prepared command resolver instance
54          * @throws      EmptyVariableException          Thrown if the default command is not set
55          * @throws      InvalidCommandException         Thrown if the default command is invalid
56          */
57         public final static function createWebControllerResolver ($commandName, ManageableApplication $appInstance) {
58                 // Create the new instance
59                 $resolverInstance = new WebControllerResolver();
60
61                 // Is the variable $commandName set and the command is valid?
62                 if (empty($commandName)) {
63                         // Then thrown an exception here
64                         throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
65                 } elseif (!$resolverInstance->isCommandValid($commandName)) {
66                         // Invalid command found
67                         throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND);
68                 }
69
70                 // Set the application instance
71                 $resolverInstance->setApplicationInstance($appInstance);
72
73                 // Return the prepared instance
74                 return $resolverInstance;
75         }
76
77         /**
78          * Resolves the default controller of the given command
79          *
80          * @return      $controllerInstance             A controller instance for the default
81          *                                                                      command
82          * @throws      InvalidCommandException                         Thrown if $commandName is
83          *                                                                                              invalid
84          * @throws      InvalidControllerInstanceException      Thrown if $commandInstance
85          *                                                                                              is invalid
86          */
87         public function resolveCommandController () {
88                 // Init variables
89                 $commandName = "";
90                 $controllerInstance = null;
91
92                 // Try to resolv the command
93                 try {
94                         // Get the command name 
95                         $commandName = $this->getCommandName();
96
97                         // Check if the command is valid
98                         if (!$this->isCommandValid($commandName)) {
99                                 // This command is invalid!
100                                 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
101                         }
102
103                         // Get the command
104                         $controllerInstance = $this->loadController($commandName);
105
106                         // And validate it
107                         if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
108                                 // This command has an invalid instance!
109                                 throw new InvalidControllerInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_CONTROLLER);
110                         }
111
112                         // Set last controller
113                         $this->lastControllerInstance = $controllerInstance;
114                 } catch (MissingArrayElementsException $e) {
115                         // Just catch it here...
116                 }
117
118                 // Return the maybe resolved instance
119                 return $controllerInstance;
120         }
121
122         /**
123          * "Loads" a given controller and instances it if not yet cached. If the
124          * controller was not found one of the default controllers will be used
125          * depending on wether news shall be displayed.
126          *
127          * @param       $commandName                    A controller name we shall look for
128          * @return      $controllerInstance             A loaded controller instance
129          * @throws      DefaultControllerException      Thrown if even the default
130          *                                                                              controller class is missing (bad!)
131          */
132         private function loadController ($commandName) {
133                  // Debug message
134                  //print("<strong>----- ".__METHOD__." -----</strong><pre>");
135                  //debug_print_backtrace();
136                  //print("</pre>");
137                  //
138
139                 // Cache default command
140                 $defaultCommand = $this->getConfigInstance()->readConfig('default_command');
141
142                 // Init controller instance
143                 $controllerInstance = null;
144
145                 // Default controller
146                 $class = "WebDefaultController";
147
148                 // Generate the class name
149                 //* DEBUG: */ echo __METHOD__.": Command=".$commandName;
150                 if ($commandName != $defaultCommand) {
151                         // Create controller class name
152                         $class = sprintf("Web%sController",
153                                 $this->convertToClassName($commandName)
154                         );
155                 } elseif ($this->getConfigInstance()->readConfig('home_with_news') == "Y") {
156                         // Yes, display news in home then set default controller with news
157                         $class = "WebDefaultNewsController";
158                 } else {
159                         // No nes at "home" page
160                         $class = "WebDefaultController";
161                 }
162                 //* DEBUG: */ echo ", controller=".$class."<br />\n";
163
164                 // Is this class loaded?
165                 if (!class_exists($class)) {
166                         // Class not found, so try the default one or throw exception
167                         if ($commandName != $defaultCommand) {
168                                 // Try the default controller
169                                 return $this->loadController($defaultCommand);
170                         } else {
171                                 // Still not found?
172                                 throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE);
173                         }
174                 } // END - if
175
176                 // Initiate the resolver and controller
177                 $resolverInstance = ObjectFactory::createObjectByConfiguredName('web_cmd_resolver_class', array($commandName, $this->getApplicationInstance()));
178                 $controllerInstance = ObjectFactory::createObjectByName($class, array($resolverInstance));
179
180                 // Remove resolver
181                 unset($resolverInstance);
182
183                 // Return the result
184                 return $controllerInstance;
185         }
186 }
187
188 // [EOF]
189 ?>