hanneskod / classtools
从文件系统中查找、提取和处理类
1.2.1
2020-03-05 20:41 UTC
Requires
- php: >=7.1
- nikic/php-parser: ^4
- symfony/finder: ^4|^5
This package is auto-updated.
Last update: 2024-09-16 04:15:38 UTC
README
从文件系统中查找、提取和处理类。
安装
使用 composer 安装。作为 hanneskod/classtools 存在于 packagist 存储库中。从命令行使用
composer require hanneskod/classtools
使用迭代器
ClassIterator 消耗一个 symfony finder 并扫描文件以查找php类、接口和特性。
访问类映射
getClassMap()
返回一个类名到 SplFileInfo 对象的映射。
$finder = new Symfony\Component\Finder\Finder; $iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src')); // Print the file names of classes, interfaces and traits in 'src' foreach ($iter->getClassMap() as $classname => $splFileInfo) { echo $classname.': '.$splFileInfo->getRealPath(); }
查找语法错误
包含语法错误的源文件无法解析,因此无法检索包含的类信息。使用 getErrors()
读取遇到的错误列表。
$finder = new Symfony\Component\Finder\Finder; $iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src')); print_r($iter->getErrors());
遍历 ReflectionClass 对象
ClassIterator 还是一个 Traversable,在迭代时产生类名作为键和 ReflectionClass 对象作为值。
请注意,为了使用反射,在文件系统中找到的类必须包含在环境中。启用自动加载以动态从 ClassIterator 加载类。
$finder = new Symfony\Component\Finder\Finder(); $iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src')); // Enable reflection by autoloading found classes $iter->enableAutoloading(); // Print all classes, interfaces and traits in 'src' foreach ($iter as $class) { echo $class->getName(); }
基于类属性进行过滤
ClassIterator 可过滤,且过滤器可链式使用。
$finder = new Symfony\Component\Finder\Finder(); $iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src')); $iter->enableAutoloading(); // Print all Filter types (including the interface itself) foreach ($iter->type('hanneskod\classtools\Iterator\Filter') as $class) { echo $class->getName(); } // Print definitions in the Iterator namespace whose name contains 'Class' foreach ($iter->inNamespace('hanneskod\classtools\Iterator\Filter')->name('/type/i') as $class) { echo $class->getName(); } // Print implementations of the Filter interface foreach ($iter->type('hanneskod\classtools\Iterator\Filter')->where('isInstantiable') as $class) { echo $class->getName(); }
否定过滤器
过滤器也可以通过包装在 not()
方法调用中来进行否定。
$finder = new Symfony\Component\Finder\Finder(); $iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src')); $iter->enableAutoloading(); // Print all classes, interfaces and traits NOT instantiable foreach ($iter->not($iter->where('isInstantiable')) as $class) { echo $class->getName(); }
转换类
找到的类、接口和特性定义可以被转换并写入单个文件。
$finder = new Symfony\Component\Finder\Finder(); $iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src')); $iter->enableAutoloading(); // Print all found definitions in one snippet echo $iter->minimize(); // The same can be done using echo $iter->transform(new hanneskod\classtools\Transformer\MinimizingWriter);
使用转换器
将代码包裹在命名空间中
$reader = new Reader("<?php class Bar {}"); $writer = new Writer; $writer->apply(new Action\NamespaceWrapper('Foo')); // Outputs class Bar wrapped in namespace Foo echo $writer->write($reader->read('Bar'));