3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Resolver\BaseResolver;
10 * A generic controller resolver class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseControllerResolver extends BaseResolver {
33 * Protected constructor
35 * @param $className Name of the real class
38 protected function __construct ($className) {
39 // Call parent constructor
40 parent::__construct($className);
44 * "Loads" a given controller and instances it if not yet cached. If the
45 * controller was not found one of the default controllers will be used
46 * depending on whether news shall be displayed.
48 * @param $controllerName A controller name we shall look for
49 * @return $controllerInstance A loaded controller instance
50 * @throws InvalidControllerException Thrown if even the requested
51 * controller class is missing (bad!)
53 protected function loadController ($controllerName) {
54 // Cache default controller
55 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
57 // Init controller instance
58 $controllerInstance = NULL;
61 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
63 // Generate the class name
64 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
65 if ($controllerName != $defaultController) {
66 // Create controller class name
67 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) . 'Controller';
70 $this->setClassName($className);
72 // No news at main controller or non-news controller
73 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
75 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
77 // Is this class loaded?
78 if (!class_exists($this->getClassName())) {
79 // Throw an exception here
80 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
83 // Set default resolver config name
84 $resolverConfigEntry = '';
86 // Try to read a config entry for our resolver including controller name... ;-)
87 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
89 // Get the config, this will throw an exception if there is no special controller resolver
90 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
92 // Initiate the resolver and controller
93 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
97 $this->getApplicationInstance()
100 $controllerInstance = ObjectFactory::createObjectByName(
101 $this->getClassName(),
102 array($resolverInstance)
106 return $controllerInstance;
110 * Checks whether the given controller is valid
112 * @param $controllerName The default controller we shall execute
113 * @return $isValid Whether the given controller is valid
114 * @throws EmptyVariableException Thrown if given controller is not set
115 * @throws DefaultControllerException Thrown if default controller was not found
117 public function isControllerValid ($controllerName) {
118 // By default nothing shall be valid
121 // Is a controller set?
122 if (empty($controllerName)) {
123 // Then thrown an exception here
124 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
128 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) . 'Controller';
130 // Now, let us create the full name of the controller class
131 $this->setClassName($className);
133 // Try it hard to get an controller
134 while ($isValid === FALSE) {
135 // Is this class already loaded?
136 if (class_exists($this->getClassName())) {
137 // This class does exist. :-)
139 } elseif ($this->getClassName() != $this->getCapitalizedClassPrefix() . 'DefaultNewsController') {
140 // Set default controller
141 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
143 // All is tried, give it up here
144 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
153 * Resolves the default controller of the given controller
155 * @return $controllerInstance A controller instance for the default
157 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
160 public function resolveController () {
162 $controllerName = '';
163 $controllerInstance = NULL;
165 // Get the controller name
166 $controllerName = $this->getControllerName();
168 // Get the controller
169 $controllerInstance = $this->loadController($controllerName);
172 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
173 // This controller has an invalid instance!
174 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
177 // Set last controller
178 $this->setResolvedInstance($controllerInstance);
180 // Return the maybe resolved instance
181 return $controllerInstance;