dtl/class-to-file

此包已被废弃,不再维护。作者建议使用 phpactor/class-to-file 包。

库函数:将类名转换为文件路径,反之亦然

0.5.2 2022-10-08 08:32 UTC

README

CI Scrutinizer Code Quality

此库提供猜测/转换类名到文件路径和反向操作的设施。

它使用 composer 自动加载来猜测最佳候选者,使用 PSR-0PSR-4

用法

// require the composer autoloader for the class you want to investigate
$autoloader = require(__DIR__ . '/vendor/autoload.php');

// for file candidates
$converter = ClassFileConverter::fromComposerAutoloader($autoloader);
$candidates = $classToFile->classToFileCandidates(ClassName::fromString('Foobar\\Barfoo\\MyClass'));

echo (string) $candidates->empty(); // return true if there are no candidates
echo (string) $candidates->best(); // path to the "best" candidate

// or for class candidates
$candidates = $classToFile->fileToClassCandidates(FilePath::fromString('lib/Foobar/BarFoo/MyClass.php'));

Composer 是否提供类的文件路径?

是的,它提供了,但仅当类 存在 时。此类的一个目的是能够确定一个可能不存在的类的文件路径(例如,在新的文件中生成一个类)。

您可能还想这样做,并且不污染您的自动加载环境(遗憾的是,此库目前 会污染自动加载器,但未来可以避免)。

为什么要从文件名中确定类名呢?

很高兴您问了!当您想在空文件中生成一个空类时,这非常有用。

当单个自动加载器不够用时

在某些特殊情况下,您可能有一个项目,该项目拥有多个 composer 自动加载器,这通过 ChainFileToClassChainClassToFile 类支持,或者最简单的是通过 外观

$converter = ClassFileConverter::fromComposerAutoloaders([ $autoloader1, $autoloader2 ]);

贡献

此包是开源的,欢迎贡献!请随意在此存储库中打开一个拉取请求。

支持

  • 在主要的 Phpactor 存储库中创建一个问题。
  • 加入 Slack Symfony Devs 频道中的 #phpactor 通道。