Skip to content
This repository has been archived by the owner on Aug 11, 2018. It is now read-only.

Commit

Permalink
Merge pull request #7 from veger/find_autoload
Browse files Browse the repository at this point in the history
Better way to find autoloader
  • Loading branch information
harikt authored Nov 17, 2016
2 parents cf838d9 + df88f8c commit 5175adb
Showing 1 changed file with 61 additions and 19 deletions.
80 changes: 61 additions & 19 deletions php/getfilepath.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,68 @@
$class = $argv[1];
$file = $argv[2];
$rootpath = $argv[3];

require __DIR__ . '/FQCN.php';

$foundAutoLoader = false;

// First try default location
$autoloader = $rootpath . '/vendor/autoload.php';
if (is_readable($autoloader)) {
$loader = require $autoloader;

require __DIR__ . '/FQCN.php';

$contents = file_get_contents($file);
$fqns = new Hkt\FQCN();
$useClasses = $fqns->getAllUseStatements($contents);
if (array_key_exists($class, $useClasses)) {
echo $loader->findFile($useClasses[$class]);
} else {
// Get current namespace
$namespace = $fqns->getNamespace();
if ($namespace) {
$class = $namespace . '\\' . $class;
}
echo $loader->findFile($class);
}
if(is_readable($autoloader)) {
$foundAutoLoader = true;
if(getFilePath($rootpath . '/vendor/autoload.php', $class, $file)) {
exit(0);
}
}

// Try to find autoload.php elsewhere in $rootpath
$dirIterator = new RecursiveDirectoryIterator($rootpath);
$reqIterator = new RecursiveIteratorIterator($dirIterator);
$regexIterator = new RegexIterator($reqIterator, '#/vendor/autoload.php$#i');
foreach($regexIterator as $autoloader) {
$foundAutoLoader = true;
if(getFilePath($autoloader, $class, $file)) {
exit(0);
}
}

if($foundAutoLoader) {
echo "Class $class not found.";
} else {
echo "Please make sure the composer vendor/autoload.php file exists in root of project and is readable.";
echo "Class $class not found, please make sure the composer vendor/autoload.php file exists in your project and is readable.";
}

function getFilePath($autoloader, $class, $file) {
if (!is_readable($autoloader)) {
return false;
}

$loader = require $autoloader;

$contents = file_get_contents($file);
$fqns = new Hkt\FQCN();
$useClasses = $fqns->getAllUseStatements($contents);
if (array_key_exists($class, $useClasses)) {
echo $loader->findFile($useClasses[$class]);
return true;
}

// Try using current namespace
$namespace = $fqns->getNamespace();
if ($namespace) {
$foundFile = $loader->findFile($namespace . '\\' . $class);
if($foundFile) {
echo $foundFile;
return true;
}
}

// Try using global namespace
$foundFile = $loader->findFile($class);
if($foundFile) {
echo $foundFile;
return true;
}

return false;
}

0 comments on commit 5175adb

Please sign in to comment.