From bb1abe676a8dabf64901c5d095901a0f93f6a987 Mon Sep 17 00:00:00 2001
From: Tim van Dijen <tvdijen@gmail.com>
Date: Tue, 3 May 2022 16:56:04 +0200
Subject: [PATCH] Cleanup: autoloader files (#1635)

---
 composer.json                  | 14 +++++++----
 lib/_autoload.php              | 22 ----------------
 lib/_autoload_modules.php      | 46 ----------------------------------
 tests/_autoload_modules.php    | 45 ---------------------------------
 tests/routers/configLoader.php |  2 ++
 www/_include.php               |  3 ---
 6 files changed, 11 insertions(+), 121 deletions(-)
 delete mode 100644 lib/_autoload.php
 delete mode 100644 lib/_autoload_modules.php
 delete mode 100644 tests/_autoload_modules.php

diff --git a/composer.json b/composer.json
index 88dc097a2..bce73a4b0 100644
--- a/composer.json
+++ b/composer.json
@@ -21,9 +21,14 @@
     ],
     "autoload": {
         "psr-4": {
-            "SimpleSAML\\": "lib/SimpleSAML"
-        },
-        "files": ["lib/_autoload_modules.php"]
+            "SimpleSAML\\": "lib/SimpleSAML",
+            "SimpleSAML\\Module\\admin\\": "modules/admin/lib",
+            "SimpleSAML\\Module\\core\\": "modules/core/lib",
+            "SimpleSAML\\Module\\cron\\": "modules/cron/lib",
+            "SimpleSAML\\Module\\exampleauth\\": "modules/exampleauth/lib",
+            "SimpleSAML\\Module\\multiauth\\": "modules/multiauth/lib",
+            "SimpleSAML\\Module\\saml\\": "modules/saml/lib"
+        }
     },
     "autoload-dev": {
         "psr-4": {
@@ -34,8 +39,7 @@
             "SimpleSAML\\Test\\Module\\exampleauth\\": ["tests/lib/SimpleSAML/modules/exampleauth/lib"],
             "SimpleSAML\\Test\\Module\\multiauth\\": ["tests/lib/SimpleSAML/modules/multiauth/lib"],
             "SimpleSAML\\Test\\Module\\saml\\": ["tests/lib/SimpleSAML/modules/saml/lib"]
-        },
-        "files": ["tests/_autoload_modules.php"]
+        }
     },
     "require": {
         "php": ">=7.4 || ^8.0",
diff --git a/lib/_autoload.php b/lib/_autoload.php
deleted file mode 100644
index 4eabbc022..000000000
--- a/lib/_autoload.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-/**
- * This file is a backwards compatible autoloader for SimpleSAMLphp.
- * Loads the Composer autoloader.
- *
- * @package SimpleSAMLphp
- */
-
-declare(strict_types=1);
-
-// SSP is loaded as a separate project
-if (file_exists(dirname(dirname(__FILE__)) . '/vendor/autoload.php')) {
-    require_once dirname(dirname(__FILE__)) . '/vendor/autoload.php';
-} else {
-    // SSP is loaded as a library
-    if (file_exists(dirname(dirname(__FILE__)) . '/../../autoload.php')) {
-        require_once dirname(dirname(__FILE__)) . '/../../autoload.php';
-    } else {
-        throw new Exception('Unable to load Composer autoloader');
-    }
-}
diff --git a/lib/_autoload_modules.php b/lib/_autoload_modules.php
deleted file mode 100644
index 5d98c4dda..000000000
--- a/lib/_autoload_modules.php
+++ /dev/null
@@ -1,46 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/**
- * This file registers an autoloader for SimpleSAMLphp modules.
- *
- * @package SimpleSAMLphp
- */
-
-/**
- * Autoload function for SimpleSAMLphp modules following PSR-4.
- *
- * @param string $className Name of the class.
- */
-function sspmodAutoloadPSR4(string $className): void
-{
-    $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('sspmodAutoloadPSR4');
diff --git a/tests/_autoload_modules.php b/tests/_autoload_modules.php
deleted file mode 100644
index eae6153bc..000000000
--- a/tests/_autoload_modules.php
+++ /dev/null
@@ -1,45 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/**
- * This file registers an autoloader for test classes used by SimpleSAMLphp modules unit tests.
- */
-
-/**
- * Autoload function for SimpleSAMLphp modules test classes following PSR-4.
- * Module test classes have namespaces like SimpleSAML\Test\Module\<moduleName>\Auth\Process
- *
- * @param string $className Name of the class.
- */
-function sspmodTestClassAutoloadPSR4(string $className): void
-{
-    $elements = explode('\\', $className);
-    if ($elements[0] === '') {
-        // class name starting with /, ignore
-        array_shift($elements);
-    }
-    if (count($elements) < 5) {
-        return; // it can't be a module test class
-    }
-    if (array_shift($elements) !== 'SimpleSAML') {
-        return; // the first element is not "SimpleSAML"
-    }
-    if (array_shift($elements) !== 'Test') {
-        return; // the second element is not "test"
-    }
-    if (array_shift($elements) !== 'Module') {
-        return; // the third element is not "module"
-    }
-
-    // this is a SimpleSAMLphp module test class following PSR-4
-    $module = array_shift($elements);
-    $moduleTestDir = __DIR__  . '/modules/' . $module;
-    $file = $moduleTestDir . '/lib/' . implode('/', $elements) . '.php';
-
-    if (file_exists($file)) {
-        require_once($file);
-    }
-}
-
-spl_autoload_register('sspmodTestClassAutoloadPSR4');
diff --git a/tests/routers/configLoader.php b/tests/routers/configLoader.php
index f239c902b..5fa2b8888 100644
--- a/tests/routers/configLoader.php
+++ b/tests/routers/configLoader.php
@@ -2,6 +2,8 @@
 
 declare(strict_types=1);
 
+namespace SimpleSAML\Test;
+
 use SimpleSAML\Configuration;
 
 /*
diff --git a/www/_include.php b/www/_include.php
index 893f1fbf3..8f95031bc 100644
--- a/www/_include.php
+++ b/www/_include.php
@@ -1,8 +1,5 @@
 <?php
 
-// initialize the autoloader
-require_once(dirname(dirname(__FILE__)) . '/lib/_autoload.php');
-
 use SAML2\Compat\ContainerSingleton;
 use SimpleSAML\Compat\SspContainer;
 use SimpleSAML\Configuration;
-- 
GitLab