Skip to content

Commit

Permalink
[ClassLoader] Support for autoloading include_path incl. tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Bjørnskov committed Aug 29, 2011
1 parent a746055 commit ae3b128
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
Expand Up @@ -38,6 +38,7 @@ static public function enable()
$loader->registerPrefixFallbacks($function[0]->getPrefixFallbacks());
$loader->registerNamespaces($function[0]->getNamespaces());
$loader->registerPrefixes($function[0]->getPrefixes());
$loader->useIncludePath($function[0]->getUseIncludePath());

$function[0] = $loader;
}
Expand Down
44 changes: 39 additions & 5 deletions src/Symfony/Component/ClassLoader/UniversalClassLoader.php
Expand Up @@ -41,6 +41,10 @@
* 'Swift_' => __DIR__.'/Swift',
* ));
*
*
* // to enable searching the include path (eg. for PEAR packages)
* $loader->useIncludePath(true);
*
* // activate the autoloader
* $loader->register();
*
Expand All @@ -60,6 +64,29 @@ class UniversalClassLoader
private $prefixes = array();
private $namespaceFallbacks = array();
private $prefixFallbacks = array();
private $useIncludePath = false;

/**
* Turns on searching the include for class files. Allows easy loading
* of installed PEAR packages
*
* @param Boolean $useIncludePath
*/
public function useIncludePath($useIncludePath)
{
$this->useIncludePath = $useIncludePath;
}

/**
* Can be used to check if the autoloader uses the include path to check
* for classes.
*
* @return Boolean
*/
public function getUseIncludePath()
{
return $this->useIncludePath;
}

/**
* Gets the configured namespaces.
Expand Down Expand Up @@ -219,47 +246,54 @@ public function findFile($class)
if (false !== $pos = strrpos($class, '\\')) {
// namespaced class name
$namespace = substr($class, 0, $pos);
$className = substr($class, $pos + 1);
$normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
foreach ($this->namespaces as $ns => $dirs) {
if (0 !== strpos($namespace, $ns)) {
continue;
}

foreach ($dirs as $dir) {
$className = substr($class, $pos + 1);
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (file_exists($file)) {
return $file;
}
}
}

foreach ($this->namespaceFallbacks as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (file_exists($file)) {
return $file;
}
}

} else {
// PEAR-like class name
$normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
foreach ($this->prefixes as $prefix => $dirs) {
if (0 !== strpos($class, $prefix)) {
continue;
}

foreach ($dirs as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (file_exists($file)) {
return $file;
}
}
}

foreach ($this->prefixFallbacks as $dir) {
$file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
$file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
if (file_exists($file)) {
return $file;
}
}
}

if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {
return $file;
}
}
}
@@ -0,0 +1,5 @@
<?php

class Foo
{
}
Expand Up @@ -37,6 +37,25 @@ public function getLoadClassTests()
);
}

public function testUseIncludePath()
{
$loader = new UniversalClassLoader();
$this->assertFalse($loader->getUseIncludePath());

$this->assertEquals(null, $loader->findFile('Foo'));

$includePath = get_include_path();

$loader->useIncludePath(true);
$this->assertTrue($loader->getUseIncludePath());

set_include_path(__DIR__.'/Fixtures/includepath' . PATH_SEPARATOR . $includePath);

$this->assertEquals(__DIR__.'/Fixtures/includepath/Foo.php', $loader->findFile('Foo'));

set_include_path($includePath);
}

/**
* @dataProvider getLoadClassFromFallbackTests
*/
Expand Down

0 comments on commit ae3b128

Please sign in to comment.