From 748daa08ca671265e66ecc5121f93f3947dba8fa Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Fri, 19 Feb 2016 13:30:10 +0100
Subject: [PATCH] Add support for the new module namespace in
 SimpleSAML_Module::resolveClass().

---
 lib/SimpleSAML/Module.php | 30 ++++++++++++++++++++++--------
 1 file changed, 22 insertions(+), 8 deletions(-)

diff --git a/lib/SimpleSAML/Module.php b/lib/SimpleSAML/Module.php
index 4868c5694..be9cdbbdb 100644
--- a/lib/SimpleSAML/Module.php
+++ b/lib/SimpleSAML/Module.php
@@ -142,17 +142,31 @@ class SimpleSAML_Module
         assert('is_string($subclass) || is_null($subclass)');
 
         $tmp = explode(':', $id, 2);
-        if (count($tmp) === 1) {
+        if (count($tmp) === 1) { // no module involved
             $className = $tmp[0];
-        } else {
-            $className = 'sspmod_'.$tmp[0].'_'.$type.'_'.$tmp[1];
+            if (!class_exists($className)) {
+                throw new Exception("Could not resolve '$id': no class named '$className'.");
+            }
+        } else { // should be a module
+            // make sure empty types are handled correctly
+            $type = (empty($type)) ? '_' : '_'.$type.'_';
+
+            // check for the old-style class names
+            $className = 'sspmod_'.$tmp[0].$type.$tmp[1];
+
+            if (!class_exists($className)) {
+                // check for the new-style class names, using namespaces
+                $type = str_replace('_', '\\', $type);
+                $newClassName = 'SimpleSAML\Module\\'.$tmp[0].$type.$tmp[1];
+
+                if (!class_exists($newClassName)) {
+                    throw new Exception("Could not resolve '$id': no class named '$className' or '$newClassName'.");
+                }
+                $className = $newClassName;
+            }
         }
 
-        if (!class_exists($className)) {
-            throw new Exception(
-                'Could not resolve \''.$id.'\': No class named \''.$className.'\'.'
-            );
-        } elseif ($subclass !== null && !is_subclass_of($className, $subclass)) {
+        if ($subclass !== null && !is_subclass_of($className, $subclass)) {
             throw new Exception(
                 'Could not resolve \''.$id.'\': The class \''.$className.'\' isn\'t a subclass of \''.$subclass.'\'.'
             );
-- 
GitLab