]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/factories/objects/class_ObjectFactory.php
Continued:
[core.git] / framework / main / classes / factories / objects / class_ObjectFactory.php
index e0ca76b3087bd3937aea774d9e4677484b073284..e100dc69e8f662e34812be92b1c96705733a306c 100644 (file)
@@ -1,9 +1,11 @@
 <?php
 // Own namespace
-namespace Org\Mxchange\CoreFramework\Factory;
+namespace Org\Mxchange\CoreFramework\Factory\Object;
 
 // Import framework stuff
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
+use Org\Mxchange\CoreFramework\Factory\BaseFactory;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Loader\NoClassException;
 
 // Import SPL stuff
@@ -14,7 +16,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
  *
@@ -38,7 +40,7 @@ class ObjectFactory extends BaseFactory {
         * @param       $className      Name of this class
         * @return      void
         */
-       protected function __construct ($className = __CLASS__) {
+       protected function __construct (string $className = __CLASS__) {
                // Call parent constructor
                parent::__construct($className);
        }
@@ -48,39 +50,42 @@ class ObjectFactory extends BaseFactory {
         * the class was not found. No parameters for the object are currently
         * supported.
         *
-        * @param       $className                      Name of the class we shall construct
+        * @param       $fullClassName          Name of the class we shall construct
         * @param       $args                           Arguments in an indexed array
         * @return      $objectInstance         An instance of the requested object
         * @throws      NoClassException        If the requested class was not found
         * @throws      InvalidArgumentException        If className is empty or the name not following naming-convention
         */
-       public static final function createObjectByName ($className, array $args = array()) {
-               // First get an instance of this factory
-               $factoryInstance = new ObjectFactory();
-
-               // Split class name on backslash to check naming-convention
-               $classNameParts = explode("\\", $className);
-
+       public static final function createObjectByName (string $fullClassName, array $args = []) {
                // Is the class name valid and is the class there?
-               if (empty($className)) {
+               if (empty($fullClassName)) {
                        // Throw an exception here
-                       throw new InvalidArgumentException('Parameter "className" is empty');
-               } elseif (!class_exists($className)) {
+                       throw new InvalidArgumentException('Parameter "fullClassName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif (!self::isClassCounted($fullClassName) && !class_exists($fullClassName)) {
+                       // First get an instance of this factory
+                       $factoryInstance = new ObjectFactory();
+
                        // Then throw an exception
-                       throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
-               } elseif (count($classNameParts) < 3) {
+                       throw new NoClassException([$factoryInstance, $fullClassName], FrameworkInterface::EXCEPTION_CLASS_NOT_FOUND);
+               }
+
+               // Split class name on backslash to check naming-convention
+               $classNameParts = explode("\\", $fullClassName);
+
+               // Okay, does it follow naming-convention?
+               if (count($classNameParts) < 4) {
                        // Namespaces are missing
-                       throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Project\Package[\SubPackage...]\SomeFooBar', $className));
+                       throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Tld\Domain\Project\Package[\SubPackage...]\SomeFooBar', $fullClassName));
                }
 
                // Create method name
-               $methodName = sprintf('create%s', self::stripNamespaceFromClassName($className));
+               $methodName = sprintf('create%s', self::stripNamespaceFromClassName($fullClassName));
 
                // Run the user function
-               $objectInstance = call_user_func_array(array($className, $methodName), $args);
+               $objectInstance = call_user_func_array([$fullClassName, $methodName], $args);
 
                // Count this one up
-               self::countObject($className);
+               self::countObject($fullClassName);
 
                // Return the prepared instance
                return $objectInstance;
@@ -89,16 +94,16 @@ class ObjectFactory extends BaseFactory {
        /**
         * Creates an object by it's configured name
         *
-        * @param       $configEnttry           Configuration entry to read
+        * @param       $configKey              Configuration key to read
         * @param       $args                           Arguments in an indexed array
         * @return      $objectInstance         An instance of the requested object
         */
-       public static final function createObjectByConfiguredName ($configEntry, array $args = array()) {
+       public static final function createObjectByConfiguredName (string $configKey, array $args = []) {
                // Read the configuration entry
-               $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
+               $fullClassName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configKey);
 
                // Send this to the other factory...
-               $objectInstance = self::createObjectByName($className, $args);
+               $objectInstance = self::createObjectByName($fullClassName, $args);
 
                // Return the instance
                return $objectInstance;
@@ -111,18 +116,11 @@ class ObjectFactory extends BaseFactory {
         *
         * @param       $fullClassName  Class name with namespace
         * @return      $shortClassName Stripped class name (no namespace)
-        * @throws      InvalidClassNameException       If the class name does not follow naming convention
         */
-       private static function stripNamespaceFromClassName ($fullClassName) {
+       private static function stripNamespaceFromClassName (string $fullClassName) {
                // The class name should contain at least 2 back-slashes, so split at them
                $classNameParts = explode("\\", $fullClassName);
 
-               // At least 3 parts should be there
-               if (count($classNameParts) < 3) {
-                       // Namespace scheme is: Project\Package[\SubPackage...]
-                       throw new InvalidClassNameException($fullClassName, self::EXCEPTION_INVALID_CLASS_NAME);
-               } // END - if
-
                // Get last element
                $shortClassName = array_pop($classNameParts);