Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,20 +251,19 @@ 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';

// NOTE: Cannot use `foreach (static::$psr0 as list($prefix, $path))`
// 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;
}
Expand Down
35 changes: 35 additions & 0 deletions tests/AutoloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class AutoloadTest extends \PHPUnit_Framework_TestCase
{
/**
* @group psr4
* @covers Fedora::Autoloader::Autoload::addPsr4
**/
public function testAddPsr4()
Expand All @@ -24,6 +25,7 @@ public function testAddPsr4()
}

/**
* @group psr4
* @covers Fedora::Autoloader::Autoload::addPsr4
**/
public function testAddPsr4Order()
Expand All @@ -37,6 +39,7 @@ public function testAddPsr4Order()
}

/**
* @group classmap
* @covers Fedora::Autoloader::Autoload::addClassMap
**/
public function testAddClassMap()
Expand All @@ -52,6 +55,7 @@ public function testAddClassMap()
}

/**
* @group classmap
* @covers Fedora::Autoloader::Autoload::addClassMap
**/
public function testAddClassMapTemplate()
Expand All @@ -62,6 +66,7 @@ public function testAddClassMapTemplate()
}

/**
* @group classmap
* @covers Fedora::Autoloader::Autoload::addClassMap
**/
public function testAddClassMapLowerCase()
Expand All @@ -72,6 +77,7 @@ public function testAddClassMapLowerCase()
}

/**
* @group classmap
* @covers Fedora::Autoloader::Autoload::addClassMap
**/
public function testAddClassMapTemplateOrder()
Expand All @@ -85,6 +91,7 @@ public function testAddClassMapTemplateOrder()
}

/**
* @group classmap
* @covers Fedora::Autoloader::Autoload::addClassMap
**/
public function testAddClassMapTemplateOrderBis()
Expand All @@ -104,6 +111,7 @@ public function testAddClassMapTemplateOrderBis()
}

/**
* @group psr0
* @covers Fedora::Autoloader::Autoload::addIncludePath
**/
public function testAddIncludePath()
Expand All @@ -129,6 +137,7 @@ public function testAddIncludePath()
}

/**
* @group psr0
* @covers Fedora::Autoloader::Autoload::addPsr0
**/
public function testAddPsr0Simple()
Expand All @@ -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'));
}
}