dtl / class-to-file
0.5.2
2022-10-08 08:32 UTC
Requires
- php: ^7.4 || ^8.0
- psr/log: ^1.0 || ^2.0 || ^3.0
- symfony/filesystem: ^4.2 || ^5.0 || ^6.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.0
- friendsofphp/php-cs-fixer: ^3.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^6.0 || ^5.0
This package is auto-updated.
Last update: 2024-01-26 08:23:18 UTC
README
此库提供猜测/转换类名到文件路径和反向操作的设施。
它使用 composer 自动加载来猜测最佳候选者,使用 PSR-0 和 PSR-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 自动加载器,这通过 ChainFileToClass
和 ChainClassToFile
类支持,或者最简单的是通过 外观
$converter = ClassFileConverter::fromComposerAutoloaders([ $autoloader1, $autoloader2 ]);
贡献
此包是开源的,欢迎贡献!请随意在此存储库中打开一个拉取请求。
支持
- 在主要的 Phpactor 存储库中创建一个问题。
- 加入 Slack Symfony Devs 频道中的
#phpactor
通道。