laragear/discover
Composer 安装命令:
composer require laragear/discover
包简介
Discover and filter PHP Classes within a directory
README 文档
README
Discover and filter PHP Classes within a directory.
use Laragear\Discover\Facades\Discover; foreach (Discover::in('Rules') as $rule) { // ... };
Become a sponsor
Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can spread the word!
Requisites
- PHP 8.3 or later
- Laravel 12 or later
Installation
You can install the package via Composer:
composer require laragear/discover
Usage
The Discover finds classes under a given project path. It contains fluent methods to filter the classes to discover, like method and property names, interfaces, traits and attributes.
Let's make a simple example: list all classes that include the method handle(), inside the App\Scoreboards or deeper.
use Laragear\Discover\Facades\Discover; $classes = Discover::withMethod('handle')->in('Scoreboards')->allClasses();
The Discover class will automatically resolve your project path, and use your application path (app) and namespace (App) as the base to find the matching classes.
Important
The discovered classes must be PSR-4 autoloaded.
Application path and namespace
Most Laravel projects use the app and App as the application path and namespace, respectively. The library will use these, or any other set for your application.
To discover classes elsewhere, you will need to use the at() method to change both. If you don't set the namespace, it will be inferred from the path.
use Laragear\Discover\Facades\Discover; // Discover starting at the "my-project/score" using "Score" as the base namespace. $classes = Discover::at('score')->classes(); // Discover starting at the "my-project/match" using the "Matches" as the base namespace. $classes = Discover::at('/match', 'Matches')->classes();
Namespace
Use the in() method to set the base namespace to find classes. For example, if we want the classes in the App\Scoreboards namespace, we only need to set Scoreboardsboards.
use Laragear\Discover\Facades\Discover; $classes = Discover::in('Scoreboards')->classes();
Filters
You may use the included filters to find classes that are instances of another class or interfaces, or contains a given method, property or attribute.
use Laragear\Discover\Facades\Discover; use App\Score\Contracts\Score; use App\Score\Concerns\FiresEvents; use App\Attributes\Subscribable; // Filter all classes instances of at least one of the given classes/interfaces. Discover::in('Scoreboards')->instancesOf(ScoreContract::class)->classes(); // Filter all classes with at least one of the given public methods. Discover::in('Scoreboards')->withMethod('show')->classes(); // Filter all classes with at least one of the given public properties. Discover::in('Scoreboards')->withProperty('user')->classes(); // Filter all classes with at least one of the given traits. Discover::in('Scoreboards')->withTrait(FiresEvents::class)->classes(); // Filter all classes with at least one of the given attributes. Discover::in('Scoreboards')->withAttribute(Subscribable::class)->classes();
Additional filters
Since the classes are returned as ReflectionClass instances inside a Collection, you can further filter the list. For example, filter all the class names that don't end with Score.
use Laragear\Discover\Facades\Discover; use Illuminate\Support\Str; $classes = Discover::in('Scoreboards') ->classes() ->filter(fn ($class) => Str::endsWith($class->getName(), 'Score'));
Retrieving classes
Once you're done building your filters, you can retrieve the found classes as a Collection using classes().
use Laragear\Discover\Facades\Discover; // Find all classes in `App\Scoreboards`. $classes = Discover::in('Scoreboards')->classes();
The Discover class only looks for classes in the namespace set. To make the search recursive, you can use allClasses(), or use recursive() before the retrieval.
use Laragear\Discover\Facades\Discover; // Find all classes in `App\Scoreboards` and deeper. $classes = Discover::in('Scoreboards')->recursively()->classes(); // Same as... $classes = Discover::in('Scoreboards')->allClasses();
In any case, the Discoverer itself is iterable, so you can immediately use it inside a foreach loop.
use Laragear\Discover\Facades\Discover; foreach (Discover::recursive()->in('Scoreboards') as $class) { // ... }
You may also pass down any method Collection method or High Order method directly, which may save you a few keystrokes.
use Laragear\Discover\Facades\Discover; $classes = Discover::in('Scoreboards')->map->isFinal();
Outside Laravel
It's possible to use the Discoverer outside Laravel projects, as it only requires the illuminate/support library. You may need to set your project root path manually when instancing the Laragear\Discover\Discoverer class.
use Laragear\Discover\Discoverer; $discoverer = new Discoverer(__DIR__ . '/..'); foreach ($discoverer->at('software', 'Application')->in('Events') as $event) { // ... }
Laravel Octane compatibility
- There are no singletons using a stale application instance.
- There are no singletons using a stale config instance.
- There are no singletons using a stale request instance.
- There are no static properties written.
There should be no problems using this package with Laravel Octane.
Security
If you discover any security related issues, please email darkghosthunter@gmail.com instead of using the issue tracker.
Licence
This specific package version is licensed under the terms of the MIT License, at time of publishing.
Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2025 Laravel LLC.
laragear/discover 适用场景与选型建议
laragear/discover 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.32k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2024 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「class」 「classes」 「laravel」 「finder」 「discover」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 laragear/discover 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laragear/discover 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 laragear/discover 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Collection of methods that I or you usually use.
A very simple yet useful helper class to handle PHP file uploads.
Contao 4 Css Style Selector bundle
Yii2 js base class
Class loader implementation (PSR-0, PSR-4)
Some helpers for contao
统计信息
- 总下载量: 3.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-03
