Skip to content

check included files for defined classes before include #3369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
45 changes: 44 additions & 1 deletion autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,38 @@ public static function load($class)
}//end load()


/**
* Determine which class was loaded from path
*
* @param array $classesBeforeLoad The classes/interfaces/traits before the file was included.
* @param string $path The path that already has been included.
*
* @return string The fully qualified name of the class in the loaded file.
*/
private static function determinePathClass(
array $classesBeforeLoad, $path
) {
$alreadyIncluded = in_array($path, get_included_files(), true);
if ($alreadyIncluded === false) {
return null;
}

$name = pathinfo($path, PATHINFO_FILENAME);
$declared = preg_grep(
'(\\\\'.preg_quote($name).'$)',
$classesBeforeLoad['classes']
);
foreach ($declared as $className) {
$reflection = new \ReflectionClass($className);
$classFileName = $reflection->getFileName();
if ($classFileName === $path) {
return $className;
}
}

}//end determinePathClass()


/**
* Includes a file and tracks what class or interface was loaded as a result.
*
Expand All @@ -166,6 +198,12 @@ public static function loadFile($path)
'traits' => get_declared_traits(),
];

$className = self::determinePathClass($classesBeforeLoad, $path);

if ($className !== null) {
goto determined_className;
}

include $path;

$classesAfterLoad = [
Expand All @@ -174,7 +212,12 @@ public static function loadFile($path)
'traits' => get_declared_traits(),
];

$className = self::determineLoadedClass($classesBeforeLoad, $classesAfterLoad);
$className = self::determineLoadedClass(
$classesBeforeLoad,
$classesAfterLoad
);

determined_className:

self::$loadedClasses[$path] = $className;
self::$loadedFiles[$className] = $path;
Expand Down