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;
16 use \InvalidArgumentException;
19 * A generic controller resolver class
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 abstract class BaseControllerResolver extends BaseResolver {
44 private $controllerName = '';
47 * Protected constructor
49 * @param $className Name of the real class
52 protected function __construct ($className) {
53 // Call parent constructor
54 parent::__construct($className);
58 * Setter for controller name
60 * @param $controllerName Last validated controller name
63 protected final function setControllerName (string $controllerName) {
64 $this->controllerName = $controllerName;
68 * Getter for controller name
70 * @return $controllerName Last validated controller name
72 protected final function getControllerName () {
73 return $this->controllerName;
77 * "Loads" a given controller and instances it if not yet cached. If the
78 * controller was not found one of the default controllers will be used
79 * depending on whether news shall be displayed.
81 * @param $controllerName A controller name we shall look for
82 * @return $controllerInstance A loaded controller instance
83 * @throws InvalidControllerException Thrown if even the requested
84 * controller class is missing (bad!)
86 protected function loadController ($controllerName) {
87 // Cache default controller
88 $defaultController = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
90 // Init controller instance
91 $controllerInstance = NULL;
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 self::convertToClassName($controllerName)
115 $this->setClassName($className);
117 // No news at main controller or non-news controller
118 $this->setClassName($className);
120 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
122 // Is this class loaded?
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 GenericRegistry::getRegistry()->getInstance('application')
143 $controllerInstance = ObjectFactory::createObjectByName(
144 $this->getClassName(),
145 array($resolverInstance)
149 return $controllerInstance;
153 * Checks whether the given controller is valid
155 * @param $namespace Namespace to look in, no trailing backslash
156 * @param $controllerName The default controller we shall execute
157 * @return $isValid Whether the given controller is valid
158 * @throws InvalidArgumentException Thrown if given controller is not set
159 * @throws DefaultControllerException Thrown if default controller was not found
161 protected function isControllerValid ($namespace, $controllerName) {
163 if (empty($namespace)) {
164 // Then thrown an exception here
165 throw new InvalidArgumentException('Parameter "namespace" is empty');
166 } elseif (empty($controllerName)) {
167 // Then thrown an exception here
168 throw new InvalidArgumentException('Parameter "controllerName" is empty');
171 // By default nothing shall be valid
175 $className = sprintf(
178 $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
180 $newsControllerName = sprintf(
181 '%s\%sDefaultNewsController',
183 $this->getCapitalizedClassPrefix()
187 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
189 // Now, let us create the full name of the controller class
190 $this->setClassName($className);
192 // Try it hard to get an controller
193 while ($isValid === false) {
194 // Is this class already loaded?
195 if (class_exists($this->getClassName())) {
196 // This class does exist. :-)
198 } elseif ($this->getClassName() != $newsControllerName) {
199 // Set default controller
200 $this->setClassName($newsControllerName);
202 // All is tried, give it up here
203 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
212 * Resolves the default controller of the given controller
214 * @return $controllerInstance A controller instance for the default
216 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
219 public function resolveController () {
221 $controllerName = '';
222 $controllerInstance = NULL;
224 // Get namespace and controller name
225 $controllerName = $this->getControllerName();
227 // Get the controller
228 $controllerInstance = $this->loadController($controllerName);
231 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
232 // This controller has an invalid instance!
233 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
236 // Set last controller
237 $this->setResolvedInstance($controllerInstance);
239 // Return the maybe resolved instance
240 return $controllerInstance;