darkghosthunter / larafind
此包已被废弃,不再维护。未建议替代包。
一个小型实用工具,用于从项目路径中查找PSR-4类。
v1.0.0
2021-07-31 07:47 UTC
Requires
- php: ^8.0
- illuminate/container: 8.*
- illuminate/support: 8.*
- symfony/finder: 5.*
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.16
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2022-04-29 18:50:09 UTC
README
Larafind
一个小型实用工具,用于从基础应用程序路径或项目根目录中查找PSR-4类。
use DarkGhostHunter\Larafind\Facades\Find; use Illuminate\Database\Eloquent\Scope; $classes = Find::path('Scopes')->implementing(Scope::class)->get();
您可以将此用作“自动发现”开发人员(或您)可能在给定目录下可能存在的类的方法。
要求
- PHP 8.0
- Laravel 8.x
安装
您可以通过composer安装此包
composer require darkghosthunter/larafind
使用
使用Find
外观简化您的开发痛苦。外观创建了一种“构建器”类型的对象,将返回所有发现的PSR-4兼容类的列表,作为ReflectionClass
。
默认情况下,查找器将使用默认的app
目录,但您可以使用path()
方法在应用程序路径中查找特定文件夹。
use DarkGhostHunter\Larafind\Facades\Find; $classes = Find::path('Scopes')->get();
要查找项目根目录内的其他路径,请使用basePath()
方法。请注意,查找器确保您使用的路径已自动加载。
use DarkGhostHunter\Larafind\Facades\Find; $classes = Find::basePath('app_foo/Scopes')->get();
递归
发现是递归的,这意味着它将展开到子目录中。您可以使用nonRecursive()
使其非递归。
use DarkGhostHunter\Larafind\Facades\Find; $classes = Find::path('Scopes')->nonRecursive()->get();
过滤
Find
返回一个项目集合,因此您可以使用filter()
方法仅获取通过真值测试的类。
use DarkGhostHunter\Larafind\Facades\Find; $classes = Find::path('Scopes')->nonRecursive()->get() ->filter(fn($class) => str_starts_with($class->name, 'Foo'));
为了简化操作,您可以使用一些预过滤方法,以避免在获取集合后手动调用过滤器。
方法 | 描述 |
---|---|
implementing() |
通过实现接口进行过滤。 |
extends() |
通过扩展类进行过滤。 |
uses() |
通过使用所有特质进行过滤。 |
methods() |
通过公共方法进行过滤。 |
properties() |
通过公共属性进行过滤。 |
use DarkGhostHunter\Larafind\Facades\Find; use Illuminate\Database\Eloquent\Model; $arrayAccessible = Find::implementing(ArrayAccess::class)->get(); $eloquentModels = Find::extending(Model::class)->get(); $usesTraits = Find::using('App\MyCustomTrait')->get(); $hasMethod = Find::methods('handle', 'terminate')->get(); $hasProperties = Find::properties('service', 'model')->get();
许可证
MIT许可证(MIT)。请参阅许可证文件获取更多信息。