]> git.mxchange.org Git - core.git/blobdiff - framework/loader/class_ClassLoader.php
better this way, "cache" loader instance to avoid calling getSelfInstance()
[core.git] / framework / loader / class_ClassLoader.php
index 808edcf5b83fd0d77e69b9bae98ce023a643d95e..0560362015876308c5d86c846d46d652bb4f3634 100644 (file)
@@ -4,6 +4,7 @@ namespace CoreFramework\Loader;
 
 // Import framework stuff
 use CoreFramework\Configuration\FrameworkConfiguration;
+use CoreFramework\EntryPoint\ApplicationEntryPoint;
 use CoreFramework\Object\BaseFrameworkSystem;
 
 // Import SPL stuff
@@ -114,6 +115,11 @@ class ClassLoader {
         */
        private $total = 0;
 
+       /**
+        * By default the class loader is strict with naming-convention check
+        */
+       private static $strictNamingConventionCheck = TRUE;
+
        /**
         * Framework/application paths for classes, etc.
         */
@@ -124,6 +130,11 @@ class ClassLoader {
                'middleware'  // The middleware
        );
 
+       /**
+        * Registered paths where test classes can be found. These are all relative
+        * to base_path .
+        */
+       private static $testPaths = array();
 
        /**
         * The protected constructor. Please use the factory method below, or use
@@ -132,7 +143,7 @@ class ClassLoader {
         * @return      void
         */
        protected function __construct () {
-               // Is currently empty
+               // This is empty for now
        }
 
        /**
@@ -193,17 +204,42 @@ class ClassLoader {
                // Trace message
                //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
 
-               // Cache loader instance
+               // Get loader instance
                $loaderInstance = self::getSelfInstance();
 
+               // Get config instance
+               $cfg = FrameworkConfiguration::getSelfInstance();
+
                // Load all classes
-               foreach (self::$frameworkPaths as $pathName) {
+               foreach (self::$frameworkPaths as $shortPath) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
+
+                       // Generate full path from it
+                       $pathName = realpath(sprintf(
+                               '%s/framework/main/%s/',
+                               $cfg->getConfigEntry('base_path'),
+                               $shortPath
+                       ));
+
                        // Debug message
                        //* NOISY-DEBUG: */ printf('[%s:%d]: pathName=%s' . PHP_EOL, __METHOD__, __LINE__, $pathName);
 
+                       // Is it not FALSE and accessible?
+                       if (is_bool($pathName)) {
+                               // Skip this
+                               continue;
+                       } elseif (!is_readable($pathName)) {
+                               // @TODO Throw exception instead of break
+                               break;
+                       }
+
                        // Try to load the framework classes
-                       $loaderInstance->scanClassPath('framework/main/' . $pathName . '/');
+                       $loaderInstance->scanClassPath($pathName);
                } // END - foreach
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
@@ -215,73 +251,97 @@ class ClassLoader {
                // Trace message
                //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
 
+               // Get loader instance
+               $loaderInstance = self::getSelfInstance();
+
                // Get config instance
                $cfg = FrameworkConfiguration::getSelfInstance();
 
                // Load all classes for the application
-               foreach (self::$frameworkPaths as $class) {
+               foreach (self::$frameworkPaths as $shortPath) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
+
                        // Create path name
-                       $pathName = sprintf('%s/%s/%s', $cfg->getConfigEntry('application_path'), $cfg->getConfigEntry('app_name'), $class);
+                       $pathName = realpath(sprintf(
+                               '%s/%s/%s',
+                               $cfg->getConfigEntry('application_path'),
+                               $cfg->getConfigEntry('app_name'),
+                               $shortPath
+                       ));
 
                        // Debug message
-                       //* NOISY-DEBUG: */ printf('[%s:%d]: pathName=%s' . PHP_EOL, __METHOD__, __LINE__, $pathName);
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
 
                        // Is the path readable?
                        if (is_dir($pathName)) {
                                // Try to load the application classes
-                               ClassLoader::getSelfInstance()->scanClassPath($pathName);
+                               $loaderInstance->scanClassPath($pathName);
                        } // END - if
                } // END - foreach
+
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
        }
 
        /**
-        * Initializes our loader class
+        * Scans for test classes, etc.
         *
-        * @param       $configInstance Configuration class instance
         * @return      void
         */
