From 1408e1f931f1db7174f0cad37fd83a8341013b72 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jaime=20Pe=CC=81rez=20Crespo?= <jaime.perez@uninett.no>
Date: Wed, 22 Nov 2017 13:43:51 +0100
Subject: [PATCH] Move the PSR* autoload functions outside of
 SimpleSAML\Module.

Doing so allows us to mock the class. Otherwise, the _autoload_module.php is always called first, and when it tries to register the functions from that class, it automatically autoloads it, making it impossible to mock it afterwards.
---
 lib/SimpleSAML/Module.php | 83 -------------------------------------
 lib/_autoload_modules.php | 87 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 85 insertions(+), 85 deletions(-)

diff --git a/lib/SimpleSAML/Module.php b/lib/SimpleSAML/Module.php
index 0492b0aae..6c9b3c6e0 100644
--- a/lib/SimpleSAML/Module.php
+++ b/lib/SimpleSAML/Module.php
@@ -27,89 +27,6 @@ class Module
     public static $module_info = array();
 
 
-    /**
-     * Autoload function for SimpleSAMLphp modules following PSR-0.
-     *
-     * @param string $className Name of the class.
-     *
-     * @deprecated This method will be removed in SSP 2.0.
-     *
-     * TODO: this autoloader should be removed once everything has been migrated to namespaces.
-     */
-    public static function autoloadPSR0($className)
-    {
-        $modulePrefixLength = strlen('sspmod_');
-        $classPrefix = substr($className, 0, $modulePrefixLength);
-        if ($classPrefix !== 'sspmod_') {
-            return;
-        }
-
-        $modNameEnd = strpos($className, '_', $modulePrefixLength);
-        $module = substr($className, $modulePrefixLength, $modNameEnd - $modulePrefixLength);
-        $path = explode('_', substr($className, $modNameEnd + 1));
-
-        if (!self::isModuleEnabled($module)) {
-            return;
-        }
-
-        $file = self::getModuleDir($module).'/lib/'.join('/', $path).'.php';
-        if (!file_exists($file)) {
-            return;
-        }
-        require_once($file);
-
-        if (!class_exists($className, false) && !interface_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) ||
-                interface_exists('SimpleSAML\Module\\'.$module.'\\'.$nspath)
-            ) {
-                // the class has been migrated, create an alias and warn about it
-                \SimpleSAML\Logger::warning(
-                    "The class or interface '$className' is now using namespaces, please use 'SimpleSAML\\Module\\".
-                    $module."\\".$nspath."' instead."
-                );
-                class_alias("SimpleSAML\\Module\\$module\\$nspath", $className);
-            }
-        }
-    }
-
-
-    /**
-     * Autoload function for SimpleSAMLphp modules following PSR-4.
-     *
-     * @param string $className Name of the class.
-     */
-    public static function autoloadPSR4($className)
-    {
-        $elements = explode('\\', $className);
-        if ($elements[0] === '') { // class name starting with /, ignore
-            array_shift($elements);
-        }
-        if (count($elements) < 4) {
-            return; // it can't be a module
-        }
-        if (array_shift($elements) !== 'SimpleSAML') {
-            return; // the first element is not "SimpleSAML"
-        }
-        if (array_shift($elements) !== 'Module') {
-            return; // the second element is not "module"
-        }
-
-        // this is a SimpleSAMLphp module following PSR-4
-        $module = array_shift($elements);
-        if (!self::isModuleEnabled($module)) {
-            return; // module not enabled, avoid giving out any information at all
-        }
-
-        $file = self::getModuleDir($module).'/lib/'.implode('/', $elements).'.php';
-
-        if (file_exists($file)) {
-            require_once($file);
-        }
-    }
-
-
     /**
      * Retrieve the base directory for a module.
      *
diff --git a/lib/_autoload_modules.php b/lib/_autoload_modules.php
index 1cbe07459..9a2c753f7 100644
--- a/lib/_autoload_modules.php
+++ b/lib/_autoload_modules.php
@@ -69,6 +69,89 @@ function temporaryLoader($class)
     }
 }
 
+
+/**
+ * Autoload function for SimpleSAMLphp modules following PSR-0.
+ *
+ * @param string $className Name of the class.
+ *
+ * @deprecated This method will be removed in SSP 2.0.
+ *
+ * TODO: this autoloader should be removed once everything has been migrated to namespaces.
+ */
+function sspmodAutoloadPSR0($className)
+{
+    $modulePrefixLength = strlen('sspmod_');
+    $classPrefix = substr($className, 0, $modulePrefixLength);
+    if ($classPrefix !== 'sspmod_') {
+        return;
+    }
+
+    $modNameEnd = strpos($className, '_', $modulePrefixLength);
+    $module = substr($className, $modulePrefixLength, $modNameEnd - $modulePrefixLength);
+    $path = explode('_', substr($className, $modNameEnd + 1));
+
+    if (!\SimpleSAML\Module::isModuleEnabled($module)) {
+        return;
+    }
+
+    $file = \SimpleSAML\Module::getModuleDir($module).'/lib/'.join('/', $path).'.php';
+    if (!file_exists($file)) {
+        return;
+    }
+    require_once($file);
+
+    if (!class_exists($className, false) && !interface_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) ||
+            interface_exists('SimpleSAML\Module\\'.$module.'\\'.$nspath)
+        ) {
+            // the class has been migrated, create an alias and warn about it
+            \SimpleSAML\Logger::warning(
+                "The class or interface '$className' is now using namespaces, please use 'SimpleSAML\\Module\\".
+                $module."\\".$nspath."' instead."
+            );
+            class_alias("SimpleSAML\\Module\\$module\\$nspath", $className);
+        }
+    }
+}
+
+
+/**
+ * Autoload function for SimpleSAMLphp modules following PSR-4.
+ *
+ * @param string $className Name of the class.
+ */
+function sspmodAutoloadPSR4($className)
+{
+    $elements = explode('\\', $className);
+    if ($elements[0] === '') { // class name starting with /, ignore
+        array_shift($elements);
+    }
+    if (count($elements) < 4) {
+        return; // it can't be a module
+    }
+    if (array_shift($elements) !== 'SimpleSAML') {
+        return; // the first element is not "SimpleSAML"
+    }
+    if (array_shift($elements) !== 'Module') {
+        return; // the second element is not "module"
+    }
+
+    // this is a SimpleSAMLphp module following PSR-4
+    $module = array_shift($elements);
+    if (!\SimpleSAML\Module::isModuleEnabled($module)) {
+        return; // module not enabled, avoid giving out any information at all
+    }
+
+    $file = \SimpleSAML\Module::getModuleDir($module).'/lib/'.implode('/', $elements).'.php';
+
+    if (file_exists($file)) {
+        require_once($file);
+    }
+}
+
 spl_autoload_register("temporaryLoader");
-spl_autoload_register(array('SimpleSAML\Module', 'autoloadPSR0'));
-spl_autoload_register(array('SimpleSAML\Module', 'autoloadPSR4'));
+spl_autoload_register('sspmodAutoloadPSR0');
+spl_autoload_register('sspmodAutoloadPSR4');
-- 
GitLab