]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/factories/objects/class_ObjectFactory.php
Rewrite:
[core.git] / framework / main / classes / factories / objects / class_ObjectFactory.php
index 46d2b6e02ba8b133887cee21acfdf776fbcb1324..2701ede9a53ffb256197a2f6fd233c31d37602b6 100644 (file)
@@ -3,10 +3,13 @@
 namespace CoreFramework\Factory;
 
 // Import framework stuff
-use CoreFramework\Configuration\FrameworkConfiguration;
+use CoreFramework\Bootstrap\FrameworkBootstrap;
 use CoreFramework\Generic\EmptyVariableException;
 use CoreFramework\Loader\NoClassException;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * An general object factory
  *
@@ -51,11 +54,15 @@ class ObjectFactory extends BaseFactory {
         * @return      $objectInstance         An instance of the requested object
         * @throws      NoClassException        If the requested class was not found
         * @throws      EmptyVariableException  If a variable is empty unexpectly
+        * @throws      InvalidArgumentException        If class name is not following naming-convention
         */
-       public static final function createObjectByName ($className, array $args=array()) {
+       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);
+
                // Is the class name valid and is the class there?
                if (empty($className)) {
                        // Throw an exception here
@@ -63,6 +70,9 @@ class ObjectFactory extends BaseFactory {
                } elseif (!class_exists($className)) {
                        // Then throw an exception
                        throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
+               } elseif (count($classNameParts) < 3) {
+                       // Namespaces are missing
+                       throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Project\Package[\SubPackage...]\SomeFooBar', $className));
                }
 
                // Create method name
@@ -85,9 +95,9 @@ class ObjectFactory extends BaseFactory {
         * @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 ($configEntry, array $args = array()) {
                // Read the configuration entry
-               $className = FrameworkConfiguration::getSelfInstance()->getConfigEntry($configEntry);
+               $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry($configEntry);
 
                // Send this to the other factory...
                $objectInstance = self::createObjectByName($className, $args);