From 07b6831248a79dacfacaf487965cbdfc643e5738 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Fri, 19 Feb 2016 14:34:17 +0100
Subject: [PATCH] Make the module autoloader to register class aliases for the
 classes that are migrated to namespaces, so that they are still accessible
 via the old name. This keeps everything working while migrating the classes.

---
 lib/_autoload_modules.php | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/lib/_autoload_modules.php b/lib/_autoload_modules.php
index 773e5e1f3..77bfd81d8 100644
--- a/lib/_autoload_modules.php
+++ b/lib/_autoload_modules.php
@@ -12,6 +12,8 @@
  * Autoload function for SimpleSAMLphp modules following PSR-0.
  *
  * @param string $className Name of the class.
+ *
+ * TODO: this autoloader should be removed once everything has been migrated to namespaces.
  */
 function SimpleSAML_autoload_psr0($className)
 {
@@ -22,18 +24,30 @@ function SimpleSAML_autoload_psr0($className)
     }
 
     $modNameEnd = strpos($className, '_', $modulePrefixLength);
-    $module = substr($className, $modulePrefixLength, $modNameEnd - $modulePrefixLength);
-    $moduleClass = substr($className, $modNameEnd + 1);
+    $module     = substr($className, $modulePrefixLength, $modNameEnd - $modulePrefixLength);
+    $path       = explode('_', substr($className, $modNameEnd + 1));
 
     if (!SimpleSAML_Module::isModuleEnabled($module)) {
         return;
     }
 
-    $file = SimpleSAML_Module::getModuleDir($module).'/lib/'.str_replace('_', '/', $moduleClass).'.php';
-
+    $file = SimpleSAML_Module::getModuleDir($module).'/lib/'.join('/', $path).'.php';
     if (file_exists($file)) {
         require_once($file);
     }
+
+    if (!class_exists($className, false)) {
+        // the file exists, but the class is not defined. Is it using namespaces?
+        $nspath = join('\\', $path);
+        if (class_exists('SimpleSAML\Module\\'.$module.'\\'.$nspath)) {
+            // the class has been migrated, create an alias and warn about it
+            SimpleSAML_Logger::warning(
+                "The class '$className' is now using namespaces, please use 'SimpleSAML\\Module\\$module\\".
+                "$nspath' instead."
+            );
+            class_alias("SimpleSAML\\Module\\$module\\$nspath", $className);
+        }
+    }
 }
 
 
-- 
GitLab