-       protected function initLoader (FrameworkConfiguration $configInstance) {
-               // Set configuration instance
-               $this->configInstance = $configInstance;
-
-               // Construct the FQFN for the cache
-               if (!defined('DEVELOPER')) {
-                       $this->listCacheFQFN  = $this->configInstance->getConfigEntry('local_db_path') . 'list-' . $this->configInstance->getConfigEntry('app_name') . '.cache';
-                       $this->classCacheFQFN = $this->configInstance->getConfigEntry('local_db_path') . 'class-' . $this->configInstance->getConfigEntry('app_name') . '.cache';
-               } // END - if
+       public static function scanTestsClasses () {
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: CALLED!' . PHP_EOL, __METHOD__, __LINE__);
 
-               // Set suffix and prefix from configuration
-               $this->suffix = $configInstance->getConfigEntry('class_suffix');
-               $this->prefix = $configInstance->getConfigEntry('class_prefix');
+               // Get config instance
+               $cfg = FrameworkConfiguration::getSelfInstance();
 
-               // Set own instance
-               self::$selfInstance = $this;
+               // Load all classes for the application
+               foreach (self::$testPaths as $shortPath) {
+                       // Debug message
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: shortPath=%s' . PHP_EOL, __METHOD__, __LINE__, $shortPath);
 
-               // Skip here if no dev-mode
-               if (defined('DEVELOPER')) {
-                       return;
-               } // END - if
+                       // Create path name
+                       $pathName = realpath(sprintf(
+                               '%s/%s',
+                               $cfg->getConfigEntry('base_path'),
+                               $shortPath
+                       ));
 
-               // IS the cache there?
-               if (BaseFrameworkSystem::isReadableFile($this->listCacheFQFN)) {
-                       // Get content
-                       $cacheContent = file_get_contents($this->listCacheFQFN);
+                       // Debug message
+                       //* NOISY-DEBUG: */ printf('[%s:%d]: pathName[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($pathName), $pathName);
 
-                       // And convert it
-                       $this->foundClasses = json_decode($cacheContent);
+                       // Is the path readable?
+                       if (is_dir($pathName)) {
+                               // Try to load the application classes
+                               ClassLoader::getSelfInstance()->scanClassPath($pathName);
+                       } // END - if
+               } // END - foreach
 
-                       // List has been restored from cache!
-                       $this->listCached = TRUE;
-               } // END - if
+               // Trace message
+               //* NOISY-DEBUG: */ printf('[%s:%d]: EXIT!' . PHP_EOL, __METHOD__, __LINE__);
+       }
 
-               // Does the class cache exist?
-               if (BaseFrameworkSystem::isReadableFile($this->listCacheFQFN)) {
-                       // Then include it
-                       require($this->classCacheFQFN);
+       /**
+        * Enables or disables strict naming-convention tests on class loading
+        *
+        * @param       $strictNamingConventionCheck    Whether to strictly check naming-convention
+        * @return      void
+        */
+       public static function enableStrictNamingConventionCheck ($strictNamingConventionCheck = TRUE) {
+               self::$strictNamingConventionCheck = $strictNamingConventionCheck;
+       }
 
-                       // Mark the class cache as loaded
-                       $this->classesCached = TRUE;
-               } // END - if
+       /**
+        * Registeres given relative path where test classes reside. For regular
+        * framework uses, they should not be loaded (and used).
+        *
+        * @param       $relativePath   Relative path to test classes
+        * @return      void
+        */
+       public static function registerTestsPath ($relativePath) {
+               // "Register" it
+               self::$testPaths[$relativePath] = $relativePath;
        }
 
        /**
@@ -417,6 +477,56 @@ class ClassLoader {
                $this->prefix = $oldPrefix;
        }
 
+       /**
+        * Initializes our loader class
+        *
+        * @param       $configInstance Configuration class instance
+        * @return      void
+        */
+       private function initLoader (FrameworkConfiguration $configInstance) {
+               // Set configuration instance
+               $this->configInstance = $configInstance;
+
+               // Construct the FQFN for the cache
+               if (!defined('DEVELOPER')) {
+                       $this->listCacheFQFN  = $this->configInstance->getConfigEntry('local_db_path') . 'list-' . $this->configInstance->getConfigEntry('app_name') . '.cache';
+                       $this->classCacheFQFN = $this->configInstance->getConfigEntry('local_db_path') . 'class-' . $this->configInstance->getConfigEntry('app_name') . '.cache';
+               } // END - if
+
+               // Set suffix and prefix from configuration
+               $this->suffix = $configInstance->getConfigEntry('class_suffix');
+               $this->prefix = $configInstance->getConfigEntry('class_prefix');
+
+               // Set own instance
+               self::$selfInstance = $this;
+
+               // Skip here if no dev-mode
+               if (defined('DEVELOPER')) {
+                       return;
+               } // END - if
+
+               // IS the cache there?
+               if (BaseFrameworkSystem::isReadableFile($this->listCacheFQFN)) {
+                       // Get content
+                       $cacheContent = file_get_contents($this->listCacheFQFN);
+
+                       // And convert it
+                       $this->foundClasses = json_decode($cacheContent);
+
+                       // List has been restored from cache!
+                       $this->listCached = TRUE;
+               } // END - if
+
+               // Does the class cache exist?
+               if (BaseFrameworkSystem::isReadableFile($this->listCacheFQFN)) {
+                       // Then include it
+                       require($this->classCacheFQFN);
+
+                       // Mark the class cache as loaded
+                       $this->classesCached = TRUE;
+               } // END - if
+       }
+
        /**
         * Tries to find the given class in our list. This method ignores silently
         * missing classes or interfaces. So if you use class_exists() this method
@@ -424,6 +534,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
         */
        private function loadClassFile ($className) {
                // Trace message
@@ -433,7 +544,7 @@ class ClassLoader {
                $classNameParts = explode("\\", $className);
 
                // At least 3 parts should be there
-               if (count($classNameParts) < 3) {
+               if ((self::$strictNamingConventionCheck === TRUE) && (count($classNameParts) < 3)) {
                        // Namespace scheme is: Project\Package[\SubPackage...]
                        throw new InvalidArgumentException(sprintf('Class name "%s" is not conform to naming-convention: Project\Package[\SubPackage...]\SomeFooBar', $className));
                } // END - if