PHP code example of baraja-core / class-map-generator

1. Go to this page and download the library: Download baraja-core/class-map-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

baraja-core / class-map-generator example snippets


use Baraja\ClassMapGenerator\ClassMapGenerator;

$map = ClassMapGenerator::createMap(__DIR__ . '/src');

// 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])) {
        

$cacheFile = __DIR__ . '/cache/classmap.php';

if (file_exists($cacheFile)) {
    $map = ts(
        $cacheFile,
        ' return ' . var_export($map, true) . ';'
    );
}

$directories = [
    __DIR__ . '/src',
    __DIR__ . '/lib',
    __DIR__ . '/modules',
];

$fullMap = [];

foreach ($directories as $dir) {
    $fullMap = array_merge($fullMap, ClassMapGenerator::createMap($dir));
}