3 namespace CoreFramework\Resolver\Controller;
6 * A generic controller resolver class
8 * @author Roland Haeder <webmaster@shipsimu.org>
10 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.shipsimu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class BaseControllerResolver extends BaseResolver {
29 * Protected constructor
31 * @param $className Name of the real class
34 protected function __construct ($className) {
35 // Call parent constructor
36 parent::__construct($className);
40 * "Loads" a given controller and instances it if not yet cached. If the
41 * controller was not found one of the default controllers will be used
42 * depending on whether news shall be displayed.
44 * @param $controllerName A controller name we shall look for
45 * @return $controllerInstance A loaded controller instance
46 * @throws InvalidControllerException Thrown if even the requested
47 * controller class is missing (bad!)
49 protected function loadController ($controllerName) {
50 // Cache default controller
51 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
53 // Init controller instance
54 $controllerInstance = NULL;
57 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
59 // Generate the class name
60 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('BEFORE: controller=' . $controllerName);
61 if ($controllerName != $defaultController) {
62 // Create controller class name
63 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) . 'Controller';
66 $this->setClassName($className);
68 // No news at main controller or non-news controller
69 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
71 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('AFTER: controller=' . $this->getClassName());
73 // Is this class loaded?
74 if (!class_exists($this->getClassName())) {
75 // Throw an exception here
76 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
79 // Set default resolver config name
80 $resolverConfigEntry = '';
82 // Try to read a config entry for our resolver including controller name... ;-)
83 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
85 // Get the config, this will throw an exception if there is no special controller resolver
86 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
88 // Initiate the resolver and controller
89 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
93 $this->getApplicationInstance()
96 $controllerInstance = ObjectFactory::createObjectByName(
97 $this->getClassName(),
98 array($resolverInstance)
102 return $controllerInstance;
106 * Checks whether the given controller is valid
108 * @param $controllerName The default controller we shall execute
109 * @return $isValid Whether the given controller is valid
110 * @throws EmptyVariableException Thrown if given controller is not set
111 * @throws DefaultControllerException Thrown if default controller was not found
113 public function isControllerValid ($controllerName) {
114 // By default nothing shall be valid
117 // Is a controller set?
118 if (empty($controllerName)) {
119 // Then thrown an exception here
120 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
124 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) . 'Controller';
126 // Now, let us create the full name of the controller class
127 $this->setClassName($className);
129 // Try it hard to get an controller
130 while ($isValid === FALSE) {
131 // Is this class already loaded?
132 if (class_exists($this->getClassName())) {
133 // This class does exist. :-)
135 } elseif ($this->getClassName() != $this->getCapitalizedClassPrefix() . 'DefaultNewsController') {
136 // Set default controller
137 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
139 // All is tried, give it up here
140 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
149 * Resolves the default controller of the given controller
151 * @return $controllerInstance A controller instance for the default
153 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
156 public function resolveController () {
158 $controllerName = '';
159 $controllerInstance = NULL;
161 // Get the controller name
162 $controllerName = $this->getControllerName();
164 // Get the controller
165 $controllerInstance = $this->loadController($controllerName);
168 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
169 // This controller has an invalid instance!
170 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
173 // Set last controller
174 $this->setResolvedInstance($controllerInstance);
176 // Return the maybe resolved instance
177 return $controllerInstance;