Skip to content

Commit

Permalink
clean up testing situation, add path tracking for debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M. Jones committed Dec 29, 2011
1 parent 2ba0799 commit e4c60e0
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion config/default.php
Expand Up @@ -2,4 +2,4 @@
/**
* Package prefix for autoloader.
*/
$loader->addPrefix('Aura\Autoload\\', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src');
$loader->add('Aura\Autoload\\', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src');
2 changes: 1 addition & 1 deletion config/test.php
Expand Up @@ -2,4 +2,4 @@
/**
* Package prefix for autoloader.
*/
$loader->addPrefix('Aura\Autoload\\', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'tests');
$loader->add('Aura\Autoload\\', dirname(__DIR__) . DIRECTORY_SEPARATOR . 'tests');
41 changes: 23 additions & 18 deletions src/Aura/Autoload/Loader.php
Expand Up @@ -83,6 +83,8 @@ class Loader
*/
protected $mode = self::MODE_NORMAL;

protected $tried_paths;

/**
*
* Sets the autoloader operational mode.
Expand Down Expand Up @@ -298,7 +300,9 @@ public function load($spec)
return;
} else {
// yes, throw an exception
throw new Exception\NotFound($spec);
$message = $spec . PHP_EOL
. implode(PHP_EOL, $this->tried_paths);
throw new Exception\NotFound($message);
}
}

Expand All @@ -321,7 +325,16 @@ public function load($spec)
$this->loaded[$spec] = $file;
}

public function isDeclared($spec)
/**
*
* Tells if a class or interface exists.
*
* @param string $spec The class or interface.
*
* @return bool
*
*/
protected function isDeclared($spec)
{
return class_exists($spec, false)
|| interface_exists($spec, false);
Expand All @@ -344,6 +357,8 @@ public function find($spec)
return $this->classes[$spec];
}

$this->tried_paths = array();

// go through each of the path prefixes
foreach ($this->paths as $prefix => $paths) {

Expand All @@ -360,7 +375,10 @@ public function find($spec)
$ctf = $this->classToFile($spec);

// ... and go through each of the paths for the prefix
foreach ($paths as $path) {
foreach ($paths as $i => $path) {

// track which paths we have tried
$this->tried_paths[] = "#{$i}: {$path}";

// convert the remaining spec to a file name
$file = $path . DIRECTORY_SEPARATOR . $ctf;
Expand All @@ -373,26 +391,13 @@ public function find($spec)
}
}

// fall back to the include-path
return $this->findInclude($spec);
}

/**
*
* Finds a class in the include-path.
*
* @param string $spec The class or interface to find.
*
* @return mixed The path to the file, or false if not found.
*
*/
protected function findInclude($spec)
{
// fall back to the include path
$file = $this->classToFile($spec);
try {
$obj = new \SplFileObject($file, 'r', true);
} catch (\RuntimeException $e) {
$k = count($this->tried_paths);
$this->tried_paths[] = "#{$k}" . get_include_path();
return false;
}
$path = $obj->getRealPath();
Expand Down
3 changes: 3 additions & 0 deletions tests/Aura/Autoload/Bar/MockAutoloadRouterClass.php
@@ -0,0 +1,3 @@
<?php
namespace Aura\Autoload\Bar;
class MockAutoloadRouterClass {}
3 changes: 3 additions & 0 deletions tests/Aura/Autoload/Foo/MockAutoloadCliClass.php
@@ -0,0 +1,3 @@
<?php
namespace Aura\Autoload\Foo;
class MockAutoloadCliClass {}
12 changes: 6 additions & 6 deletions tests/Aura/Autoload/LoaderTest.php
Expand Up @@ -233,12 +233,12 @@ public function testSetClasses()

public function testSetPaths()
{
$class1 = 'Aura\Cli\MockAutoloadCliClass';
$class2 = 'Aura\Router\MockAutoloadRouterClass';
$class1 = 'Aura\Autoload\Foo\MockAutoloadCliClass';
$class2 = 'Aura\Autoload\Bar\MockAutoloadRouterClass';
$autoloader = new Loader;
$autoloader->setPaths(array(
'Aura\Cli\\', dirname(dirname(__DIR__)),
'Aura\Router\\', dirname(dirname(__DIR__))
'Aura\Autoload\Foo\\' => dirname(dirname(__DIR__)),
'Aura\Autoload\Bar\\' => dirname(dirname(__DIR__))
));
$autoloader->load($class1);
$autoloader->load($class2);
Expand All @@ -249,8 +249,8 @@ public function testSetPaths()
$this->assertSame($class1, $actual);

$expect = array(
$class1 => dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR . 'Aura/Cli/MockAutoloadCliClass.php',
$class2 => dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR . 'Aura/Router/MockAutoloadRouterClass.php',
$class1 => dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR . 'Aura/Autoload/Foo/MockAutoloadCliClass.php',
$class2 => dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR . 'Aura/Autoload/Bar/MockAutoloadRouterClass.php',
);

$actual = $autoloader->getLoaded();
Expand Down
3 changes: 0 additions & 3 deletions tests/Aura/Cli/MockAutoloadCliClass.php

This file was deleted.

3 changes: 0 additions & 3 deletions tests/Aura/Router/MockAutoloadRouterClass.php

This file was deleted.

0 comments on commit e4c60e0

Please sign in to comment.