3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Controller\DefaultControllerException;
7 use CoreFramework\Controller\Controller;
8 use CoreFramework\Factory\ObjectFactory;
9 use CoreFramework\Registry\Registry;
10 use CoreFramework\Resolver\BaseResolver;
11 use CoreFramework\Resolver\Controller\ControllerResolver;
14 use \InvalidArgumentException;
17 * A generic controller resolver class
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class BaseControllerResolver extends BaseResolver {
40 * Protected constructor
42 * @param $className Name of the real class
45 protected function __construct ($className) {
46 // Call parent constructor
47 parent::__construct($className);
51 * "Loads" a given controller and instances it if not yet cached. If the
52 * controller was not found one of the default controllers will be used
53 * depending on whether news shall be displayed.
55 * @param $controllerName A controller name we shall look for
56 * @return $controllerInstance A loaded controller instance
57 * @throws InvalidControllerException Thrown if even the requested
58 * controller class is missing (bad!)
60 protected function loadController ($controllerName) {
61 // Cache default controller
62 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
64 // Init controller instance
65 $controllerInstance = NULL;
67 // Create full class name
69 '%s\%sDefaultNewsController',
70 $this->getNamespace(),
71 $this->getCapitalizedClassPrefix()
75 $this->setClassName($className);
77 // Generate the class name
78 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
79 if ($controllerName != $defaultController) {
80 // Create controller class name
84 $this->getCapitalizedClassPrefix(),
85 self::convertToClassName($controllerName)
89 $this->setClassName($className);
91 // No news at main controller or non-news controller
92 $this->setClassName($className);
94 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
96 // Is this class loaded?
97 if (!class_exists($this->getClassName())) {
98 // Throw an exception here
99 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
102 // Set default resolver config name
103 $resolverConfigEntry = '';
105 // Try to read a config entry for our resolver including controller name... ;-)
106 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
108 // Get the config, this will throw an exception if there is no special controller resolver
109 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
111 // Initiate the resolver and controller
112 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
113 $resolverConfigEntry,
116 Registry::getRegistry()->getInstance('app')
119 $controllerInstance = ObjectFactory::createObjectByName(
120 $this->getClassName(),
121 array($resolverInstance)
125 return $controllerInstance;
129 * Checks whether the given controller is valid
131 * @param $namespace Namespace to look in, no trailing backslash
132 * @param $controllerName The default controller we shall execute
133 * @return $isValid Whether the given controller is valid
134 * @throws InvalidArgumentException Thrown if given controller is not set
135 * @throws DefaultControllerException Thrown if default controller was not found
137 protected function isControllerValid ($namespace, $controllerName) {
138 // By default nothing shall be valid
141 // Is namespace and controller name set?
142 if (empty($namespace)) {
143 // Then thrown an exception here
144 throw new InvalidArgumentException('Parameter "namespace" is empty');
145 } elseif (empty($controllerName)) {
146 // Then thrown an exception here
147 throw new InvalidArgumentException('Parameter "controllerName" is empty');
151 $className = sprintf(
154 $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
156 $newsControllerName = sprintf(
157 '%s\%sDefaultNewsController',
159 $this->getCapitalizedClassPrefix()
163 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
165 // Now, let us create the full name of the controller class
166 $this->setClassName($className);
168 // Try it hard to get an controller
169 while ($isValid === false) {
170 // Is this class already loaded?
171 if (class_exists($this->getClassName())) {
172 // This class does exist. :-)
174 } elseif ($this->getClassName() != $newsControllerName) {
175 // Set default controller
176 $this->setClassName($newsControllerName);
178 // All is tried, give it up here
179 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
188 * Resolves the default controller of the given controller
190 * @return $controllerInstance A controller instance for the default
192 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
195 public function resolveController () {
197 $controllerName = '';
198 $controllerInstance = NULL;
200 // Get namespace and controller name
201 $controllerName = $this->getControllerName();
203 // Get the controller
204 $controllerInstance = $this->loadController($controllerName);
207 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
208 // This controller has an invalid instance!
209 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
212 // Set last controller
213 $this->setResolvedInstance($controllerInstance);
215 // Return the maybe resolved instance
216 return $controllerInstance;