Download the PHP package laragear/discover without Composer
On this page you can find all versions of the php package laragear/discover. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package discover
Discover
Discover and filter PHP Classes within a directory.
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.1 or later
- Laravel 10 or later (optional)
Installation
You can install the package via Composer:
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.
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 from()
method to change both. If you don't set the namespace, it will be inferred from the path.
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 Scoreboards
boards.
Filters
You may use the included filters to find classes that are instances of another class, or contains a given member or attribute.
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
.
Retrieving classes
Once you're done building, you can retrieve the found classes as a Collection using classes()
.
The Discover class only looks for classes in the namespace set. To make the search recursive, you can use allClasses()
, or set recursive()
before the retrieval.
In any case, the Discoverer
itself is iterable, so you can immediately use it inside a foreach
loop.
You may also pass down any method Collection method or High Order method directly, which may save you a few keystrokes.
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.
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 [email protected] 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-2024 Laravel LLC.