3 namespace CoreFramework\Resolver\Controller;
5 // Import framework stuff
6 use CoreFramework\Controller\DefaultControllerException;
7 use CoreFramework\Controller\Generic\Controller;
8 use CoreFramework\Factory\ObjectFactory;
9 use CoreFramework\Resolver\BaseResolver;
10 use CoreFramework\Resolver\Controller\ControllerResolver;
12 * A generic controller resolver class
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class BaseControllerResolver extends BaseResolver {
35 * Protected constructor
37 * @param $className Name of the real class
40 protected function __construct ($className) {
41 // Call parent constructor
42 parent::__construct($className);
46 * "Loads" a given controller and instances it if not yet cached. If the
47 * controller was not found one of the default controllers will be used
48 * depending on whether news shall be displayed.
50 * @param $controllerName A controller name we shall look for
51 * @return $controllerInstance A loaded controller instance
52 * @throws InvalidControllerException Thrown if even the requested
53 * controller class is missing (bad!)
55 protected function loadController ($controllerName) {
56 // Cache default controller
57 $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
59 // Init controller instance
60 $controllerInstance = NULL;
62 // Create full class name
64 '%s\%sDefaultNewsController',
65 $this->getNamespace(),
66 $this->getCapitalizedClassPrefix()
70 $this->setClassName($className);
72 // Generate the class name
73 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BEFORE: controller=' . $controllerName);
74 if ($controllerName != $defaultController) {
75 // Create controller class name
76 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName) . 'Controller';
79 $this->setClassName($className);
81 // No news at main controller or non-news controller
82 $this->setClassName($className);
84 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
86 // Is this class loaded?
87 if (!class_exists($this->getClassName())) {
88 // Throw an exception here
89 throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
92 // Set default resolver config name
93 $resolverConfigEntry = '';
95 // Try to read a config entry for our resolver including controller name... ;-)
96 $resolverConfigEntry = sprintf('%s_cmd_%s_resolver_class', strtolower($this->getClassPrefix()), strtolower($controllerName));
98 // Get the config, this will throw an exception if there is no special controller resolver
99 $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
101 // Initiate the resolver and controller
102 $resolverInstance = ObjectFactory::createObjectByConfiguredName(
103 $resolverConfigEntry,
106 $this->getApplicationInstance()
109 $controllerInstance = ObjectFactory::createObjectByName(
110 $this->getClassName(),
111 array($resolverInstance)
115 return $controllerInstance;
119 * Checks whether the given controller is valid
121 * @param $namespace Namespace to look in, no trailing backslash
122 * @param $controllerName The default controller we shall execute
123 * @return $isValid Whether the given controller is valid
124 * @throws EmptyVariableException Thrown if given controller is not set
125 * @throws DefaultControllerException Thrown if default controller was not found
127 public function isControllerValid ($namespace, $controllerName) {
128 // By default nothing shall be valid
131 // Is namespace and controller name set?
132 if (empty($namespace)) {
133 // Then thrown an exception here
134 throw new EmptyVariableException(array($this, 'namespace'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
135 } elseif (empty($controllerName)) {
136 // Then thrown an exception here
137 throw new EmptyVariableException(array($this, 'controllerName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
141 $className = sprintf(
144 $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
146 $newsControllerName = sprintf(
147 '%s\%sDefaultNewsController',
149 $this->getCapitalizedClassPrefix()
153 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
155 // Now, let us create the full name of the controller class
156 $this->setClassName($className);
158 // Try it hard to get an controller
159 while ($isValid === FALSE) {
160 // Is this class already loaded?
161 if (class_exists($this->getClassName())) {
162 // This class does exist. :-)
164 } elseif ($this->getClassName() != $newsControllerName) {
165 // Set default controller
166 $this->setClassName($newsControllerName);
168 // All is tried, give it up here
169 throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
178 * Resolves the default controller of the given controller
180 * @return $controllerInstance A controller instance for the default
182 * @throws InvalidControllerInstanceException Thrown if $controllerInstance
185 public function resolveController () {
187 $controllerName = '';
188 $controllerInstance = NULL;
190 // Get namespace and controller name
191 $controllerName = $this->getControllerName();
193 // Get the controller
194 $controllerInstance = $this->loadController($controllerName);
197 if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
198 // This controller has an invalid instance!
199 throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
202 // Set last controller
203 $this->setResolvedInstance($controllerInstance);
205 // Return the maybe resolved instance
206 return $controllerInstance;