3 namespace Org\Mxchange\CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Controller\Controller;
7 use Org\Mxchange\CoreFramework\Controller\DefaultControllerException;
8 use Org\Mxchange\CoreFramework\Controller\InvalidControllerException;
9 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
11 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
12 use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver;
15 use \InvalidArgumentException;
18 * A generic controller resolver class
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 abstract class BaseControllerResolver extends BaseResolver {
41 * Protected constructor
43 * @param $className Name of the real class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
52 * "Loads" a given controller and instances it if not yet cached. If the
53 * controller was not found one of the default controllers will be used
54 * depending on whether news shall be displayed.
56 * @param $controllerName A controller name we shall look for
57 * @return $controllerInstance A loaded controller instance
58 * @throws InvalidControllerException Thrown if even the requested
59 * controller class is missing (bad!)
61 protected function loadController ($controllerName) {
62 // Cache default controller
63 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
65 // Init controller instance
66 $controllerInstance = NULL;
68 // Create full class name
70 '%s\%sDefaultNewsController',
71 $this->getNamespace(),
72 $this->getCapitalizedClassPrefix()
76 $this->setClassName($className);
78 // Generate the class name
79 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
80 if ($controllerName != $defaultController) {
81 // Create controller class name
84 $this->getNamespace(),
85 $this->getCapitalizedClassPrefix(),
86 self::convertToClassName($controllerName)
90 $this->setClassName($className);
92 // No news at main controller or non-news controller
93 $this->setClassName($className);
95 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
97 // Is this class loaded?
98 if (!class_exists($this->getClassName())) {
99 // Throw an exception here
100 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
103 // Set default resolver config name
104 $resolverConfigEntry = '';
106 // Try to read a config entry for our resolver including controller name... ;-)
107 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
109 // Get the config, this will throw an exception if there is no special controller resolver
110 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
112 // Initiate the resolver and controller
113 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
114 $resolverConfigEntry,
117 GenericRegistry::getRegistry()->getInstance('application')
120 $controllerInstance = ObjectFactory::createObjectByName(
121 $this->getClassName(),
122 array($resolverInstance)
126 return $controllerInstance;
130 * Checks whether the given controller is valid
132 * @param $namespace Namespace to look in, no trailing backslash
133 * @param $controllerName The default controller we shall execute
134 * @return $isValid Whether the given controller is valid
135 * @throws InvalidArgumentException Thrown if given controller is not set
136 * @throws DefaultControllerException Thrown if default controller was not found
138 protected function isControllerValid ($namespace, $controllerName) {
139 // By default nothing shall be valid
142 // Is namespace and controller name set?
143 if (empty($namespace)) {
144 // Then thrown an exception here
145 throw new InvalidArgumentException('Parameter "namespace" is empty');
146 } elseif (empty($controllerName)) {
147 // Then thrown an exception here
148 throw new InvalidArgumentException('Parameter "controllerName" is empty');
152 $className = sprintf(
155 $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
157 $newsControllerName = sprintf(
158 '%s\%sDefaultNewsController',
160 $this->getCapitalizedClassPrefix()
164 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
166 // Now, let us create the full name of the controller class
167 $this->setClassName($className);
169 // Try it hard to get an controller
170 while ($isValid === false) {
171 // Is this class already loaded?
172 if (class_exists($this->getClassName())) {
173 // This class does exist. :-)
175 } elseif ($this->getClassName() != $newsControllerName) {
176 // Set default controller
177 $this->setClassName($newsControllerName);
179 // All is tried, give it up here
180 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
189 * Resolves the default controller of the given controller
191 * @return $controllerInstance A controller instance for the default
193 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
196 public function resolveController () {
198 $controllerName = '';
199 $controllerInstance = NULL;
201 // Get namespace and controller name
202 $controllerName = $this->getControllerName();
204 // Get the controller
205 $controllerInstance = $this->loadController($controllerName);
208 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
209 // This controller has an invalid instance!
210 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
213 // Set last controller
214 $this->setResolvedInstance($controllerInstance);
216 // Return the maybe resolved instance
217 return $controllerInstance;