3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Controller\DefaultControllerException;
7 use CoreFramework\Controller\Controller;
8 use CoreFramework\Factory\ObjectFactory;
9 use CoreFramework\Generic\EmptyVariableException;
10 use CoreFramework\Registry\Registry;
11 use CoreFramework\Resolver\BaseResolver;
12 use CoreFramework\Resolver\Controller\ControllerResolver;
15 * A generic controller resolver class
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class BaseControllerResolver extends BaseResolver {
38 * Protected constructor
40 * @param $className Name of the real class
43 protected function __construct ($className) {
44 // Call parent constructor
45 parent::__construct($className);
49 * "Loads" a given controller and instances it if not yet cached. If the
50 * controller was not found one of the default controllers will be used
51 * depending on whether news shall be displayed.
53 * @param $controllerName A controller name we shall look for
54 * @return $controllerInstance A loaded controller instance
55 * @throws InvalidControllerException Thrown if even the requested
56 * controller class is missing (bad!)
58 protected function loadController ($controllerName) {
59 // Cache default controller
60 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
62 // Init controller instance
63 $controllerInstance = NULL;
65 // Create full class name
67 '%s\%sDefaultNewsController',
68 $this->getNamespace(),
69 $this->getCapitalizedClassPrefix()
73 $this->setClassName($className);
75 // Generate the class name
76 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
77 if ($controllerName != $defaultController) {
78 // Create controller class name
82 $this->getCapitalizedClassPrefix(),
83 self::convertToClassName($controllerName)
87 $this->setClassName($className);
89 // No news at main controller or non-news controller
90 $this->setClassName($className);
92 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->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 controller resolver
107 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
109 // Initiate the resolver and controller
110 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
111 $resolverConfigEntry,
114 Registry::getRegistry()->getInstance('app')
117 $controllerInstance = ObjectFactory::createObjectByName(
118 $this->getClassName(),
119 array($resolverInstance)
123 return $controllerInstance;
127 * Checks whether the given controller is valid
129 * @param $namespace Namespace to look in, no trailing backslash
130 * @param $controllerName The default controller we shall execute
131 * @return $isValid Whether the given controller is valid
132 * @throws EmptyVariableException Thrown if given controller is not set
133 * @throws DefaultControllerException Thrown if default controller was not found
135 protected function isControllerValid ($namespace, $controllerName) {
136 // By default nothing shall be valid
139 // Is namespace and controller name set?
140 if (empty($namespace)) {
141 // Then thrown an exception here
142 throw new EmptyVariableException(array($this, 'namespace'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
143 } elseif (empty($controllerName)) {
144 // Then thrown an exception here
145 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
149 $className = sprintf(
152 $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
154 $newsControllerName = sprintf(
155 '%s\%sDefaultNewsController',
157 $this->getCapitalizedClassPrefix()
161 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
163 // Now, let us create the full name of the controller class
164 $this->setClassName($className);
166 // Try it hard to get an controller
167 while ($isValid === false) {
168 // Is this class already loaded?
169 if (class_exists($this->getClassName())) {
170 // This class does exist. :-)
172 } elseif ($this->getClassName() != $newsControllerName) {
173 // Set default controller
174 $this->setClassName($newsControllerName);
176 // All is tried, give it up here
177 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
186 * Resolves the default controller of the given controller
188 * @return $controllerInstance A controller instance for the default
190 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
193 public function resolveController () {
195 $controllerName = '';
196 $controllerInstance = NULL;
198 // Get namespace and controller name
199 $controllerName = $this->getControllerName();
201 // Get the controller
202 $controllerInstance = $this->loadController($controllerName);
205 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
206 // This controller has an invalid instance!
207 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
210 // Set last controller
211 $this->setResolvedInstance($controllerInstance);
213 // Return the maybe resolved instance
214 return $controllerInstance;