haydenpierce / class-finder
一个库,可以提供指定命名空间中类的列表
Requires
- php: >=5.3
- ext-json: *
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ~9.0
This package is not auto-updated.
Last update: 2024-09-22 23:47:32 UTC
README
一个简单的工具,用于识别给定命名空间中的类。
这个包是对Stack Overflow上一个答案的改进实现,并提供了一些额外的功能,减少了配置需求。
要求
- 应用程序使用Composer。
- 类可以用Composer自动加载。
- PHP >= 5.3.0
安装
通过Composer要求安装。
composer require haydenpierce/class-finder
注意:目前不支持其他安装方法,因为需要Composer来管理ClassFinder识别的自动加载类。
支持的自动加载方法
方法 | 支持 | 使用ClassFinder::RECURSIVE_MODE |
---|---|---|
PSR-4 | ✔️ | ✔️ |
PSR-0 | ❌️ | ❌️ | |
类映射 | ✔️ | ✔️ |
文件 | ✔️^ | ❌️** |
\^ 实验。
* 计划。
** 不计划。如果你需要这个功能,请提出问题。
示例
标准模式
<?php
require_once __DIR__ . '/vendor/autoload.php';
ClassFinder::disablePSR4Vendors(); // Optional; see performance notes below
$classes = ClassFinder::getClassesInNamespace('TestApp1\Foo');
/**
* array(
* 'TestApp1\Foo\Bar',
* 'TestApp1\Foo\Baz',
* 'TestApp1\Foo\Foo'
* )
*/
var_dump($classes);
递归模式
(自v0.3-beta版起可用)
<?php
require_once __DIR__ . '/vendor/autoload.php';
ClassFinder::disablePSR4Vendors(); // Optional; see performance notes below
$classes = ClassFinder::getClassesInNamespace('TestApp1\Foo', ClassFinder::RECURSIVE_MODE);
/**
* array(
* 'TestApp1\Foo\Bar',
* 'TestApp1\Foo\Baz',
* 'TestApp1\Foo\Foo',
* 'TestApp1\Foo\Box\Bar',
* 'TestApp1\Foo\Box\Baz',
* 'TestApp1\Foo\Box\Foo',
* 'TestApp1\Foo\Box\Lon\Bar',
* 'TestApp1\Foo\Box\Lon\Baz',
* 'TestApp1\Foo\Box\Lon\Foo',
* )
*/
var_dump($classes);
性能
大多数应用程序和库都依赖于PSR-4来自动加载类。为了检测这些类,ClassFinder会扫描应用程序的文件,检查各种目录中的类。默认情况下,ClassFinder还会扫描vendor目录中的第三方类,以定位提供的命名空间中可能存在的类。如果你的应用程序有大量的第三方代码,这可能会非常慢。你可以使用ClassFinder::disablePSR4Vendors()
来提前忽略这些类 - 当调用此方法时,随后的ClassFinder::getClassesInNamespace()
调用将跳过vendor目录中的任何扫描,并且只扫描在composer.json中指定的用户定义的命名空间。
附加文档
异常:
当识别到常见的配置错误时,会抛出以下异常
内部
欢迎为该项目做出贡献;请阅读以下关于如何进行测试的文档。这些信息不是在项目中使用ClassFinder包所必需的。
未来工作
警告:在1.0.0之前,预计修复程序将不会回滚到旧版本。在次要版本0.X.Y中,可能会引入不兼容的更改,其中X发生变化。
以下是一份关于ClassFinder的计划或提议的想法列表;欢迎合并请求添加任何这些功能。
支持
psr0
~~
ClassFinder::getClassesInNamespace('TestApp1\Foo', ClassFinder::RECURSIVE_MODE)
. 提供多级命名空间的类。~~ (已包含在v0.3-beta版中)ClassFinder::getClassesImplementingInterface('TestApp1\Foo', 'TestApp1\FooInterface', ClassFinder::RECURSIVE_MODE)
. 过滤类,仅包括实现特定命名空间的类。ClassFinder::debugRenderReport('TestApp1\Foo\Baz')
解决由于命名空间中存在错误、缺少目录等导致的“找不到类”错误的指导。将打印一个HTML报告。不打算用于生产,但适用于调试。