]> git.mxchange.org Git - core.git/commitdiff
Strict naming-convention (Project\Package[\SubPackage]\SomeFooBar) is now also
authorRoland Häder <roland@mxchange.org>
Tue, 23 May 2017 19:11:59 +0000 (21:11 +0200)
committerRoland Häder <roland@mxchange.org>
Tue, 23 May 2017 19:11:59 +0000 (21:11 +0200)
done by ObjectFactory.

Signed-off-by: Roland Häder <roland@mxchange.org>
framework/loader/class_ClassLoader.php
framework/main/classes/factories/objects/class_ObjectFactory.php

index 211aded9b438cf368bb4a67a3103ea62f265bc62..e1660b2591dda5ee338b4b9a654b6692ec7993f9 100644 (file)
@@ -436,7 +436,7 @@ class ClassLoader {
                        // Get current entry
                        $currentEntry = $iteratorInstance->current();
 
-                       // Get filename from iterator
+                       // Get filename from iterator which is the class' name (according naming-convention)
                        $fileName = $currentEntry->getFileName();
 
                        // Get the "FQFN" (path and file name)
@@ -526,7 +526,7 @@ class ClassLoader {
         *
         * @param       $className      The class that shall be loaded
         * @return      void
-        * @throws      InvalidArgumentException        If strict-checking is enabled and class name is not following naming convention
+        * @throws      InvalidArgumentException        If strict-checking is enabled and class name is not following naming-convention
         */
        private function loadClassFile ($className) {
                // Trace message
index 46d2b6e02ba8b133887cee21acfdf776fbcb1324..ebde62499338fe56c3bfe666bd45da5c4955cf60 100644 (file)
@@ -7,6 +7,9 @@ use CoreFramework\Configuration\FrameworkConfiguration;
 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,7 +95,7 @@ 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);