3 namespace Org\Mxchange\CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Controller\DefaultControllerException;
7 use Org\Mxchange\CoreFramework\Controller\Controller;
8 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Registry\Registry;
10 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
11 use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver;
14 use \InvalidArgumentException;
17 * A generic controller resolver class
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 <<<<<<< HEAD:framework/main/classes/resolver/controller/class_BaseControllerResolver.php
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
25 >>>>>>> Some updates::inc/main/classes/resolver/controller/class_BaseControllerResolver.php
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 {
44 * Protected constructor
46 * @param $className Name of the real class
49 protected function __construct ($className) {
50 // Call parent constructor
51 parent::__construct($className);
55 * "Loads" a given controller and instances it if not yet cached. If the
56 * controller was not found one of the default controllers will be used
57 * depending on whether news shall be displayed.
59 * @param $controllerName A controller name we shall look for
60 * @return $controllerInstance A loaded controller instance
61 * @throws InvalidControllerException Thrown if even the requested
62 * controller class is missing (bad!)
64 protected function loadController ($controllerName) {
65 // Cache default controller
66 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
68 // Init controller instance
69 $controllerInstance = NULL;
71 // Create full class name
73 '%s\%sDefaultNewsController',
74 $this->getNamespace(),
75 $this->getCapitalizedClassPrefix()
79 $this->setClassName($className);
81 // Generate the class name
82 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
83 if ($controllerName != $defaultController) {
84 // Create controller class name
87 $this->getNamespace(),
88 $this->getCapitalizedClassPrefix(),
89 self::convertToClassName($controllerName)
93 $this->setClassName($className);
95 // No news at main controller or non-news controller
96 $this->setClassName($className);
98 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
100 // Is this class loaded?
101 if (!class_exists($this->getClassName())) {
102 // Throw an exception here
103 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
106 // Set default resolver config name
107 $resolverConfigEntry = '';
109 // Try to read a config entry for our resolver including controller name... ;-)
110 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
112 // Get the config, this will throw an exception if there is no special controller resolver
113 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
115 // Initiate the resolver and controller
116 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
117 $resolverConfigEntry,
120 Registry::getRegistry()->getInstance('app')
123 $controllerInstance = ObjectFactory::createObjectByName(
124 $this->getClassName(),
125 array($resolverInstance)
129 return $controllerInstance;
133 * Checks whether the given controller is valid
135 * @param $namespace Namespace to look in, no trailing backslash
136 * @param $controllerName The default controller we shall execute
137 * @return $isValid Whether the given controller is valid
138 * @throws InvalidArgumentException Thrown if given controller is not set
139 * @throws DefaultControllerException Thrown if default controller was not found
141 protected function isControllerValid ($namespace, $controllerName) {
142 // By default nothing shall be valid
145 // Is namespace and controller name set?
146 if (empty($namespace)) {
147 // Then thrown an exception here
148 throw new InvalidArgumentException('Parameter "namespace" is empty');
149 } elseif (empty($controllerName)) {
150 // Then thrown an exception here
151 throw new InvalidArgumentException('Parameter "controllerName" is empty');
155 $className = sprintf(
158 $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
160 $newsControllerName = sprintf(
161 '%s\%sDefaultNewsController',
163 $this->getCapitalizedClassPrefix()
167 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
169 // Now, let us create the full name of the controller class
170 $this->setClassName($className);
172 // Try it hard to get an controller
173 while ($isValid === false) {
174 // Is this class already loaded?
175 if (class_exists($this->getClassName())) {
176 // This class does exist. :-)
178 } elseif ($this->getClassName() != $newsControllerName) {
179 // Set default controller
180 $this->setClassName($newsControllerName);
182 // All is tried, give it up here
183 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
192 * Resolves the default controller of the given controller
194 * @return $controllerInstance A controller instance for the default
196 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
199 public function resolveController () {
201 $controllerName = '';
202 $controllerInstance = NULL;
204 // Get namespace and controller name
205 $controllerName = $this->getControllerName();
207 // Get the controller
208 $controllerInstance = $this->loadController($controllerName);
211 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
212 // This controller has an invalid instance!
213 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
216 // Set last controller
217 $this->setResolvedInstance($controllerInstance);
219 // Return the maybe resolved instance
220 return $controllerInstance;