Continued:
[core.git] / framework / main / classes / resolver / controller / class_BaseControllerResolver.php
index b1c1024ba7e911491b1c7379b934ae34fe62c19b..f60774d82ef0c7b4524b259075f05911fed76886 100644 (file)
@@ -3,13 +3,16 @@
 namespace Org\Mxchange\CoreFramework\Resolver\Controller;
 
 // Import framework stuff
+use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Controller\Controller;
 use Org\Mxchange\CoreFramework\Controller\DefaultControllerException;
 use Org\Mxchange\CoreFramework\Controller\InvalidControllerException;
-use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
-use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
 use Org\Mxchange\CoreFramework\Resolver\Controller\ControllerResolver;
+use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
 
 // Import SPL stuff
 use \InvalidArgumentException;
@@ -19,7 +22,7 @@ use \InvalidArgumentException;
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -37,17 +40,41 @@ use \InvalidArgumentException;
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 abstract class BaseControllerResolver extends BaseResolver {
+       /**
+        * Controller name
+        */
+       private $controllerName = '';
+
        /**
         * Protected constructor
         *
         * @param       $className      Name of the real class
         * @return      void
         */
-       protected function __construct ($className) {
+       protected function __construct (string $className) {
                // Call parent constructor
                parent::__construct($className);
        }
 
+       /**
+        * Setter for controller name
+        *
+        * @param       $controllerName Last validated controller name
+        * @return      void
+        */
+       protected final function setControllerName (string $controllerName) {
+               $this->controllerName = $controllerName;
+       }
+
+       /**
+        * Getter for controller name
+        *
+        * @return      $controllerName Last validated controller name
+        */
+       protected final function getControllerName () {
+               return $this->controllerName;
+       }
+
        /**
         * "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
@@ -58,12 +85,9 @@ abstract class BaseControllerResolver extends BaseResolver {
         * @throws      InvalidControllerException      Thrown if even the requested
         *                                                                              controller class is missing (bad!)
         */
-       protected function loadController ($controllerName) {
+       protected function loadController (string $controllerName) {
                // Cache default controller
-               $defaultController = $this->getConfigInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
-
-               // Init controller instance
-               $controllerInstance = NULL;
+               $defaultController = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . strtolower($this->getClassPrefix()) . '_controller');
 
                // Create full class name
                $className = sprintf(
@@ -83,7 +107,7 @@ abstract class BaseControllerResolver extends BaseResolver {
                                '%s\%s%sController',
                                $this->getNamespace(),
                                $this->getCapitalizedClassPrefix(),
-                               self::convertToClassName($controllerName)
+                               StringUtils::convertToClassName($controllerName)
                        );
 
                        // ... and set it
@@ -92,31 +116,31 @@ abstract class BaseControllerResolver extends BaseResolver {
                        // No news at main controller or non-news controller
                        $this->setClassName($className);
                }
-               //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
 
                // Is this class loaded?
+               //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('AFTER: controller=' . $this->getClassName());
                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 controller resolver
-               $resolverClass = $this->getConfigInstance()->getConfigEntry($resolverConfigEntry);
+               //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-CONTROLLER-RESOLVER: resolverConfigEntry=%s', $resolverConfigEntry));
+               $resolverClass = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($resolverConfigEntry);
 
                // Initiate the resolver and controller
                $resolverInstance = ObjectFactory::createObjectByConfiguredName(
                        $resolverConfigEntry,
                        array(
                                $controllerName,
-                               GenericRegistry::getRegistry()->getInstance('application')
+                               ApplicationHelper::getSelfInstance()
                        )
                );
+
+               // Get controller instance
                $controllerInstance = ObjectFactory::createObjectByName(
                        $this->getClassName(),
                        array($resolverInstance)
@@ -135,35 +159,42 @@ abstract class BaseControllerResolver extends BaseResolver {
         * @throws      InvalidArgumentException                Thrown if given controller is not set
         * @throws      DefaultControllerException      Thrown if default controller was not found
         */
-       protected function isControllerValid ($namespace, $controllerName) {
-               // By default nothing shall be valid
-               $isValid = false;
-
-               // Is namespace and controller name set?
+       protected function isControllerValid (string $namespace, string $controllerName) {
+               // Is a action set?
                if (empty($namespace)) {
                        // Then thrown an exception here
-                       throw new InvalidArgumentException('Parameter "namespace" is empty');
+                       throw new InvalidArgumentException('Parameter "namespace" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                } elseif (empty($controllerName)) {
                        // Then thrown an exception here
-                       throw new InvalidArgumentException('Parameter "controllerName" is empty');
+                       throw new InvalidArgumentException('Parameter "controllerName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
+               // By default nothing shall be valid
+               $isValid = false;
+
                // Create class name
                $className = sprintf(
-                       '%s\%sController',
+                       '%s\%s%sController',
                        $namespace,
-                       $this->getCapitalizedClassPrefix() . self::convertToClassName($controllerName)
+                       $this->getCapitalizedClassPrefix(),
+                       StringUtils::convertToClassName($controllerName)
                );
-               $newsControllerName = sprintf(
+
+               // Application's default news controller
+               $appDefaultControllerName = sprintf(
                        '%s\%sDefaultNewsController',
                        $namespace,
                        $this->getCapitalizedClassPrefix()
                );
 
-               // Debug message
-               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
+               // Framework's default news controller
+               $defaultControllerName = sprintf(
+                       'Org\Mxchange\CoreFramework\Controller\News\%sDefaultNewsController',
+                       $this->getCapitalizedClassPrefix()
+               );
 
                // Now, let us create the full name of the controller class
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('className=%s', $className));
                $this->setClassName($className);
 
                // Try it hard to get an controller
@@ -172,14 +203,17 @@ abstract class BaseControllerResolver extends BaseResolver {
                        if (class_exists($this->getClassName())) {
                                // This class does exist. :-)
                                $isValid = true;
-                       } elseif ($this->getClassName() != $newsControllerName) {
-                               // Set default controller
-                               $this->setClassName($newsControllerName);
+                       } elseif ($this->getClassName() != $appDefaultControllerName) {
+                               // Set application's default controller
+                               $this->setClassName($appDefaultControllerName);
+                       } elseif ($this->getClassName() != $defaultControllerName) {
+                               // Set framework's default controller
+                               $this->setClassName($defaultControllerName);
                        } else {
                                // All is tried, give it up here
                                throw new DefaultControllerException($this, self::EXCEPTION_DEFAULT_CONTROLLER_GONE);
                        }
-               } // END - while
+               }
 
                // Return the result
                return $isValid;
@@ -195,10 +229,6 @@ abstract class BaseControllerResolver extends BaseResolver {
         */
        public function resolveController () {
                // Init variables
-               $controllerName = '';
-               $controllerInstance = NULL;
-
-               // Get namespace and controller name
                $controllerName = $this->getControllerName();
 
                // Get the controller
@@ -208,7 +238,7 @@ abstract class BaseControllerResolver extends BaseResolver {
                if ((!is_object($controllerInstance)) || (!$controllerInstance instanceof Controller)) {
                        // This controller has an invalid instance!
                        throw new InvalidControllerInstanceException(array($this, $controllerName), self::EXCEPTION_INVALID_CONTROLLER);
-               } // END - if
+               }
 
                // Set last controller
                $this->setResolvedInstance($controllerInstance);