3 * A generic controller resolver class
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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 * Protected constructor
28 * @param $className Name of the real class
31 protected function __construct ($className) {
32 // Call parent constructor
33 parent::__construct($className);
37 * "Loads" a given controller and instances it if not yet cached. If the
38 * controller was not found one of the default controllers will be used
39 * depending on whether news shall be displayed.
41 * @param $controllerName A controller name we shall look for
42 * @return $controllerInstance A loaded controller instance
43 * @throws InvalidControllerException Thrown if even the requested
44 * controller class is missing (bad!)
46 protected function loadController ($controllerName) {
47 // Cache default controller
48 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
50 // Init controller instance
51 $controllerInstance = NULL;
54 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
56 // Generate the class name
57 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BEFORE: controller=' . $controllerName);
58 if ($controllerName != $defaultController) {
59 // Create controller class name
60 $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
63 $this->setClassName($className);
65 // No news at main controller or non-news controller
66 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
68 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('AFTER: controller=' . $this->getClassName());
70 // Is this class loaded?
71 if (!class_exists($this->getClassName())) {
72 // Throw an exception here
73 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
76 // Set default resolver config name
77 $resolverConfigEntry = '';
79 // Try to read a config entry for our resolver including controller name... ;-)
80 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
82 // Get the config, this will throw an exception if there is no special controller resolver
83 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
85 // Initiate the resolver and controller
86 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
90 $this->getApplicationInstance()
93 $controllerInstance = ObjectFactory::createObjectByName(
94 $this->getClassName(),
95 array($resolverInstance)
99 return $controllerInstance;
103 * Checks whether the given controller is valid
105 * @param $controllerName The default controller we shall execute
106 * @return $isValid Whether the given controller is valid
107 * @throws EmptyVariableException Thrown if given controller is not set
108 * @throws DefaultControllerException Thrown if default controller was not found
110 public function isControllerValid ($controllerName) {
111 // By default nothing shall be valid
114 // Is a controller set?
115 if (empty($controllerName)) {
116 // Then thrown an exception here
117 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
121 $className = $this->getCapitalizedClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
123 // Now, let us create the full name of the controller class
124 $this->setClassName($className);
126 // Try it hard to get an controller
127 while ($isValid === FALSE) {
128 // Is this class already loaded?
129 if (class_exists($this->getClassName())) {
130 // This class does exist. :-)
132 } elseif ($this->getClassName() != $this->getCapitalizedClassPrefix() . 'DefaultNewsController') {
133 // Set default controller
134 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
136 // All is tried, give it up here
137 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
146 * Resolves the default controller of the given controller
148 * @return $controllerInstance A controller instance for the default
150 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
153 public function resolveController () {
155 $controllerName = '';
156 $controllerInstance = NULL;
158 // Get the controller name
159 $controllerName = $this->getControllerName();
161 // Get the controller
162 $controllerInstance = $this->loadController($controllerName);
165 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
166 // This controller has an invalid instance!
167 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
170 // Set last controller
171 $this->setResolvedInstance($controllerInstance);
173 // Return the maybe resolved instance
174 return $controllerInstance;