3 * A generic controller resolver class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 BaseControllerResolver extends BaseResolver {
26 * Validated controller name
28 private $controllerName = '';
31 * Protected constructor
33 * @param $className Name of the real class
36 protected function __construct ($className) {
37 // Call parent constructor
38 parent::__construct($className);
42 * Setter for controller name
44 * @param $controllerName Last validated controller name
47 protected final function setControllerName ($controllerName) {
48 $this->controllerName = $controllerName;
52 * Getter for controller name
54 * @return $controllerName Last validated controller name
56 public final function getControllerName () {
57 return $this->controllerName;
61 * "Loads" a given controller and instances it if not yet cached. If the
62 * controller was not found one of the default controllers will be used
63 * depending on whether news shall be displayed.
65 * @param $controllerName A controller name we shall look for
66 * @return $controllerInstance A loaded controller instance
67 * @throws InvalidControllerException Thrown if even the requested
68 * controller class is missing (bad!)
70 protected function loadController ($controllerName) {
71 // Cache default command
72 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_command');
74 // Init controller instance
75 $controllerInstance = NULL;
78 $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
80 // Generate the class name
81 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BEFORE: controller=' . $controllerName);
82 if ($controllerName != $defaultController) {
83 // Create controller class name
84 $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
87 $this->setClassName($className);
89 // No news at main command or non-news command
90 $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
92 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('AFTER: controller=' . $this->getClassName());
94 // Is this class loaded?
95 if (!class_exists($this->getClassName())) {
96 // Throw an exception here
97 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
100 // Set default resolver config name
101 $resolverConfigEntry = '';
103 // Try to read a config entry for our resolver including controller name... ;-)
104 $resolverConfigEntry = sprintf("%s_cmd_%s_resolver_class", strtolower($this->getClassPrefix()), strtolower($controllerName));
106 // Get the config, this will throw an exception if there is no special command resolver
107 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
109 // Initiate the resolver and controller
110 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
111 $resolverConfigEntry,
114 $this->getApplicationInstance()
117 $controllerInstance = ObjectFactory::createObjectByName(
118 $this->getClassName(),
119 array($resolverInstance)
123 return $controllerInstance;
127 * Checks whether the given controller is valid
129 * @param $controllerName The default controller we shall execute
130 * @return $isValid Whether the given controller is valid
131 * @throws EmptyVariableException Thrown if given controller is not set
132 * @throws DefaultControllerException Thrown if default controller was not found
134 public function isControllerValid ($controllerName) {
135 // By default nothing shall be valid
138 // Is a controller set?
139 if (empty($controllerName)) {
140 // Then thrown an exception here
141 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
145 $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
147 // Now, let us create the full name of the controller class
148 $this->setClassName($className);
150 // Try it hard to get an controller
151 while ($isValid === FALSE) {
152 // Is this class already loaded?
153 if (class_exists($this->getClassName())) {
154 // This class does exist. :-)
156 } elseif ($this->getClassName() != $this->getClassPrefix() . 'DefaultNewsController') {
157 // Set default controller
158 $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
160 // All is tried, give it up here
161 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);