brenoroosevelt / flex-fqcn-finder
灵活的FQCN查找器
2.0.0
2023-09-12 14:41 UTC
Requires
- php: ^8
- psr/simple-cache: ^1|^2|^3
Requires (Dev)
- phpstan/phpstan: ^0
- phpunit/phpunit: ^6 || ^9
- squizlabs/php_codesniffer: 3.5.*
Suggests
- symfony/cache: @stable
README
Flex FQCN Finder 允许您查找项目中所有可用的完全限定类名 (FQCN)。此包旨在灵活且可靠。
功能
- 查找类、特质和接口 (PSR-4 兼容);
- 在目录中搜索 (递归或不递归);
- 许多过滤选项;
- 缓存 (PSR-16 兼容)。
- 组合查找器。
- 从 Composer ClassMap 获取类。
要求
- 支持以下版本的 PHP:
^8
。 - 对于 PHP 7.*,请安装版本 1.0.0
安装
$ composer require brenoroosevelt/flex-fqcn-finder
用法
使用 FlexFqcnFinder\Fqcn
辅助器以您自己的方式应用过滤器、装饰器和组合。
<?php use FlexFqcnFinder\Fqcn; use FlexFqcnFinder\Filter\Filter; $recursive = true; $fqcns = Fqcn::new() ->addDirectory('/path/to/dir1', $recursive) ->addDirectory('/path/to/dir2', !$recursive) ->withFilter( Filter::by() // or: Filter::anyOf() ->implementsInterface('MyInterface') ->hasMethod('method') ) ->includeDeclaredClasses() ->withCache(new MyPsr16Cache(), 'cacheKey') ->find(); var_dump($fqcns); /* output [ 'NamespaceA\FooClass', 'NamespaceB\BarClass', 'NamespaceC\Traits\HelperTrait' ] */
查找器
查找器是实现接口 FqcnFinderInterface
的类,并返回找到的 FQCN 列表(数组)。
<?php namespace FlexFqcnFinder; interface FqcnFinderInterface { /** * @return string[] The fully qualified class names found */ public function find(): array; }
此包提供了一些查找器
FlexFqcnFinder\Finder\FqcnFinder
(在目录中查找类、特质和接口)FlexFqcnFinder\Finder\ComposerClassMap
(从 Composer 自动加载类)FlexFqcnFinder\Finder\GetDeclaredClasses
(从get_declared_classes()
)FlexFqcnFinder\Finder\GetDeclaredInterfaces
(从get_declared_interfaces()
)FlexFqcnFinder\Finder\GetDeclaredTraits
(从get_declared_traits()
)
组合
您可以使用 FlexFqcnFinder\FqcnFinderComposite
来组合查找器
<?php use FlexFqcnFinder\FqcnFinderComposite; use FlexFqcnFinder\Finder\GetDeclaredClasses; use FlexFqcnFinder\Finder\FqcnFinder; use FlexFqcnFinder\Repository\FilesFromDir; $myFinder = new FqcnFinderComposite( new GetDeclaredClasses(), new FqcnFinder(new FilesFromDir('path/to/dir1')), new FqcnFinder(new FilesFromDir('path/to/dir2')) ); $fqcns = $myFinder->find();
装饰器
为查找器提供的装饰器
FlexFqcnFinder\Finder\Decorator\CachedFqcnFinder
FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder
您可以装饰任何查找器(包括组合)
<?php use FlexFqcnFinder\Finder\Decorator\CachedFqcnFinder; use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder; use FlexFqcnFinder\Filter\Specifications\IsSubClassOf; $myFinder = /* any finder, finder composition, ... */; $filtered = new FilteringFqcnFinder( $myFinder, new IsSubClassOf('MyBaseClass') ); // decorating again $cached = new CachedFqcnFinder($filtered, new MyPsr16Cache(), 'cacheKey'); $fqcns = $cached->find();
过滤器
过滤器可以用作查找器的装饰器,使用它是可选的。
所有过滤器都根据规范模式设计。您可以使用 Filter::by()
或 Filter::anyOf()
链接以下过滤器
apply(Closure $fn)
belongsToNamespace(string $namespace)
classNameEndsWith(string $value)
classNameStartsWith(string $value)
hasMethod(string $method)
implementsInterface(string $interface)
isAbstract()
isClass()
isCloneable()
isFinal()
isInstanceOf(string $subject)
isInstantiable()
isInterface()
isInternal()
isIterateable()
isSubClassOf(string $class)
isTrait()
isUserDefined()
namespaceEqualsTo(string $namespace)
not(FqcnSpecification $specification)
useTrait(string $trait)
anyOf(FqcnSpecification ...$specifications)
allOf(FqcnSpecification ...$specifications)
and(FqcnSpecification ...$specifications)
or(FqcnSpecification ...$specifications)
任何过滤器都可以与 FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder
装饰器一起使用
<?php use FlexFqcnFinder\Finder\FqcnFinder; use FlexFqcnFinder\Repository\FilesFromDir; use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder; use FlexFqcnFinder\Filter\Specifications\IsSubClassOf; $filtered = new FilteringFqcnFinder( new FqcnFinder(new FilesFromDir(__DIR__)), // first param: decorated Finder new IsSubClassOf('MyBaseClass') // second param: filters to apply ); // Or chaining filters: $filtered = new FilteringFqcnFinder( new FqcnFinder(new FilesFromDir(__DIR__)), Filter::anyOf() ->implementsInterface('MyInterface') ->hasMethod('execute') ->and( Filter::by() ->usingTrait('MyTrait') ->apply(function($fqcn) { return $fqcn === 'my_condition'; }) ) ); $fqcns = $filtered->find();
创建过滤器
您可以通过实现接口 FqcnSpecification
来创建自己的过滤器
<?php namespace Foo; use FlexFqcnFinder\Filter\FqcnSpecification; MyFilter implements FqcnSpecification { public function isSatisfiedBy(string $fqcn): bool { return $fqcn === /* ... */; } }
所以请使用它
<?php use FlexFqcnFinder\Finder\FqcnFinder; use FlexFqcnFinder\Repository\FilesFromDir; use FlexFqcnFinder\Finder\Decorator\FilteringFqcnFinder; use namespace Foo\MyFilter; $filtered = new FilteringFqcnFinder( new FqcnFinder(new FilesFromDir(__DIR__)), new MyFilter() ); $fqcns = $filtered->find();
贡献
请阅读贡献指南,了解如何为此项目做出贡献。
许可证
本项目根据 MIT 许可证的条款进行许可。有关许可证的权利和限制,请参阅LICENSE 文件。