uzbek/classtools

从文件系统中查找、提取和处理类

0.0.2 2022-08-27 19:44 UTC

This package is auto-updated.

Last update: 2024-09-22 14:51:39 UTC


README

Packagist Version Build Status Quality Score

https://github.com/hanneskod/classtools 分支

从文件系统中查找、提取和处理类。

安装

使用 composer 安装。在 packagist 仓库中存在 packagist。从命令行使用

composer require uzbek/classtools

使用迭代器

ClassIterator 消耗一个 symfony finder 并扫描文件以查找php类、接口和特性。

访问类映射

getClassMap() 返回一个类名到 SplFileInfo 对象的映射。

$finder = new Symfony\Component\Finder\Finder;
$iter = new Uzbek\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 Uzbek\ClassTools\Iterator\ClassIterator($finder->in('src'));

print_r($iter->getErrors());

遍历 ReflectionClass 对象

ClassIterator 也是一个 Traversable,在迭代时将类名作为键,将 ReflectionClass 对象作为值。

注意,为了使用反射,在文件系统中找到的类必须包含在环境中。启用自动加载以从 ClassIterator 动态加载类。

$finder = new Symfony\Component\Finder\Finder();
$iter = new Uzbek\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 Uzbek\ClassTools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

// Print all Filter types (including the interface itself)
foreach ($iter->type('uzbek\ClassTools\Iterator\Filter') as $class) {
    echo $class->getName();
}

// Print definitions in the Iterator namespace whose name contains 'Class'
foreach ($iter->inNamespace('uzbek\ClassTools\Iterator\Filter')->name('/type/i') as $class) {
    echo $class->getName();
}

// Print implementations of the Filter interface
foreach ($iter->type('uzbek\ClassTools\Iterator\Filter')->where('isInstantiable') as $class) {
    echo $class->getName();
}

否定过滤

也可以通过将它们包裹在 not() 方法调用中来否定过滤。

$finder = new Symfony\Component\Finder\Finder();
$iter = new Uzbek\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 Uzbek\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 Uzbek\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'));