summaryrefslogtreecommitdiffstats
path: root/php-fedora-autoloader-upstream.patch
blob: eb62aa6386a8fa4f8b8ac3a49827d143068d3c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
From 5b810402b041e69f67ad6b88d65f30e11a5b6683 Mon Sep 17 00:00:00 2001
From: Remi Collet <fedora@famillecollet.com>
Date: Fri, 21 Oct 2016 14:59:02 +0200
Subject: [PATCH] Handle namespaced classes with PSR-0, fix #5

---
 src/Autoload.php       |  9 ++++-----
 tests/AutoloadTest.php | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/src/Autoload.php b/src/Autoload.php
index 74e86cf..5d08c84 100644
--- a/src/Autoload.php
+++ b/src/Autoload.php
@@ -251,11 +251,11 @@ public static function findFile($class)
         // PSR-0
         if (count(static::$psr0)) {
             $pos = strrpos($class, '\\');
-            $file = '';
+            $file = $namespace = '';
             if ($pos) {
-                $namespace = substr($class, 0, $pos);
+                $namespace = substr($class, 0, $pos + 1);
                 $class = substr($class, $pos + 1);
-                $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
+                $file = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
             }
             $file .= str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
 
@@ -263,8 +263,7 @@ public static function findFile($class)
             //       for PHP < 5.5 compatibility.
             foreach (static::$psr0 as $psr0) {
                 list($prefix, $path) = $psr0;
-
-                if (empty($prefix) || 0 === strpos($class, $prefix)) {
+                if (empty($prefix) || 0 === strpos($namespace.$class, $prefix)) {
                     if (file_exists($path.$file)) {
                         return $path.$file;
                     }
diff --git a/tests/AutoloadTest.php b/tests/AutoloadTest.php
index e1e2783..087682c 100644
--- a/tests/AutoloadTest.php
+++ b/tests/AutoloadTest.php
@@ -14,6 +14,7 @@
 class AutoloadTest extends \PHPUnit_Framework_TestCase
 {
     /**
+     * @group psr4
      * @covers Fedora::Autoloader::Autoload::addPsr4
      **/
     public function testAddPsr4()
@@ -24,6 +25,7 @@ public function testAddPsr4()
     }
 
     /**
+     * @group psr4
      * @covers Fedora::Autoloader::Autoload::addPsr4
      **/
     public function testAddPsr4Order()
@@ -37,6 +39,7 @@ public function testAddPsr4Order()
     }
 
     /**
+     * @group classmap
      * @covers Fedora::Autoloader::Autoload::addClassMap
      **/
     public function testAddClassMap()
@@ -52,6 +55,7 @@ public function testAddClassMap()
     }
 
     /**
+     * @group classmap
      * @covers Fedora::Autoloader::Autoload::addClassMap
      **/
     public function testAddClassMapTemplate()
@@ -62,6 +66,7 @@ public function testAddClassMapTemplate()
     }
 
     /**
+     * @group classmap
      * @covers Fedora::Autoloader::Autoload::addClassMap
      **/
     public function testAddClassMapLowerCase()
@@ -72,6 +77,7 @@ public function testAddClassMapLowerCase()
     }
 
     /**
+     * @group classmap
      * @covers Fedora::Autoloader::Autoload::addClassMap
      **/
     public function testAddClassMapTemplateOrder()
@@ -85,6 +91,7 @@ public function testAddClassMapTemplateOrder()
     }
 
     /**
+     * @group classmap
      * @covers Fedora::Autoloader::Autoload::addClassMap
      **/
     public function testAddClassMapTemplateOrderBis()
@@ -104,6 +111,7 @@ public function testAddClassMapTemplateOrderBis()
     }
 
     /**
+     * @group psr0
      * @covers Fedora::Autoloader::Autoload::addIncludePath
      **/
     public function testAddIncludePath()
@@ -129,6 +137,7 @@ public function testAddIncludePath()
     }
 
     /**
+     * @group psr0
      * @covers Fedora::Autoloader::Autoload::addPsr0
      **/
     public function testAddPsr0Simple()
@@ -145,4 +154,30 @@ public function testAddPsr0Simple()
         $this->assertTrue(class_exists('One\\Two\\Foo'));
         $this->assertTrue(class_exists('One_Two\\Foo'));
     }
+
+    /**
+     * @group psr0
+     * @covers Fedora::Autoloader::Autoload::addPsr0
+     **/
+    public function testAddPsr0ns1()
+    {
+        $this->assertFalse(class_exists('One\\Two\\Foo'));
+
+        Autoload::addPsr0('One\\', __DIR__.'/fixtures/PSR0');
+
+        $this->assertTrue(class_exists('One\\Two\\Foo'));
+    }
+
+    /**
+     * @group psr0
+     * @covers Fedora::Autoloader::Autoload::addPsr0
+     **/
+    public function testAddPsr0ns2()
+    {
+        $this->assertFalse(class_exists('One\\Two\\Foo'));
+
+        Autoload::addPsr0('One\\Two\\', __DIR__.'/fixtures/PSR0');
+
+        $this->assertTrue(class_exists('One\\Two\\Foo'));
+    }
 }