From 641da41644935fa9a77e44564c66b1b8a74a945b Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Wed, 2 Mar 2016 16:05:01 +0100
Subject: [PATCH] Another bugfix: sometimes, very early during execution, a
 class with namespaces might not be autoloaded yet, and if we are asked to
 load the old class without namespaces, it will fail. Make it load the file
 corresponding to the class first, and see if it exists then. If not, try to
 see if it has been migrated to namespaces, and create the alias then.

---
 lib/_autoload_modules.php | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/lib/_autoload_modules.php b/lib/_autoload_modules.php
index f1fa42590..3e0c6cecf 100644
--- a/lib/_autoload_modules.php
+++ b/lib/_autoload_modules.php
@@ -23,17 +23,26 @@ function temporaryLoader($class)
         return; // not a valid class name for old classes
     }
 
+    // try to load it from the corresponding file
     $path = explode('_', $class);
+    $file = dirname(__FILE__).DIRECTORY_SEPARATOR.join(DIRECTORY_SEPARATOR, $path).'.php';
+    if (file_exists($file)) {
+        require_once $file;
+    }
+
+    // it exists, so it's not yet migrated to namespaces
+    if (class_exists($class, false)) {
+        return;
+    }
+
+    // it didn't exist, try to see if it was migrated to namespaces
     $new = join('\\', $path);
-    if (class_exists($new, false)) {
+    if (class_exists($new, false)) { // do not try to autoload it if it doesn't exist! It should!
         class_alias($new, $class);
         SimpleSAML\Logger::warning("The class '$class' is now using namespaces, please use '$new'.");
     }
 
-    $file = dirname(__FILE__).DIRECTORY_SEPARATOR.join(DIRECTORY_SEPARATOR, $path).'.php';
-    if (file_exists($file)) {
-        require_once $file;
-    }
+
 }
 
 spl_autoload_register("temporaryLoader");
-- 
GitLab