3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
9 * A generic controller resolver class
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseControllerResolver extends BaseResolver {
32 * Protected constructor
34 * @param $className Name of the real class
37 protected function __construct ($className) {
38 // Call parent constructor
39 parent::__construct($className);
43 * "Loads" a given controller and instances it if not yet cached. If the
44 * controller was not found one of the default controllers will be used
45 * depending on whether news shall be displayed.
47 * @param $controllerName A controller name we shall look for
48 * @return $controllerInstance A loaded controller instance
49 * @throws InvalidControllerException Thrown if even the requested
50 * controller class is missing (bad!)
52 protected function loadController ($controllerName) {
53 // Cache default controller
54 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
56 // Init controller instance
57 $controllerInstance = NULL;
60 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
62 // Generate the class name
63 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
64 if ($controllerName != $defaultController) {
65 // Create controller class name
66 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) . 'Controller';
69 $this->setClassName($className);
71 // No news at main controller or non-news controller
72 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
74 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
76 // Is this class loaded?
77 if (!class_exists($this->getClassName())) {
78 // Throw an exception here
79 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
82 // Set default resolver config name
83 $resolverConfigEntry = '';
85 // Try to read a config entry for our resolver including controller name... ;-)
86 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
88 // Get the config, this will throw an exception if there is no special controller resolver
89 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
91 // Initiate the resolver and controller
92 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
96 $this->getApplicationInstance()
99 $controllerInstance = ObjectFactory::createObjectByName(
100 $this->getClassName(),
101 array($resolverInstance)
105 return $controllerInstance;
109 * Checks whether the given controller is valid
111 * @param $controllerName The default controller we shall execute
112 * @return $isValid Whether the given controller is valid
113 * @throws EmptyVariableException Thrown if given controller is not set
114 * @throws DefaultControllerException Thrown if default controller was not found
116 public function isControllerValid ($controllerName) {
117 // By default nothing shall be valid
120 // Is a controller set?
121 if (empty($controllerName)) {
122 // Then thrown an exception here
123 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
127 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) . 'Controller';
129 // Now, let us create the full name of the controller class
130 $this->setClassName($className);
132 // Try it hard to get an controller
133 while ($isValid === FALSE) {
134 // Is this class already loaded?
135 if (class_exists($this->getClassName())) {
136 // This class does exist. :-)
138 } elseif ($this->getClassName() != $this->getCapitalizedClassPrefix() . 'DefaultNewsController') {
139 // Set default controller
140 $this->setClassName($this->getCapitalizedClassPrefix() . 'DefaultNewsController');
142 // All is tried, give it up here
143 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
152 * Resolves the default controller of the given controller
154 * @return $controllerInstance A controller instance for the default
156 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
159 public function resolveController () {
161 $controllerName = '';
162 $controllerInstance = NULL;
164 // Get the controller name
165 $controllerName = $this->getControllerName();
167 // Get the controller
168 $controllerInstance = $this->loadController($controllerName);
171 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
172 // This controller has an invalid instance!
173 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
176 // Set last controller
177 $this->setResolvedInstance($controllerInstance);
179 // Return the maybe resolved instance
180 return $controllerInstance;