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
28 private $lastControllerName = "";
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
39 // Set part description
40 $this->setObjectDescription("");
42 // Create unique ID number
43 $this->createUniqueID();
46 $this->removeNumberFormaters();
47 $this->removeSystemArray();
49 // Set prefix to "Web"
50 $this->setCommandPrefix("Web");
54 * Creates an instance of a resolver class with a given command
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
62 public final static function createWebControllerResolver ($commandName, ManageableApplication $appInstance) {
63 // Create the new instance
64 $resolverInstance = new WebControllerResolver();
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);
75 // Set the application instance
76 $resolverInstance->setApplicationInstance($appInstance);
78 // Return the prepared instance
79 return $resolverInstance;
83 * Resolves the default controller of the given command
85 * @return $controllerInstance A controller instance for the default
87 * @throws InvalidCommandException Thrown if $commandName is
89 * @throws InvalidControllerInstanceException Thrown if $commandInstance
92 public function resolveDefaultController () {
95 $controllerInstance = null;
97 // Try to resolv the command
99 // Get the command name
100 $commandName = $this->getCommandName();
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);
109 $controllerInstance = $this->loadController($commandName);
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);
118 $this->lastCommandInstance = $controllerInstance;
119 } catch (MissingArrayElementsException $e) {
120 // Just catch it here...
123 // Return the maybe resolved instance
124 return $controllerInstance;
128 * "Loads" a given controller and instances it if not yet cached
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!)
135 private function loadController ($commandName) {
136 // Cache default command
137 $defaultCommand = $this->getConfigInstance()->readConfig("default_command");
139 // Init controller instance
140 $controllerInstance = null;
142 // Default controller
143 $class = "WebDefaultController";
145 // Generate the class name
146 if ($commandName != $defaultCommand) {
147 // Create controller class name
148 $class = sprintf("Web%sController",
149 ucfirst(strtolower($commandName))
151 } elseif ($this->getConfigInstance()->readConfig("home_with_news") == "Y") {
152 // Yes, display news in home then set default controller with news
153 $class = "WebDefaultNewsController";
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);
164 throw new DefaultControllerException($this, self::EXCEPTION_DEFAUL_CONTROLLER_GONE);
168 // Initiate the controller
169 $eval = sprintf("\$controllerInstance = %s::create%s(WebCommandResolver::createWebCommandResolver(\$commandName, \$this->getApplicationInstance()));",
177 // Is the instance valid?
178 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
179 // Something is wrong
180 $controllerInstance = null;
184 return $controllerInstance;