1 回答
TA贡献1810条经验 获得超5个赞
这$event->getComposer()->getPackage()->getAutoload()是我所知道的从 Composer 对象获取自动加载信息的唯一方法。我使用以下函数来处理 的结果getAutoload(),希望这些信息足以回答您的问题。
function parseAutoload(Event $event)
{
foreach ($event->getComposer()->getPackage()->getAutoload() as $std => $lookup) {
switch ($std) {
case "psr-0":
case "psr-4":
foreach ($lookup as $namespace => $paths) {
if (substr($namespace, -1) != "\\") {
$namespace .= "\\";
// TODO Maybe produce a warning?
}
if (!is_array($paths)) {
$paths = array($paths);
}
foreach ($paths as $path) {
if (substr($path, 0, -1) != DIRECTORY_SEPARATOR) {
$path .= DIRECTORY_SEPARATOR;
}
if (is_dir($path)) {
$directory = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator,
'/^' . preg_quote($path, DIRECTORY_SEPARATOR) . '(.+)\.php$/i',
RegexIterator::REPLACE);
$regex->replacement = '$1';
foreach ($regex as $file => $class) {
$class = "{$namespace}" . str_replace(DIRECTORY_SEPARATOR, "\\", $class);
echo "Found class '{$class}' at '{$file}'";
}
}
}
}
break;
case "classmap":
foreach ($lookup as $class => $path) {
echo "Found class '{$class}' at '{$path}'";
}
break;
case "files":
foreach ($lookup as $i => $path) {
if (is_readable($path)) {
$code = file_get_contents($path);
$tokens = token_get_all($code);
// File all the classes in the file.
for ($i = 2; $i < count($tokens); $i++) {
if ($tokens[$i - 2][0] == T_CLASS &&
$tokens[$i - 1][0] == T_WHITESPACE &&
$tokens[$i][0] == T_STRING
) {
$class = $tokens[$i][1];
echo "Found class '{$class}' at '{$path}'";
}
}
}
}
break;
default:
// TODO Maybe produce a warning?
}
}
}
- 1 回答
- 0 关注
- 170 浏览
添加回答
举报