Skip to content

Commit

Permalink
The autoloader now uses the composer autoloader (if available) to det…
Browse files Browse the repository at this point in the history
…ermine file paths for unknown files (ref #1259)
  • Loading branch information
gsherwood committed Jan 25, 2017
1 parent 8cda7ed commit b03ae89
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
class Autoload
{

/**
* The composer autoloader.
*
* @var Composer\Autoload\ClassLoader
*/
private static $composerAutoloader = null;

/**
* A mapping of file names to class names.
*
Expand Down Expand Up @@ -46,8 +53,20 @@ class Autoload
*/
public static function load($class)
{
// Include the composer autoloader if there is one, but unregister it
// as we need to include all files so we can figure out what
// the class/interface/trait name is.
if (self::$composerAutoloader === null) {
if (file_exists(__DIR__.'/../../autoload.php') === true) {
self::$composerAutoloader = include_once __DIR__.'/../../autoload.php';
self::$composerAutoloader->unregister();
} else {
self::$composerAutoloader = false;
}
}

$ds = DIRECTORY_SEPARATOR;
$path = null;
$path = false;

if (substr($class, 0, 16) === 'PHP_CodeSniffer\\') {
if (substr($class, 0, 22) === 'PHP_CodeSniffer\Tests\\') {
Expand All @@ -64,7 +83,12 @@ public static function load($class)
}
}

if ($path !== null && is_file($path) === true) {
// See if the composer autoloader knows where the class is.
if ($path === false && self::$composerAutoloader !== false) {
$path = self::$composerAutoloader->findFile($class);
}

if ($path !== false && is_file($path) === true) {
self::loadFile($path);
return true;
}
Expand All @@ -83,12 +107,18 @@ public static function load($class)
*/
public static function loadFile($path)
{
$path = realpath($path);
if ($path === false) {
return false;
}

if (isset(self::$loadedClasses[$path]) === true) {
return self::$loadedClasses[$path];
}

$classes = get_declared_classes();
$interfaces = get_declared_interfaces();
$traits = get_declared_traits();

include $path;

Expand All @@ -102,7 +132,17 @@ public static function loadFile($path)
}

if ($className === null) {
$newClasses = array_diff(get_declared_interfaces(), $classes);
$newClasses = array_reverse(array_diff(get_declared_traits(), $classes));
foreach ($newClasses as $name) {
if (isset(self::$loadedFiles[$name]) === false) {
$className = $name;
break;
}
}
}

if ($className === null) {
$newClasses = array_reverse(array_diff(get_declared_interfaces(), $classes));
foreach ($newClasses as $name) {
if (isset(self::$loadedFiles[$name]) === false) {
$className = $name;
Expand Down Expand Up @@ -182,7 +222,6 @@ public static function getLoadedFiles()

}//end class


// Register the autoloader before any existing autoloaders to ensure
// it gets a chance to hear about every autoload request, and record
// the file and class name for it.
Expand Down

0 comments on commit b03ae89

Please sign in to comment.