Download the PHP package baraja-core/class-map-generator without Composer
On this page you can find all versions of the php package baraja-core/class-map-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download baraja-core/class-map-generator
More information about baraja-core/class-map-generator
Files in baraja-core/class-map-generator
Package class-map-generator
Short Description Find all classes in specific directory.
License
Homepage https://github.com/baraja-core/class-map-generator
Informations about the package class-map-generator
PHP Class Map Generator
A lightweight PHP library for scanning directories and generating a complete map of all PHP classes, interfaces, and traits with their corresponding file paths.
π― Key Features
- Recursive directory scanning - Automatically traverses all subdirectories to find PHP files
- Token-based parsing - Uses PHP's native
token_get_all()for reliable class detection - Full namespace support - Correctly resolves fully qualified class names with namespaces
- Supports all class types - Detects classes, interfaces, and traits
- Memory efficient - Includes garbage collection optimization for large codebases
- Zero dependencies - Pure PHP implementation with no external requirements
- PHP 8.0+ compatible - Built for modern PHP applications
ποΈ Architecture
The library consists of a single, focused class that handles all scanning and parsing logic:
How It Works
-
Directory Iteration: The
createMap()method usesRecursiveDirectoryIteratorto traverse all files in the specified directory and its subdirectories. -
File Filtering: Only files with the
.phpextension are processed, skipping all other file types. -
Token Parsing: Each PHP file is read and parsed using PHP's
token_get_all()function, which breaks down the source code into tokens. -
Namespace Detection: The parser tracks
T_NAMESPACEtokens to build the current namespace context for class resolution. -
Class Detection: When
T_CLASS,T_INTERFACE, orT_TRAITtokens are encountered, the parser extracts the class name and combines it with the current namespace. -
::class Constant Handling: The parser intelligently skips
::classconstant usage (e.g.,SomeClass::class) to avoid false positives. - Memory Management: After processing each file,
gc_mem_caches()is called to address a known PHP memory issue withtoken_get_all().
π¦ Installation
It's best to use Composer for installation, and you can also find the package on Packagist and GitHub.
To install, simply use the command:
You can use the package manually by creating an instance of the internal classes, or register a DIC extension to link the services directly to the Nette Framework.
Requirements
- PHP 8.0 or higher
- No external dependencies
π Usage
Basic Usage
Generate a class map for a directory:
Return Value
The createMap() method returns an associative array where:
- Keys are fully qualified class names (including namespace)
- Values are absolute file paths
Practical Examples
Finding a Specific Class File
Listing All Classes in a Namespace
Building an Autoloader
Caching the Class Map
For performance optimization in production environments:
Scanning Multiple Directories
π What Gets Detected
The library detects the following PHP constructs:
| Construct | Detected | Example |
|---|---|---|
| Classes | Yes | class MyClass {} |
| Abstract classes | Yes | abstract class BaseController {} |
| Final classes | Yes | final class ImmutableValue {} |
| Interfaces | Yes | interface PaymentGateway {} |
| Traits | Yes | trait Loggable {} |
| Anonymous classes | No | new class {} |
| Enums | No | enum Status {} |
β οΈ Limitations
- Anonymous classes are not detected (they have no name to map)
- PHP Enums (PHP 8.1+) are not detected by the current implementation
- Files must be syntactically valid PHP - parsing errors may cause issues
- Multiple classes per file are fully supported, but only the class matching the filename is typically recommended
π§ Technical Notes
Memory Management
The library calls gc_mem_caches() after processing each file to address a known PHP memory issue where token_get_all() doesn't release memory properly. This ensures stable memory usage when scanning large codebases.
Path Resolution
The library attempts to get the real path of each file using getRealPath(). If this fails (e.g., on some edge cases with symlinks), it falls back to getPathname().
Inspiration
This library is based on the Symfony ClassLoader component, adapted and modernized for PHP 8.0+ with strict typing and simplified API.
π¨βπ» Author
Jan BarΓ‘Ε‘ek - https://baraja.cz
π License
baraja-core/class-map-generator is licensed under the MIT license. See the LICENSE file for more details.