]> git.mxchange.org Git - core.git/blobdiff - inc/classes/main/resolver/controller/class_BaseControllerResolver.php
Fixed a typo
[core.git] / inc / classes / main / resolver / controller / class_BaseControllerResolver.php
index 23e449ac913386c2b6092539f41cdfc2d39004b5..b4032e266eb2e22dfc311fcbfaf0780363ae9da8 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseControllerResolver extends BaseResolver {
-       /**
-        * Prefix for local, remote or other resolver
-        */
-       private $controllerPrefix = '';
-
        /**
         * Validated controller name
         */
@@ -43,16 +38,6 @@ class BaseControllerResolver extends BaseResolver {
                parent::__construct($className);
        }
 
-       /**
-        * Setter for controller prefix
-        *
-        * @param       $controllerPrefix       Last validated controllerPrefix
-        * @return      void
-        */
-       protected final function setControllerPrefix ($controllerPrefix) {
-               $this->controllerPrefix = $controllerPrefix;
-       }
-
        /**
         * Setter for controller name
         *
@@ -73,10 +58,76 @@ class BaseControllerResolver extends BaseResolver {
        }
 
        /**
-        * Checks wether the given controller is valid
+        * "Loads" a given controller and instances it if not yet cached. If the
+        * controller was not found one of the default controllers will be used
+        * depending on whether news shall be displayed.
+        *
+        * @param       $controllerName                 A controller name we shall look for
+        * @return      $controllerInstance             A loaded controller instance
+        * @throws      InvalidControllerException      Thrown if even the requested
+        *                                                                              controller class is missing (bad!)
+        */
+       protected function loadController ($controllerName) {
+               // Cache default command
+               $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_command');
+
+               // Init controller instance
+               $controllerInstance = NULL;
+
+               // Default controller
+               $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
+
+               // Generate the class name
+               //* DEBUG: */ $this->debugOutput('BEFORE: controller=' . $controllerName);
+               if ($controllerName != $defaultController) {
+                       // Create controller class name
+                       $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
+
+                       // ... and set it
+                       $this->setClassName($className);
+               } else {
+                       // No news at main command or non-news command
+                       $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
+               }
+               //* DEBUG: */ $this->debugOutput('AFTER: controller=' . $this->getClassName());
+
+               // Is this class loaded?
+               if (!class_exists($this->getClassName())) {
+                       // Throw an exception here
+                       throw new InvalidControllerException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
+               } // END - if
+
+               // Set default resolver config name
+               $resolverConfigEntry = '';
+
+               // Try to read a config entry for our resolver including controller name... ;-)
+               $resolverConfigEntry = sprintf("%s_cmd_%s_resolver_class", strtolower($this->getClassPrefix()), strtolower($controllerName));
+
+               // Get the config, this will throw an exception if there is no special command resolver
+               $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
+
+               // Initiate the resolver and controller
+               $resolverInstance = ObjectFactory::createObjectByConfiguredName(
+                       $resolverConfigEntry,
+                       array(
+                               $controllerName,
+                               $this->getApplicationInstance()
+                       )
+               );
+               $controllerInstance = ObjectFactory::createObjectByName(
+                       $this->getClassName(),
+                       array($resolverInstance)
+               );
+
+               // Return the result
+               return $controllerInstance;
+       }
+
+       /**
+        * Checks whether the given controller is valid
         *
         * @param       $controllerName         The default controller we shall execute
-        * @return      $isValid                        Wether the given controller is valid
+        * @return      $isValid                        Whether the given controller is valid
         * @throws      EmptyVariableException          Thrown if given controller is not set
         * @throws      DefaultControllerException      Thrown if default controller was not found
         */
@@ -91,7 +142,7 @@ class BaseControllerResolver extends BaseResolver {
                } // END - if
 
                // Create class name
-               $className = $this->controllerPrefix . $this->convertToClassName($controllerName) . 'Controller';
+               $className = $this->getClassPrefix() . $this->convertToClassName($controllerName) . 'Controller';
 
                // Now, let us create the full name of the controller class
                $this->setClassName($className);
@@ -102,9 +153,9 @@ class BaseControllerResolver extends BaseResolver {
                        if (class_exists($this->getClassName())) {
                                // This class does exist. :-)
                                $isValid = true;
-                       } elseif ($this->getClassName() != $this->controllerPrefix.'DefaultNewsController') {
+                       } elseif ($this->getClassName() != $this->getClassPrefix() . 'DefaultNewsController') {
                                // Set default controller
-                               $this->setClassName($this->controllerPrefix . 'DefaultNewsController');
+                               $this->setClassName($this->getClassPrefix() . 'DefaultNewsController');
                        } else {
                                // All is tried, give it up here
                                throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);