]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/resolver/web/class_WebControllerResolver.php
Intercepting filter basicly added, generic form processor implemented (unfinished...
[shipsimu.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 part description
45                 $this->setObjectDescription("");
46
47                 // Create unique ID number
48                 $this->createUniqueID();
49
50                 // Clean up a little
51                 $this->removeNumberFormaters();
52                 $this->removeSystemArray();
53
54                 // Set prefix to "Web"
55                 $this->setCommandPrefix("Web");
56         }
57
58         /**
59          * Creates an instance of a resolver class with a given command
60          *
61          * @param       $commandName                            The default command we shall execute
62          * @param       $appInstance                            An instance of a manageable application helper class
63          * @return      $resolverInstance                       The prepared command resolver instance
64          * @throws      EmptyVariableException          Thrown if the default command is not set
65          * @throws      InvalidCommandException         Thrown if the default command is invalid
66          */
67         public final static function createWebControllerResolver ($commandName, ManageableApplication $appInstance) {
68                 // Create the new instance
69                 $resolverInstance = new WebControllerResolver();
70
71                 // Is the variable $commandName set and the command is valid?
72                 if (empty($commandName)) {
73                         // Then thrown an exception here
74                         throw new EmptyVariableException(array($resolverInstance, 'commandName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
75                 } elseif (!$resolverInstance->isCommandValid($commandName)) {
76                         // Invalid command found
77                         throw new InvalidCommandException(array($resolverInstance, $commandName), self::EXCEPTION_INVALID_COMMAND);
78                 }
79
80                 // Set the application instance
81                 $resolverInstance->setApplicationInstance($appInstance);
82
83                 // Return the prepared instance
84                 return $resolverInstance;
85         }
86
87         /**
88          * Resolves the default controller of the given command
89          *
90          * @return      $controllerInstance             A controller instance for the default
91          *                                                                      command
92          * @throws      InvalidCommandException                         Thrown if $commandName is
93          *                                                                                              invalid
94          * @throws      InvalidControllerInstanceException      Thrown if $commandInstance
95          *                                                                                              is invalid
96          */
97         public function resolveDefaultController () {
98                 // Init variables
99                 $commandName = "";
100                 $controllerInstance = null;
101
102                 // Try to resolv the command
103                 try {
104                         // Get the command name 
105                         $commandName = $this->getCommandName();
106
107                         // Check if the command is valid
108                         if (!$this->isCommandValid($commandName)) {
109                                 // This command is invalid!
110                                 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
111                         }
112
113                         // Get the command
114                         $controllerInstance = $this->loadController($commandName);
115
116                         // And validate it
117                         if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
118                                 // This command has an invalid instance!
119                                 throw new InvalidControllerInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_CONTROLLER);
120                         }
121
122                         // Set last controller
123                         $this->lastControllerInstance = $controllerInstance;
124                 } catch (MissingArrayElementsException $e) {
125                         // Just catch it here...
126                 }
127
128                 // Return the maybe resolved instance
129                 return $controllerInstance;
130         }
131
132         /**
133          * "Loads" a given controller and instances it if not yet cached
134          *
135          * @param       $commandName                    A controller name we shall look for
136          * @return      $controllerInstance             A loaded controller instance
137          * @throws      DefaultControllerException      Thrown if even the default
138          *                                                                              controller class is missing (bad!)
139          */
140         private function loadController ($commandName) {
141                  // Debug message
142                  //print("<strong>----- ".__METHOD__." -----</strong><pre>");
143                  //debug_print_backtrace();
144                  //print("</pre>");
145                  //
146
147                 // Cache default command
148                 $defaultCommand = $this->getConfigInstance()->readConfig("default_command");
149
150                 // Init controller instance
151                 $controllerInstance = null;
152
153                 // Default controller
154                 $class = "WebDefaultController";
155
156                 // Generate the class name
157                 if ($commandName != $defaultCommand) {
158                         // Create controller class name
159                         $class = sprintf("Web%sController",
160                                 $this->convertToClassName($commandName)
161                         );
162                 } elseif ($this->getConfigInstance()->readConfig("home_with_news") == "Y") {
163                         // Yes, display news in home then set default controller with news
164                         $class = "WebDefaultNewsController";
165                 } else {
166                         // No nes at "home" page
167                         $class = "WebDefaultController";
168                 }
169
170                 // Is this class loaded?
171                 if (!class_exists($class)) {
172                         // Class not found, so try the default one or throw exception
173                         if ($commandName != $defaultCommand) {
174                                 // Try the default controller
175                                 return $this->loadController($defaultCommand);
176                         } else {
177                                 // Still not found?
178                                 throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE);
179                         }
180                 }
181
182                 // Initiate the controller
183                 $eval = sprintf("\$controllerInstance = %s::create%s(WebCommandResolver::createWebCommandResolver(\$commandName, \$this->getApplicationInstance()));",
184                         $class,
185                         $class
186                 );
187
188                 // Run the command
189                 eval($eval);
190
191                 // Is the instance valid?
192                 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
193                         // Something is wrong
194                         $controllerInstance = null;
195                 }
196
197                 // Return the result
198                 return $controllerInstance;
199         }
200 }
201
202 // [EOF]
203 ?>