]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/resolver/command/class_BaseCommandResolver.php
Continued:
[core.git] / framework / main / classes / resolver / command / class_BaseCommandResolver.php
index 98c3a81e3dc19d1ec7286881a49a22a3f911976e..2a231dc0ee4e98c0fcda71db7f27b1fd268d795a 100644 (file)
@@ -6,9 +6,11 @@ namespace Org\Mxchange\CoreFramework\Resolver\Command;
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Command\Commandable;
 use Org\Mxchange\CoreFramework\Command\InvalidCommandException;
-use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
+use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Request\Requestable;
 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
+use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
 
 // Import SPL stuff
 use \InvalidArgumentException;
@@ -19,7 +21,7 @@ use \UnexpectedValueException;
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 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
  *
@@ -68,7 +70,7 @@ abstract class BaseCommandResolver extends BaseResolver {
         *
         * @return      $commandName    Last validated command name
         */
-       protected final function getCommandName () {
+       public final function getCommandName () {
                return $this->commandName;
        }
 
@@ -80,7 +82,7 @@ abstract class BaseCommandResolver extends BaseResolver {
         * @throws      InvalidCommandException         Thrown if even the default
         *                                                                              command class is missing (bad!)
         */
-       protected function loadCommand ($commandName) {
+       protected function loadCommand (string $commandName) {
                // Init command instance
                $commandInstance = NULL;
 
@@ -89,7 +91,7 @@ abstract class BaseCommandResolver extends BaseResolver {
                        '%s\%s%sCommand',
                        $this->getNamespace(),
                        $this->getCapitalizedClassPrefix(),
-                       self::convertToClassName($commandName)
+                       StringUtils::convertToClassName($commandName)
                );
 
                // Create command class name
@@ -99,10 +101,10 @@ abstract class BaseCommandResolver extends BaseResolver {
                if (!class_exists($this->getClassName())) {
                        // Class not found, so throw an exception
                        throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
-               } // END - if
+               }
 
                // Initiate the command
-               $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
+               $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), [$this]);
 
                // Return the result
                return $commandInstance;
@@ -119,22 +121,21 @@ abstract class BaseCommandResolver extends BaseResolver {
         */
        public function resolveCommandByRequest (Requestable $requestInstance) {
                // Init variables
-               $commandName = '';
-               $commandInstance = NULL;
-
-               // This goes fine so let's resolve the command
                $commandName = $requestInstance->getRequestElement('command');
+               $commandType = FrameworkBootstrap::getRequestTypeFromSystem();
+               $commandInstance = NULL;
 
                // Is the command empty? Then fall back to default command
                if (empty($commandName)) {
-                       $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
-               } // END - if
+                       // Fall back to default command
+                       $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('default_%s_command', $commandType));
+               }
 
                // Check if command is valid
                if ($this->isCommandValid($this->getNamespace(), $commandName) === false) {
                        // This command is invalid!
                        throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
-               } // END - if
+               }
 
                // Get the command
                $commandInstance = $this->loadCommand($commandName);
@@ -142,12 +143,15 @@ abstract class BaseCommandResolver extends BaseResolver {
                // And validate it
                if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
                        // This command has an invalid instance!
-                       throw new UnexpectedValueException(sprintf('commandInstance for commandName=%s is not object (%s) or does not implement Commandable.', $commandName, gettype($commandInstance)), self::EXCEPTION_INVALID_COMMAND);
-               } // END - if
+                       throw new UnexpectedValueException(sprintf('commandInstance for commandName=%s is not object (%s) or does not implement Commandable.', $commandName, gettype($commandInstance)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
+               }
 
                // Set last command
                $this->setResolvedInstance($commandInstance);
 
+               // Init template engine
+               $commandInstance->initTemplateEngine($commandType);
+
                // Return the resolved command instance
                return $commandInstance;
        }
@@ -160,34 +164,38 @@ abstract class BaseCommandResolver extends BaseResolver {
         * @return      $commandInstance        An instance of the command class
         * @throws      InvalidCommandException         Thrown if $commandName is invalid
         */
-       public function resolveCommand ($namespace, $commandName) {
+       public function resolveCommand (string $namespace, string $commandName) {
                // 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($commandName)) {
                        // Then thrown an exception here
-                       throw new InvalidArgumentException('Parameter "commandName" is empty');
+                       throw new InvalidArgumentException('Parameter "commandName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
                // Initiate the instance variable
                $commandInstance = NULL;
+               $commandType = FrameworkBootstrap::getRequestTypeFromSystem();
 
                // Is the command empty? Then fall back to default command
                if (empty($commandName)) {
                        // Init default command
-                       $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
-               } // END - if
+                       $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('default_%s_command', $commandType));
+               }
 
                // Check if command is valid
                if ($this->isCommandValid($namespace, $commandName) === false) {
                        // This command is invalid!
                        throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
-               } // END - if
+               }
 
                // Get the command
                $commandInstance = $this->loadCommand($commandName);
 
+               // Init template engine
+               $commandInstance->initTemplateEngine($commandType);
+
                // Return the instance
                return $commandInstance;
        }
@@ -200,14 +208,14 @@ abstract class BaseCommandResolver extends BaseResolver {
         * @return      $isValid                Whether the given command is valid
         * @throws      InvalidArgumentException        Thrown if given command is not set
         */
-       protected function isCommandValid ($namespace, $commandName) {
+       protected function isCommandValid (string $namespace, string $commandName) {
                // Is namespace and command name 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($commandName)) {
                        // Then thrown an exception here
-                       throw new InvalidArgumentException('Parameter "commandName" is empty');
+                       throw new InvalidArgumentException('Parameter "commandName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
                }
 
                // By default nothing shall be valid
@@ -218,7 +226,7 @@ abstract class BaseCommandResolver extends BaseResolver {
                        '%s\%s%sCommand',
                        $namespace,
                        $this->getCapitalizedClassPrefix(),
-                       self::convertToClassName($commandName)
+                       StringUtils::convertToClassName($commandName)
                );
 
                // Now, let us create the full name of the command class
@@ -228,7 +236,7 @@ abstract class BaseCommandResolver extends BaseResolver {
                if (class_exists($this->getClassName())) {
                        // This class does exist. :-)
                        $isValid = true;
-               } // END - if
+               }
 
                // Set command name
                $this->setCommandName($commandName);