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

Find autoload #7

Merged
merged 3 commits into from
Nov 17, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 61 additions & 20 deletions php/getfilepath.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,67 @@
$class = $argv[1];
$file = $argv[2];
$rootpath = $argv[3];
$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);
}

require __DIR__ . '/FQCN.php';

$foundAutoLoader = false;

// First try default location
if(is_readable($autoloader)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey,

I have a question where did this $autoloader is assigned ? Did you missed / accidentally removed https://github.com/harikt/php-hyperclick/pull/7/files#diff-5ef8e23f048e9259d4783d28d2407b5dL5

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, you are right!

The default location should be used here, but was deleted by accident.

I have updated the PR

$foundAutoLoader = true;
if(getFilePath($rootpath . '/vendor/autoload.php', $class, $file)) {
exit(0);
}
}

// Try to find autoloader.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;
}