donquixote / hasty-reflection-parser
0.0.0.alpha.4
2016-01-04 01:04 UTC
Requires
- php: >=5.5.0
- donquixote/hasty-php-ast: ~0.0
- donquixote/hasty-php-parser: ~0.0
- donquixote/hasty-reflection-common: ~0.0
This package is auto-updated.
Last update: 2024-09-24 13:55:58 UTC
README
hasty-reflection-parser
一个解析PHP代码以生成反射对象的库。
它被称为“hasty”,因为它跳过了很多内容,省略并忽略了很多东西,只关注通常需要的内容,例如注解发现。
目前它忽略了变量声明、常规语句、函数调用、对象变量、函数声明等。
使用场景
它适用于类似Doctrine或Drupal 8中的注解发现等。
部分库(作为依赖项)
hasty-php-ast
抽象语法树。这是PHP的不完整表示。
hasty-php-parser
解析器。将PHP字符串转换为AST图。基于token_get_all()
。
hasty-reflection-common
类似于核心反射API的类和接口。包含一个使用核心反射而不是解析的工作“原生”实现。
hasty-reflection-parser
使用解析器生成反射对象。
如何使用
首先,选择ClassIndexInterface
的多种实现之一。例如。
// Use native reflection and native class loader. This means, all files will be *really* included in PHP. $classIndex = new ClassIndex_Native(); // Files will be parsed AND included. $classIndex = ClassIndex_Ast::createSemiNative(); // Class files will be parsed, and NOT included. // Using the currently active Composer class loader/finder. Obviously you need to make sure to get the path right. $composerClassLoader = include dirname(dirname(__DIR__)) . '/vendor/autoload.php'; $classIndex = ClassIndex_Ast::createWithClassLoader(new ClassLoader_Composer($composerClassLoader));
现在您可以按需获取类反射对象。
$classReflection = $classIndex->classGetReflection(C::class); print $classReflection->getDocComment();
ClassIndex确保每个类/接口/特质/方法只有一个反射对象。
assert($classIndex->classGetReflection(C::class) === $classIndex->classGetReflection(C::class)); print $classReflection->getDocComment();
性能
这个应该相当快,因为解析器跳过了很多东西。对于长方法或函数体,它将简单地快速跳过令牌,只计算开闭括号!
它目前无法做到删除类体PHP。这是因为它是为了一个我们真正感兴趣的类体的用例而制作的。
然而,类体最初会以快速模式读取,以便稍后如果需要则拾取。(这是某些类的“懒”参数)。
从token_get_all()
得到的令牌数组永远不会被切割或操作,这意味着PHP只需要传递数组指针,而不需要创建数组的不同版本。
也不会记住令牌数组的长度,而是以一个唯一的符号终止。
此库的状态
这非常新鲜。也许在不久的将来,一些名称会发生变化!