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\Generic\EmptyVariableException;
10 use CoreFramework\Resolver\BaseResolver;
11 use CoreFramework\Resolver\Controller\ControllerResolver;
13 * A generic controller resolver class
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class BaseControllerResolver extends BaseResolver {
36 * Protected constructor
38 * @param $className Name of the real class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
47 * "Loads" a given controller and instances it if not yet cached. If the
48 * controller was not found one of the default controllers will be used
49 * depending on whether news shall be displayed.
51 * @param $controllerName A controller name we shall look for
52 * @return $controllerInstance A loaded controller instance
53 * @throws InvalidControllerException Thrown if even the requested
54 * controller class is missing (bad!)
56 protected function loadController ($controllerName) {
57 // Cache default controller
58 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
60 // Init controller instance
61 $controllerInstance = NULL;
63 // Create full class name
65 '%s\%sDefaultNewsController',
66 $this->getNamespace(),
67 $this->getCapitalizedClassPrefix()
71 $this->setClassName($className);
73 // Generate the class name
74 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
75 if ($controllerName != $defaultController) {
76 // Create controller class name
80 $this->getCapitalizedClassPrefix(),
81 self::convertToClassName($controllerName)
85 $this->setClassName($className);
87 // No news at main controller or non-news controller
88 $this->setClassName($className);
90 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
92 // Is this class loaded?
93 if (!class_exists($this->getClassName())) {
94 // Throw an exception here
95 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
98 // Set default resolver config name
99 $resolverConfigEntry = '';
101 // Try to read a config entry for our resolver including controller name... ;-)
102 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
104 // Get the config, this will throw an exception if there is no special controller resolver
105 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
107 // Initiate the resolver and controller
108 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
109 $resolverConfigEntry,
112 $this->getApplicationInstance()
115 $controllerInstance = ObjectFactory::createObjectByName(
116 $this->getClassName(),
117 array($resolverInstance)
121 return $controllerInstance;
125 * Checks whether the given controller is valid
127 * @param $namespace Namespace to look in, no trailing backslash
128 * @param $controllerName The default controller we shall execute
129 * @return $isValid Whether the given controller is valid
130 * @throws EmptyVariableException Thrown if given controller is not set
131 * @throws DefaultControllerException Thrown if default controller was not found
133 protected function isControllerValid ($namespace, $controllerName) {
134 // By default nothing shall be valid
137 // Is namespace and controller name set?
138 if (empty($namespace)) {
139 // Then thrown an exception here
140 throw new EmptyVariableException(array($this, 'namespace'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
141 } elseif (empty($controllerName)) {
142 // Then thrown an exception here
143 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
147 $className = sprintf(
150 $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
152 $newsControllerName = sprintf(
153 '%s\%sDefaultNewsController',
155 $this->getCapitalizedClassPrefix()
159 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
161 // Now, let us create the full name of the controller class
162 $this->setClassName($className);
164 // Try it hard to get an controller
165 while ($isValid === FALSE) {
166 // Is this class already loaded?
167 if (class_exists($this->getClassName())) {
168 // This class does exist. :-)
170 } elseif ($this->getClassName() != $newsControllerName) {
171 // Set default controller
172 $this->setClassName($newsControllerName);
174 // All is tried, give it up here
175 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
184 * Resolves the default controller of the given controller
186 * @return $controllerInstance A controller instance for the default
188 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
191 public function resolveController () {
193 $controllerName = '';
194 $controllerInstance = NULL;
196 // Get namespace and controller name
197 $controllerName = $this->getControllerName();
199 // Get the controller
200 $controllerInstance = $this->loadController($controllerName);
203 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
204 // This controller has an invalid instance!
205 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
208 // Set last controller
209 $this->setResolvedInstance($controllerInstance);
211 // Return the maybe resolved instance
212 return $controllerInstance;