3 * A resolver for resolving controllers locally
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class WebControllerResolver extends BaseResolver implements ControllerResolver {
26 * Last successfull resolved controller (name)
28 private $lastControllerName = "";
31 * Last successfull resolved controller (instance)
33 private $lastControllerInstance = null;
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
44 // Set part description
45 $this->setObjectDescription("");
47 // Create unique ID number
48 $this->createUniqueID();
50 // Set prefix to "Web"
51 $this->setCommandPrefix("Web");
55 * Creates an instance of a resolver class with a given command
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
63 public final static function createWebControllerResolver ($commandName, ManageableApplication $appInstance) {
64 // Create the new instance
65 $resolverInstance = new WebControllerResolver();
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);
76 // Set the application instance
77 $resolverInstance->setApplicationInstance($appInstance);
79 // Return the prepared instance
80 return $resolverInstance;
84 * Resolves the default controller of the given command
86 * @return $controllerInstance A controller instance for the default
88 * @throws InvalidCommandException Thrown if $commandName is
90 * @throws InvalidControllerInstanceException Thrown if $commandInstance
93 public function resolveCommandController () {
96 $controllerInstance = null;
98 // Try to resolv the command
100 // Get the command name
101 $commandName = $this->getCommandName();
103 // Check if the command is valid
104 if (!$this->isCommandValid($commandName)) {
105 // This command is invalid!
106 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
110 $controllerInstance = $this->loadController($commandName);
113 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
114 // This command has an invalid instance!
115 throw new InvalidControllerInstanceException(array($this, $commandName), self::EXCEPTION_INVALID_CONTROLLER);
118 // Set last controller
119 $this->lastControllerInstance = $controllerInstance;
120 } catch (MissingArrayElementsException $e) {
121 // Just catch it here...
124 // Return the maybe resolved instance
125 return $controllerInstance;
129 * "Loads" a given controller and instances it if not yet cached
131 * @param $commandName A controller name we shall look for
132 * @return $controllerInstance A loaded controller instance
133 * @throws DefaultControllerException Thrown if even the default
134 * controller class is missing (bad!)
136 private function loadController ($commandName) {
138 //print("<strong>----- ".__METHOD__." -----</strong><pre>");
139 //debug_print_backtrace();
143 // Cache default command
144 $defaultCommand = $this->getConfigInstance()->readConfig('default_command');
146 // Init controller instance
147 $controllerInstance = null;
149 // Default controller
150 $class = "WebDefaultController";
152 // Generate the class name
153 if ($commandName != $defaultCommand) {
154 // Create controller class name
155 $class = sprintf("Web%sController",
156 $this->convertToClassName($commandName)
158 } elseif ($this->getConfigInstance()->readConfig('home_with_news') == "Y") {
159 // Yes, display news in home then set default controller with news
160 $class = "WebDefaultNewsController";
162 // No nes at "home" page
163 $class = "WebDefaultController";
166 // Is this class loaded?
167 if (!class_exists($class)) {
168 // Class not found, so try the default one or throw exception
169 if ($commandName != $defaultCommand) {
170 // Try the default controller
171 return $this->loadController($defaultCommand);
174 throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE);
178 // Initiate the controller
179 $eval = sprintf("\$controllerInstance = %s::create%s(WebCommandResolver::createWebCommandResolver(\$commandName, \$this->getApplicationInstance()));",
187 // Is the instance valid?
188 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
189 // Something is wrong
190 $controllerInstance = null;
194 return $controllerInstance;