A lightweight PHP library for scanning directories and generating a complete map of all PHP classes, interfaces, and traits with their corresponding file paths.
- 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
The library consists of a single, focused class that handles all scanning and parsing logic:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ClassMapGenerator β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββ β
β β createMap() βββββΊβ Directory Scan βββββΊβ Output β β
β β (public) β β (recursive) β β Map β β
β βββββββββββββββββββ ββββββββββ¬ββββββββββ βββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββββββ β
β β findClasses() β β
β β (private) β β
β ββββββββββ¬ββββββββββ β
β β β
β βΌ β
β ββββββββββββββββββββ β
β β Token Parser β β
β β - T_NAMESPACE β β
β β - T_CLASS β β
β β - T_INTERFACE β β
β β - T_TRAIT β β
β ββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-
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().
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:
$ composer require baraja-core/class-map-generatorYou 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.
- PHP 8.0 or higher
- No external dependencies
Generate a class map for a directory:
use Baraja\ClassMapGenerator\ClassMapGenerator;
$map = ClassMapGenerator::createMap(__DIR__ . '/src');The createMap() method returns an associative array where:
- Keys are fully qualified class names (including namespace)
- Values are absolute file paths
// Example output:
[
'App\Controllers\HomeController' => '/var/www/project/src/Controllers/HomeController.php',
'App\Models\User' => '/var/www/project/src/Models/User.php',
'App\Services\AuthService' => '/var/www/project/src/Services/AuthService.php',
]$map = ClassMapGenerator::createMap(__DIR__ . '/src');
if (isset($map['App\Services\PaymentService'])) {
$filePath = $map['App\Services\PaymentService'];
echo "PaymentService is located at: {$filePath}";
}$map = ClassMapGenerator::createMap(__DIR__ . '/src');
$controllers = array_filter(
$map,
fn($class) => str_starts_with($class, 'App\\Controllers\\'),
ARRAY_FILTER_USE_KEY
);
foreach ($controllers as $className => $filePath) {
echo "{$className}\n";
}$map = ClassMapGenerator::createMap(__DIR__ . '/src');
spl_autoload_register(function (string $class) use ($map): void {
if (isset($map[$class])) {
require_once $map[$class];
}
});For performance optimization in production environments:
$cacheFile = __DIR__ . '/cache/classmap.php';
if (file_exists($cacheFile)) {
$map = require $cacheFile;
} else {
$map = ClassMapGenerator::createMap(__DIR__ . '/src');
file_put_contents(
$cacheFile,
'<?php return ' . var_export($map, true) . ';'
);
}$directories = [
__DIR__ . '/src',
__DIR__ . '/lib',
__DIR__ . '/modules',
];
$fullMap = [];
foreach ($directories as $dir) {
$fullMap = array_merge($fullMap, ClassMapGenerator::createMap($dir));
}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 {} |
- 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
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.
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().
This library is based on the Symfony ClassLoader component, adapted and modernized for PHP 8.0+ with strict typing and simplified API.
Jan BarΓ‘Ε‘ek - https://baraja.cz
baraja-core/class-map-generator is licensed under the MIT license. See the LICENSE file for more details.