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\Object\ObjectFactory;
11 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
12 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
13 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
14 use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver;
15 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
18 use \InvalidArgumentException;
21 * A generic controller resolver class
23 * @author Roland Haeder <webmaster@shipsimu.org>
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
26 * @license GNU GPL 3.0 or any newer version
27 * @link http://www.shipsimu.org
29 * This program is free software: you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation, either version 3 of the License, or
32 * (at your option) any later version.
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public License
40 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42 abstract class BaseControllerResolver extends BaseResolver {
46 private $controllerName = '';
49 * Protected constructor
51 * @param $className Name of the real class
54 protected function __construct (string $className) {
55 // Call parent constructor
56 parent::__construct($className);
60 * Setter for controller name
62 * @param $controllerName Last validated controller name
65 protected final function setControllerName (string $controllerName) {
66 $this->controllerName = $controllerName;
70 * Getter for controller name
72 * @return $controllerName Last validated controller name
74 protected final function getControllerName () {
75 return $this->controllerName;
79 * "Loads" a given controller and instances it if not yet cached. If the
80 * controller was not found one of the default controllers will be used
81 * depending on whether news shall be displayed.
83 * @param $controllerName A controller name we shall look for
84 * @return $controllerInstance A loaded controller instance
85 * @throws InvalidControllerException Thrown if even the requested
86 * controller class is missing (bad!)
88 protected function loadController (string $controllerName) {
89 // Cache default controller
90 $defaultController = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
92 // Create full class name
94 '%s\%sDefaultNewsController',
95 $this->getNamespace(),
96 $this->getCapitalizedClassPrefix()
100 $this->setClassName($className);
102 // Generate the class name
103 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
104 if ($controllerName != $defaultController) {
105 // Create controller class name
106 $className = sprintf(
108 $this->getNamespace(),
109 $this->getCapitalizedClassPrefix(),
110 StringUtils::convertToClassName($controllerName)
114 $this->setClassName($className);
116 // No news at main controller or non-news controller
117 $this->setClassName($className);
120 // Is this class loaded?
121 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
122 if (!class_exists($this->getClassName())) {
123 // Throw an exception here
124 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
127 // Try to read a config entry for our resolver including controller name... ;-)
128 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
130 // Get the config, this will throw an exception if there is no special controller resolver
131 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONTROLLER-RESOLVER: resolverConfigEntry=%s', $resolverConfigEntry));
132 $resolverClass = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($resolverConfigEntry);
134 // Initiate the resolver and controller
135 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
136 $resolverConfigEntry,
139 ApplicationHelper::getSelfInstance()
143 // Get controller instance
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 (string $namespace, string $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(),
180 StringUtils::convertToClassName($controllerName)
183 // Application's default news controller
184 $appDefaultControllerName = sprintf(
185 '%s\%sDefaultNewsController',
187 $this->getCapitalizedClassPrefix()
190 // Framework's default news controller
191 $defaultControllerName = sprintf(
192 'Org\Mxchange\CoreFramework\Controller\News\%sDefaultNewsController',
193 $this->getCapitalizedClassPrefix()
196 // Now, let us create the full name of the controller class
197 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
198 $this->setClassName($className);
200 // Try it hard to get an controller
201 while ($isValid === false) {
202 // Is this class already loaded?
203 if (class_exists($this->getClassName())) {
204 // This class does exist. :-)
206 } elseif ($this->getClassName() != $appDefaultControllerName) {
207 // Set application's default controller
208 $this->setClassName($appDefaultControllerName);
209 } elseif ($this->getClassName() != $defaultControllerName) {
210 // Set framework's default controller
211 $this->setClassName($defaultControllerName);
213 // All is tried, give it up here
214 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
223 * Resolves the default controller of the given controller
225 * @return $controllerInstance A controller instance for the default
227 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
230 public function resolveController () {
232 $controllerName = $this->getControllerName();
234 // Get the controller
235 $controllerInstance = $this->loadController($controllerName);
238 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
239 // This controller has an invalid instance!
240 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
243 // Set last controller
244 $this->setResolvedInstance($controllerInstance);
246 // Return the maybe resolved instance
247 return $controllerInstance;