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