更新:由于这个答案变得有点流行,我创建了一个 packagegist 包来简化事情。它基本上包含了我在这里描述的内容,无需自己添加类或手动配置$appRoot。它最终可能支持的不仅仅是 PSR-4。
可以在此处找到该软件包:haydenpierce/class-finder。
$ composer require haydenpierce/class-finder
在 README 文件中查看更多信息。
我对这里的任何解决方案都不满意,所以我最终建立了我的班级来处理这个问题。 此解决方案要求您是:
简而言之,这个类试图根据您在composer.json 中定义的命名空间来确定类在文件系统上的实际位置。例如,在命名空间Backup\Test 中定义的类可以在/home/hpierce/BackupApplicationRoot/src/Test 中找到。这是可以信任的,因为将目录结构映射到命名空间是required by PSR-4:
“命名空间前缀”之后的连续子命名空间名称
对应于“基本目录”中的子目录,其中
命名空间分隔符表示目录分隔符。子目录
名称必须与子命名空间名称的大小写匹配。
您可能需要调整appRoot 以指向包含composer.json 的目录。
<?php
namespace Backup\Util;
class ClassFinder
{
//This value should be the directory that contains composer.json
const appRoot = __DIR__ . "/../../";
public static function getClassesInNamespace($namespace)
{
$files = scandir(self::getNamespaceDirectory($namespace));
$classes = array_map(function($file) use ($namespace){
return $namespace . '\\' . str_replace('.php', '', $file);
}, $files);
return array_filter($classes, function($possibleClass){
return class_exists($possibleClass);
});
}
private static function getDefinedNamespaces()
{
$composerJsonPath = self::appRoot . 'composer.json';
$composerConfig = json_decode(file_get_contents($composerJsonPath));
return (array) $composerConfig->autoload->{'psr-4'};
}
private static function getNamespaceDirectory($namespace)
{
$composerNamespaces = self::getDefinedNamespaces();
$namespaceFragments = explode('\\', $namespace);
$undefinedNamespaceFragments = [];
while($namespaceFragments) {
$possibleNamespace = implode('\\', $namespaceFragments) . '\\';
if(array_key_exists($possibleNamespace, $composerNamespaces)){
return realpath(self::appRoot . $composerNamespaces[$possibleNamespace] . implode('/', $undefinedNamespaceFragments));
}
array_unshift($undefinedNamespaceFragments, array_pop($namespaceFragments));
}
return false;
}
}