From 6cce0644779e41c2b33ba434a380392267f770d2 Mon Sep 17 00:00:00 2001
From: Adam Roses Wight <awight@wikimedia.org>
Date: Mon, 2 Sep 2013 10:45:31 -0700
Subject: [PATCH] Registry supports namespaces

Note that files with multiple namespaces (not a recommended practice) will
cause breakage.
---
 includes/registry.inc                    |    8 +++++++-
 modules/system/system.info               |    1 +
 modules/system/system.test               |   21 +++++++++++++++++++++
 modules/system/tests/NamespacedClass.php |    4 ++++
 4 files changed, 33 insertions(+), 1 deletions(-)
 create mode 100644 modules/system/tests/NamespacedClass.php

diff --git a/includes/registry.inc b/includes/registry.inc
index f6c81eb..16385f1 100644
--- a/includes/registry.inc
+++ b/includes/registry.inc
@@ -161,8 +161,14 @@ function _registry_parse_files($files) {
  *   (optional) Weight of the module.
  */
 function _registry_parse_file($filename, $contents, $module = '', $weight = 0) {
+  $namespace = '';
+  // We support one namespace per file
+  if (preg_match('/^<\?(?:php)?\s*(?:declare[^;]+;\s*)?namespace\s+\\\\?([a-zA-Z0-9_\\\\]+)/ms', $contents, $matches)) {
+    $namespace = $matches[1] . '\\';
+  }
   if (preg_match_all('/^\s*(?:abstract|final)?\s*(class|interface)\s+([a-zA-Z0-9_]+)/m', $contents, $matches)) {
-    foreach ($matches[2] as $key => $name) {
+    foreach ($matches[2] as $key => &$name) {
+      $name = $namespace . $name;
       db_merge('registry')
         ->key(array(
           'name' => $name,
diff --git a/modules/system/system.info b/modules/system/system.info
index 28abf66..e50171c 100644
--- a/modules/system/system.info
+++ b/modules/system/system.info
@@ -9,5 +9,6 @@ files[] = system.queue.inc
 files[] = system.tar.inc
 files[] = system.updater.inc
 files[] = system.test
+files[] = tests/NamespacedClass.php
 required = TRUE
 configure = admin/config/system
diff --git a/modules/system/system.test b/modules/system/system.test
index 99e0cbe..79c9b2f 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -2712,3 +2712,24 @@ class TokenScanTest extends DrupalWebTestCase {
   }
 }
 
+/**
+ * Test registry autoloader
+ */
+class AutoloadTest extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Autoloader and registry',
+      'description' => 'Check that classes are correctly inserted into the registry and are discoverable by the autoloader.',
+      'group' => 'System',
+    );
+  }
+
+  function testGlobalNamespace() {
+    $this->assertTrue(class_exists('EntityFieldQuery'), 'Class with global namespace can be autoloaded.');
+  }
+
+  function testNamespacedClass() {
+    $this->assertTrue(class_exists('foo\bar\NamespacedClass'), 'Namespaced class can be autoloaded.');
+  }
+}
diff --git a/modules/system/tests/NamespacedClass.php b/modules/system/tests/NamespacedClass.php
new file mode 100644
index 0000000..b27aedb
--- /dev/null
+++ b/modules/system/tests/NamespacedClass.php
@@ -0,0 +1,4 @@
+<?php namespace foo\bar;
+
+class NamespacedClass {
+}
-- 
1.7.2.5

