Skip to content

Commit

Permalink
Fix locating files for classes without namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Apr 5, 2012
1 parent 7d97f17 commit 64ce1d4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Metadata/Driver/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(array $dirs)
{
$this->dirs = $dirs;
}

public function getDirs()
{
return $this->dirs;
Expand All @@ -19,11 +19,12 @@ public function getDirs()
public function findFileForClass(\ReflectionClass $class, $extension)
{
foreach ($this->dirs as $prefix => $dir) {
if (0 !== strpos($class->getNamespaceName(), $prefix)) {
if ('' !== $prefix && 0 !== strpos($class->getNamespaceName(), $prefix)) {
continue;
}

$path = $dir.'/'.str_replace('\\', '.', substr($class->getName(), strlen($prefix)+1)).'.'.$extension;
$len = '' === $prefix ? 0 : strlen($prefix) + 1;
$path = $dir.'/'.str_replace('\\', '.', substr($class->getName(), $len)).'.'.$extension;
if (file_exists($path)) {
return $path;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Metadata/Tests/Driver/FileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ public function testFindFileForClass()
$ref = new \ReflectionClass('Metadata\Tests\Driver\Fixture\C\SubDir\C');
$this->assertEquals(realpath(__DIR__.'/Fixture/C/SubDir.C.yml'), realpath($locator->findFileForClass($ref, 'yml')));
}

public function testFindFileForGlobalNamespacedClass()
{
$locator = new FileLocator(array(
'' => __DIR__.'/Fixture/D',
));

require_once __DIR__.'/Fixture/D/D.php';
$ref = new \ReflectionClass('D');
$this->assertEquals(realpath(__DIR__.'/Fixture/D/D.yml'), realpath($locator->findFileForClass($ref, 'yml')));
}
}
3 changes: 3 additions & 0 deletions tests/Metadata/Tests/Driver/Fixture/D/D.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class D { }
Empty file.

0 comments on commit 64ce1d4

Please sign in to comment.