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\Generic\FrameworkInterface;
12 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
13 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
14 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
15 use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver;
16 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
19 use \InvalidArgumentException;
22 * A generic controller resolver class
24 * @author Roland Haeder <webmaster@shipsimu.org>
26 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 abstract class BaseControllerResolver extends BaseResolver {
47 private $controllerName = '';
50 * Protected constructor
52 * @param $className Name of the real class
55 protected function __construct (string $className) {
56 // Call parent constructor
57 parent::__construct($className);
61 * Setter for controller name
63 * @param $controllerName Last validated controller name
66 protected final function setControllerName (string $controllerName) {
67 $this->controllerName = $controllerName;
71 * Getter for controller name
73 * @return $controllerName Last validated controller name
75 protected final function getControllerName () {
76 return $this->controllerName;
80 * "Loads" a given controller and instances it if not yet cached. If the
81 * controller was not found one of the default controllers will be used
82 * depending on whether news shall be displayed.
84 * @param $controllerName A controller name we shall look for
85 * @return $controllerInstance A loaded controller instance
86 * @throws InvalidControllerException Thrown if even the requested
87 * controller class is missing (bad!)
89 protected function loadController (string $controllerName) {
90 // Cache default controller
91 $defaultController = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
93 // Create full class name
95 '%s\%sDefaultNewsController',
96 $this->getNamespace(),
97 $this->getCapitalizedClassPrefix()
100 // Default controller
101 $this->setClassName($className);
103 // Generate the class name
104 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
105 if ($controllerName != $defaultController) {
106 // Create controller class name
107 $className = sprintf(
109 $this->getNamespace(),
110 $this->getCapitalizedClassPrefix(),
111 StringUtils::convertToClassName($controllerName)
115 $this->setClassName($className);
117 // No news at main controller or non-news controller
118 $this->setClassName($className);
121 // Is this class loaded?
122 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
123 if (!class_exists($this->getClassName())) {
124 // Throw an exception here
125 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
128 // Try to read a config entry for our resolver including controller name... ;-)
129 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
131 // Get the config, this will throw an exception if there is no special controller resolver
132 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONTROLLER-RESOLVER: resolverConfigEntry=%s', $resolverConfigEntry));
133 $resolverClass = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($resolverConfigEntry);
135 // Initiate the resolver and controller
136 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
137 $resolverConfigEntry,
140 ApplicationHelper::getSelfInstance()
144 // Get controller instance
145 $controllerInstance = ObjectFactory::createObjectByName(
146 $this->getClassName(),
147 array($resolverInstance)
151 return $controllerInstance;
155 * Checks whether the given controller is valid
157 * @param $namespace Namespace to look in, no trailing backslash
158 * @param $controllerName The default controller we shall execute
159 * @return $isValid Whether the given controller is valid
160 * @throws InvalidArgumentException Thrown if given controller is not set
161 * @throws DefaultControllerException Thrown if default controller was not found
163 protected function isControllerValid (string $namespace, string $controllerName) {
165 if (empty($namespace)) {
166 // Then thrown an exception here
167 throw new InvalidArgumentException('Parameter "namespace" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
168 } elseif (empty($controllerName)) {
169 // Then thrown an exception here
170 throw new InvalidArgumentException('Parameter "controllerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
173 // By default nothing shall be valid
177 $className = sprintf(
180 $this->getCapitalizedClassPrefix(),
181 StringUtils::convertToClassName($controllerName)
184 // Application's default news controller
185 $appDefaultControllerName = sprintf(
186 '%s\%sDefaultNewsController',
188 $this->getCapitalizedClassPrefix()
191 // Framework's default news controller
192 $defaultControllerName = sprintf(
193 'Org\Mxchange\CoreFramework\Controller\News\%sDefaultNewsController',
194 $this->getCapitalizedClassPrefix()
197 // Now, let us create the full name of the controller class
198 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
199 $this->setClassName($className);
201 // Try it hard to get an controller
202 while ($isValid === false) {
203 // Is this class already loaded?
204 if (class_exists($this->getClassName())) {
205 // This class does exist. :-)
207 } elseif ($this->getClassName() != $appDefaultControllerName) {
208 // Set application's default controller
209 $this->setClassName($appDefaultControllerName);
210 } elseif ($this->getClassName() != $defaultControllerName) {
211 // Set framework's default controller
212 $this->setClassName($defaultControllerName);
214 // All is tried, give it up here
215 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
224 * Resolves the default controller of the given controller
226 * @return $controllerInstance A controller instance for the default
228 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
231 public function resolveController () {
233 $controllerName = $this->getControllerName();
235 // Get the controller
236 $controllerInstance = $this->loadController($controllerName);
239 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
240 // This controller has an invalid instance!
241 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
244 // Set last controller
245 $this->setResolvedInstance($controllerInstance);
247 // Return the maybe resolved instance
248 return $controllerInstance;