3 namespace Org\Mxchange\CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Controller\Controller;
8 use Org\Mxchange\CoreFramework\Controller\DefaultControllerException;
9 use Org\Mxchange\CoreFramework\Controller\InvalidControllerException;
10 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
12 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
13 use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver;
14 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
17 use \InvalidArgumentException;
20 * A generic controller resolver class
22 * @author Roland Haeder <webmaster@shipsimu.org>
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 abstract class BaseControllerResolver extends BaseResolver {
45 private $controllerName = '';
48 * Protected constructor
50 * @param $className Name of the real class
53 protected function __construct (string $className) {
54 // Call parent constructor
55 parent::__construct($className);
59 * Setter for controller name
61 * @param $controllerName Last validated controller name
64 protected final function setControllerName (string $controllerName) {
65 $this->controllerName = $controllerName;
69 * Getter for controller name
71 * @return $controllerName Last validated controller name
73 protected final function getControllerName () {
74 return $this->controllerName;
78 * "Loads" a given controller and instances it if not yet cached. If the
79 * controller was not found one of the default controllers will be used
80 * depending on whether news shall be displayed.
82 * @param $controllerName A controller name we shall look for
83 * @return $controllerInstance A loaded controller instance
84 * @throws InvalidControllerException Thrown if even the requested
85 * controller class is missing (bad!)
87 protected function loadController (string $controllerName) {
88 // Cache default controller
89 $defaultController = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
91 // Init controller instance
92 $controllerInstance = NULL;
94 // Create full class name
96 '%s\%sDefaultNewsController',
97 $this->getNamespace(),
98 $this->getCapitalizedClassPrefix()
101 // Default controller
102 $this->setClassName($className);
104 // Generate the class name
105 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
106 if ($controllerName != $defaultController) {
107 // Create controller class name
108 $className = sprintf(
110 $this->getNamespace(),
111 $this->getCapitalizedClassPrefix(),
112 StringUtils::convertToClassName($controllerName)
116 $this->setClassName($className);
118 // No news at main controller or non-news controller
119 $this->setClassName($className);
121 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
123 // Is this class loaded?
124 if (!class_exists($this->getClassName())) {
125 // Throw an exception here
126 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
129 // Try to read a config entry for our resolver including controller name... ;-)
130 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
132 // Get the config, this will throw an exception if there is no special controller resolver
133 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONTROLLER-RESOLVER: resolverConfigEntry=%s', $resolverConfigEntry));
134 $resolverClass = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($resolverConfigEntry);
136 // Initiate the resolver and controller
137 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
138 $resolverConfigEntry,
141 GenericRegistry::getRegistry()->getInstance('application')
144 $controllerInstance = ObjectFactory::createObjectByName(
145 $this->getClassName(),
146 array($resolverInstance)
150 return $controllerInstance;
154 * Checks whether the given controller is valid
156 * @param $namespace Namespace to look in, no trailing backslash
157 * @param $controllerName The default controller we shall execute
158 * @return $isValid Whether the given controller is valid
159 * @throws InvalidArgumentException Thrown if given controller is not set
160 * @throws DefaultControllerException Thrown if default controller was not found
162 protected function isControllerValid ($namespace, $controllerName) {
164 if (empty($namespace)) {
165 // Then thrown an exception here
166 throw new InvalidArgumentException('Parameter "namespace" is empty');
167 } elseif (empty($controllerName)) {
168 // Then thrown an exception here
169 throw new InvalidArgumentException('Parameter "controllerName" is empty');
172 // By default nothing shall be valid
176 $className = sprintf(
179 $this->getCapitalizedClassPrefix() . StringUtils::convertToClassName($controllerName)
181 $newsControllerName = sprintf(
182 '%s\%sDefaultNewsController',
184 $this->getCapitalizedClassPrefix()
188 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
190 // Now, let us create the full name of the controller class
191 $this->setClassName($className);
193 // Try it hard to get an controller
194 while ($isValid === false) {
195 // Is this class already loaded?
196 if (class_exists($this->getClassName())) {
197 // This class does exist. :-)
199 } elseif ($this->getClassName() != $newsControllerName) {
200 // Set default controller
201 $this->setClassName($newsControllerName);
203 // All is tried, give it up here
204 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
213 * Resolves the default controller of the given controller
215 * @return $controllerInstance A controller instance for the default
217 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
220 public function resolveController () {
222 $controllerName = $this->getControllerName();
223 $controllerInstance = NULL;
225 // Get the controller
226 $controllerInstance = $this->loadController($controllerName);
229 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
230 // This controller has an invalid instance!
231 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
234 // Set last controller
235 $this->setResolvedInstance($controllerInstance);
237 // Return the maybe resolved instance
238 return $controllerInstance;